diff options
-rw-r--r-- | Controllers/HomeController.cs | 8 | ||||
-rw-r--r-- | Models/IRepository.cs | 10 | ||||
-rw-r--r-- | Models/MyDarlingRepository.cs | 32 | ||||
-rw-r--r-- | Program.cs | 11 | ||||
-rw-r--r-- | Views/Home/_Projects.cshtml | 4 |
5 files changed, 54 insertions, 11 deletions
diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index 255af9f..228a9c3 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -6,14 +6,14 @@ namespace MyDarling.Controllers { public class HomeController : Controller { - private DataContext context; - public HomeController(DataContext context) + private IRepository repository; + public HomeController(IRepository repository) { - this.context = context; + this.repository = repository; } public IActionResult Index() { - return View(context.UnderwearBundles.Include(bundle => bundle.Figures).Where(x => x.Price != 0)); + return View(repository); } } }
\ No newline at end of file diff --git a/Models/IRepository.cs b/Models/IRepository.cs new file mode 100644 index 0000000..363f48d --- /dev/null +++ b/Models/IRepository.cs @@ -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(); + } +}
\ No newline at end of file diff --git a/Models/MyDarlingRepository.cs b/Models/MyDarlingRepository.cs new file mode 100644 index 0000000..07caff8 --- /dev/null +++ b/Models/MyDarlingRepository.cs @@ -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(); + } + } +}
\ No newline at end of file @@ -3,11 +3,12 @@ using MyDarling.Models; var builder = WebApplication.CreateBuilder(args); -builder.Services.AddDbContext<DataContext>(opts => +builder.Services.AddDbContext<DataContext>(opts => { - opts.UseSqlite(builder.Configuration["ConnectionStrings:MyDarlingDb"]); - opts.EnableSensitiveDataLogging(true); + opts.UseSqlite(builder.Configuration["ConnectionStrings:MyDarlingDb"]); + opts.EnableSensitiveDataLogging(true); }); +builder.Services.AddSingleton<IRepository, MyDarlingRepository>(); builder.Services.AddControllersWithViews(); var app = builder.Build(); @@ -16,7 +17,7 @@ app.UseStaticFiles(); app.MapControllers(); app.MapDefaultControllerRoute(); -var context = app.Services.CreateScope().ServiceProvider.GetRequiredService<DataContext>(); -SeedData.SeedDatabase(context); +// var context = app.Services.CreateScope().ServiceProvider.GetRequiredService<DataContext>(); +// SeedData.SeedDatabase(context); app.Run();
\ No newline at end of file diff --git a/Views/Home/_Projects.cshtml b/Views/Home/_Projects.cshtml index d35fc11..314a9f0 100644 --- a/Views/Home/_Projects.cshtml +++ b/Views/Home/_Projects.cshtml @@ -1,10 +1,10 @@ -@model IQueryable<MyDarling.Models.UnderwearBundle>; +@model MyDarling.Models.IRepository; @using System.Globalization; <section class="projects-section bg-light" id="projects"> <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"> - @foreach (var bundle in @Model) + @foreach (var bundle in @Model.Bundles.Where(b => b.Price != 0)) { <div class="col mb-5"> <div class="card h-100"> |