0
0

Merge branch 'di'

This commit is contained in:
Sergey Chebotar 2023-02-16 08:05:29 +03:00
commit a27cab1321
5 changed files with 54 additions and 11 deletions

View File

@ -6,14 +6,14 @@ namespace MyDarling.Controllers
{ {
public class HomeController : Controller public class HomeController : Controller
{ {
private DataContext context; private IRepository repository;
public HomeController(DataContext context) public HomeController(IRepository repository)
{ {
this.context = context; this.repository = repository;
} }
public IActionResult Index() public IActionResult Index()
{ {
return View(context.UnderwearBundles.Include(bundle => bundle.Figures).Where(x => x.Price != 0)); return View(repository);
} }
} }
} }

10
Models/IRepository.cs Normal file
View File

@ -0,0 +1,10 @@
namespace MyDarling.Models
{
public interface IRepository
{
public IQueryable<UnderwearBundle> Bundles { get; }
public void Add(UnderwearBundle b);
public void Remove(UnderwearBundle p);
public void Save();
}
}

View File

@ -0,0 +1,32 @@
using Microsoft.EntityFrameworkCore;
namespace MyDarling.Models
{
public class MyDarlingRepository : IRepository
{
private DataContext context;
public MyDarlingRepository(IServiceProvider provider)
{
context = provider.CreateScope().ServiceProvider.GetRequiredService<DataContext>();
}
public IQueryable<UnderwearBundle> Bundles => context.UnderwearBundles.Include(b => b.Figures);
public void Add(UnderwearBundle b)
{
context.UnderwearBundles.Add(b);
context.SaveChanges();
}
public void Remove(UnderwearBundle b)
{
context.UnderwearBundles.Remove(b);
context.SaveChanges();
}
public void Save()
{
context.SaveChanges();
}
}
}

View File

@ -3,11 +3,12 @@ using MyDarling.Models;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<DataContext>(opts => builder.Services.AddDbContext<DataContext>(opts =>
{ {
opts.UseSqlite(builder.Configuration["ConnectionStrings:MyDarlingDb"]); opts.UseSqlite(builder.Configuration["ConnectionStrings:MyDarlingDb"]);
opts.EnableSensitiveDataLogging(true); opts.EnableSensitiveDataLogging(true);
}); });
builder.Services.AddSingleton<IRepository, MyDarlingRepository>();
builder.Services.AddControllersWithViews(); builder.Services.AddControllersWithViews();
var app = builder.Build(); var app = builder.Build();
@ -16,7 +17,7 @@ app.UseStaticFiles();
app.MapControllers(); app.MapControllers();
app.MapDefaultControllerRoute(); app.MapDefaultControllerRoute();
var context = app.Services.CreateScope().ServiceProvider.GetRequiredService<DataContext>(); // var context = app.Services.CreateScope().ServiceProvider.GetRequiredService<DataContext>();
SeedData.SeedDatabase(context); // SeedData.SeedDatabase(context);
app.Run(); app.Run();

View File

@ -1,10 +1,10 @@
@model IQueryable<MyDarling.Models.UnderwearBundle>; @model MyDarling.Models.IRepository;
@using System.Globalization; @using System.Globalization;
<section class="projects-section bg-light" id="projects"> <section class="projects-section bg-light" id="projects">
<div class="container px-3 px-lg-4 mt-4"> <div class="container px-3 px-lg-4 mt-4">
<div class="row gx-4 gx-lg-5 row-cols-2 row-cols-md-3 row-cols-xl-4 justify-content-center"> <div class="row gx-4 gx-lg-5 row-cols-2 row-cols-md-3 row-cols-xl-4 justify-content-center">
@foreach (var bundle in @Model) @foreach (var bundle in @Model.Bundles.Where(b => b.Price != 0))
{ {
<div class="col mb-5"> <div class="col mb-5">
<div class="card h-100"> <div class="card h-100">