From 607d029030baa11f890af7b948b129c0ec975905 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Thu, 2 Mar 2023 07:28:22 +0300 Subject: [PATCH] Base figure adding --- Controllers/BundleController.cs | 211 ++++++++++++++++++-------------- Controllers/FigureController.cs | 21 ++++ Models/SeedData.cs | 118 +++++++++--------- Program.cs | 4 +- Views/Bundle/Details.cshtml | 7 +- Views/Figure/Details.cshtml | 1 + Views/Home/_Projects.cshtml | 9 +- 7 files changed, 210 insertions(+), 161 deletions(-) diff --git a/Controllers/BundleController.cs b/Controllers/BundleController.cs index fe52a6e..a802a34 100644 --- a/Controllers/BundleController.cs +++ b/Controllers/BundleController.cs @@ -1,109 +1,132 @@ using System.Data; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using MyDarling.Models; namespace MyDarling.Controllers { - public class BundleController : Controller - { - private DataContext context; + public class BundleController : Controller + { + private DataContext context; + private IWebHostEnvironment environment; - public BundleController(DataContext context) - { - this.context = context; - } + public BundleController(DataContext context, IWebHostEnvironment environment) + { + this.environment = environment; + this.context = context; + } - public ActionResult Index() - { - return View(context.UnderwearBundles.Include(b => b.Figures)); - } + public ActionResult Index() + { + return View(context.UnderwearBundles.Include(b => b.Figures)); + } - public ActionResult Create() - { - return View(); - } + public ActionResult Create() + { + return View(); + } - [HttpPost] - public async Task Create([Bind] UnderwearBundle bundle) - { - try - { - if (ModelState.IsValid) - { - await context.UnderwearBundles.AddAsync(bundle); - context.SaveChanges(); - return RedirectToAction(nameof(Index)); - } - } - catch (DataException) - { - ModelState.AddModelError("", "Unable to save changes"); - } - return View(bundle); - } + [HttpPost] + public async Task Create([Bind] UnderwearBundle bundle) + { + try + { + if (ModelState.IsValid) + { + await context.UnderwearBundles.AddAsync(bundle); + context.SaveChanges(); + return RedirectToAction(nameof(Index)); + } + } + catch (DataException) + { + ModelState.AddModelError("", "Unable to save changes"); + } + return View(bundle); + } - public async Task Details(int id) - { - return View(await context.UnderwearBundles.Include(b => b.Figures).Where(b => b.Id == id).FirstOrDefaultAsync()); - } - - public async Task Edit(int id) - { - return View(nameof(Details), await context.UnderwearBundles.FindAsync(id)); - } + public async Task Details(int id) + { + return View(await context.UnderwearBundles.Include(b => b.Figures).Where(b => b.Id == id).FirstOrDefaultAsync()); + } - [HttpPost] - public async Task Edit(int? id) - { - if (id == null) - { - return NotFound(); - } - - var bundle = await context.UnderwearBundles.FindAsync(id); - if (bundle == null) - { - return NotFound(); - } - - if (await TryUpdateModelAsync( - bundle, - "", - b => b.Name, b => b.Description, b => b.Figures, b => b.Price)) - { - try - { - await context.SaveChangesAsync(); - return RedirectToAction(nameof(Index)); - } - catch (System.Exception) - { - ModelState.AddModelError("", "Unable to save changes"); - } - } - return View(bundle); - } + public async Task Edit(int id) + { + return View(nameof(Details), await context.UnderwearBundles.FindAsync(id)); + } - [HttpPost] - public async Task Delete(int id) - { - var bundleToDelete = await context.UnderwearBundles.FindAsync(id); - if (bundleToDelete == null) - { - return NotFound(); - } - - try - { - context.UnderwearBundles.Remove(bundleToDelete); - await context.SaveChangesAsync(); - return RedirectToAction(nameof(Index)); - } - catch(DbUpdateException) - { - return RedirectToAction(nameof(Delete), new {id = id, saveChangesError = true}); - } - } - } + [HttpPost] + public async Task Edit(int? id) + { + + if (id == null) + { + return NotFound(); + } + + var bundle = await context.UnderwearBundles.FindAsync(id); + if (bundle == null) + { + return NotFound(); + } + + var file = Request.Form.Files.FirstOrDefault(); + + if (await TryUpdateModelAsync( + bundle, + "", + b => b.Name, b => b.Description, b => b.Figures, b => b.Price)) + { + if (file != null) + { + var newFigure = new Figure(); + bundle.Figures.Add(newFigure); + newFigure.FilePath = $"/Content/{bundle.Id}/{Guid.NewGuid()}{Path.GetExtension(file.FileName)}"; + var savePath = environment.WebRootPath + "/Content/" + bundle.Id + "/"; + if (!Directory.Exists(savePath)) + { + Directory.CreateDirectory(savePath); + } + using var fileStream = new FileStream(environment.WebRootPath + newFigure.FilePath, FileMode.Create); + await file.CopyToAsync(fileStream); + } + + try + { + await context.SaveChangesAsync(); + return RedirectToAction(nameof(Index)); + } + + catch (System.Exception) + { + ModelState.AddModelError("", "Unable to save changes"); + } + + + } + return View(bundle); + } + + [HttpPost] + public async Task Delete(int id) + { + var bundleToDelete = await context.UnderwearBundles.FindAsync(id); + if (bundleToDelete == null) + { + return NotFound(); + } + + try + { + context.UnderwearBundles.Remove(bundleToDelete); + await context.SaveChangesAsync(); + return RedirectToAction(nameof(Index)); + } + catch (DbUpdateException) + { + return RedirectToAction(nameof(Delete), new { id = id, saveChangesError = true }); + } + } + } } \ No newline at end of file diff --git a/Controllers/FigureController.cs b/Controllers/FigureController.cs index 5785f86..03616bc 100644 --- a/Controllers/FigureController.cs +++ b/Controllers/FigureController.cs @@ -54,5 +54,26 @@ namespace MyDarling.Controllers } return View(figure); } + + [HttpPost] + public async Task Delete(int id) + { + var figureToDelete = await context.Figures.FindAsync(id); + if (figureToDelete == null) + { + return NotFound(); + } + + try + { + context.Figures.Remove(figureToDelete); + await context.SaveChangesAsync(); + return RedirectToAction(nameof(Index), "Bundle"); + } + catch (DbUpdateException) + { + return RedirectToAction(nameof(Delete), new { id = id, saveChangesError = true }); + } + } } } \ No newline at end of file diff --git a/Models/SeedData.cs b/Models/SeedData.cs index da8263f..3a15543 100644 --- a/Models/SeedData.cs +++ b/Models/SeedData.cs @@ -1,64 +1,64 @@ -using Microsoft.EntityFrameworkCore; -namespace MyDarling.Models -{ - public static class SeedData - { - public static void SeedDatabase(DataContext context) - { - context.Database.Migrate(); - if (context.UnderwearBundles.Count() == 0) - { - var aliceFigures = new List
- { - new Figure() - { - Description = @"Комплект из бежевого эластичного кружева с голубой отделкой.", - FilePath = "/content/0/img/IMG_4896.JPG" - }, - new Figure() - { - Description = @"В комплект входит бра, 2 трусиков (на высокой посадке и стандартной на регуляции) и чокер. Низ можно сделать на выбор стринги/бразильянки.", - FilePath = "/content/0/img/IMG_4902.JPG" - } - }; +// using Microsoft.EntityFrameworkCore; +// namespace MyDarling.Models +// { +// public static class SeedData +// { +// public static void SeedDatabase(DataContext context) +// { +// context.Database.Migrate(); +// if (context.UnderwearBundles.Count() == 0) +// { +// var aliceFigures = new List
+// { +// new Figure() +// { +// Description = @"Комплект из бежевого эластичного кружева с голубой отделкой.", +// FilePath = "/content/0/img/IMG_4896.JPG" +// }, +// new Figure() +// { +// Description = @"В комплект входит бра, 2 трусиков (на высокой посадке и стандартной на регуляции) и чокер. Низ можно сделать на выбор стринги/бразильянки.", +// FilePath = "/content/0/img/IMG_4902.JPG" +// } +// }; - var nikkiFigures = new List
- { - new Figure() - { - Description = @"Базовый сет из мягкой эластичной сетки.", - FilePath = "/content/1/img/IMG_4897.JPG" - }, - new Figure() - { - Description = @"В комплект входит лиф на косточках и 2 трусиков – бразильянки на высокой посадке и стринги на стандартной посадке с регуляцией. Доступен в цветах: желтый, черный, бежевый молочный.", - FilePath = "/content/1/img/IMG_4898.JPG" - } - }; +// var nikkiFigures = new List
+// { +// new Figure() +// { +// Description = @"Базовый сет из мягкой эластичной сетки.", +// FilePath = "/content/1/img/IMG_4897.JPG" +// }, +// new Figure() +// { +// Description = @"В комплект входит лиф на косточках и 2 трусиков – бразильянки на высокой посадке и стринги на стандартной посадке с регуляцией. Доступен в цветах: желтый, черный, бежевый молочный.", +// FilePath = "/content/1/img/IMG_4898.JPG" +// } +// }; - context.Figures.AddRange(aliceFigures); - context.Figures.AddRange(nikkiFigures); - context.SaveChanges(); +// context.Figures.AddRange(aliceFigures); +// context.Figures.AddRange(nikkiFigures); +// context.SaveChanges(); - var alice = new UnderwearBundle - { - Name = "Alice", - Figures = aliceFigures, - Description = @"Комплект из бежевого эластичного кружева с голубой отделкой.", - Price = 3000 - }; +// var alice = new UnderwearBundle +// { +// Name = "Alice", +// Figures = aliceFigures, +// Description = @"Комплект из бежевого эластичного кружева с голубой отделкой.", +// Price = 3000 +// }; - var nikki = new UnderwearBundle - { - Name = "Nikki", - Figures = nikkiFigures, - Description = @"Базовый сет из мягкой эластичной сетки.", - Price = 3800 - }; +// var nikki = new UnderwearBundle +// { +// Name = "Nikki", +// Figures = nikkiFigures, +// Description = @"Базовый сет из мягкой эластичной сетки.", +// Price = 3800 +// }; - context.UnderwearBundles.AddRange(alice, nikki); - context.SaveChanges(); - } - } - } -} \ No newline at end of file +// context.UnderwearBundles.AddRange(alice, nikki); +// context.SaveChanges(); +// } +// } +// } +// } \ No newline at end of file diff --git a/Program.cs b/Program.cs index 4079b2b..61ee7fb 100644 --- a/Program.cs +++ b/Program.cs @@ -17,7 +17,7 @@ app.UseStaticFiles(); app.MapControllers(); app.MapDefaultControllerRoute(); -var context = app.Services.CreateScope().ServiceProvider.GetRequiredService(); -SeedData.SeedDatabase(context); +// var context = app.Services.CreateScope().ServiceProvider.GetRequiredService(); +// SeedData.SeedDatabase(context); app.Run(); \ No newline at end of file diff --git a/Views/Bundle/Details.cshtml b/Views/Bundle/Details.cshtml index b7df95b..ac13624 100644 --- a/Views/Bundle/Details.cshtml +++ b/Views/Bundle/Details.cshtml @@ -11,7 +11,7 @@ -
+
@@ -33,9 +33,12 @@
- } + } +
+ +
diff --git a/Views/Figure/Details.cshtml b/Views/Figure/Details.cshtml index 2148ed3..ace2a9a 100644 --- a/Views/Figure/Details.cshtml +++ b/Views/Figure/Details.cshtml @@ -26,6 +26,7 @@ @Html.TextAreaFor(model => model.Description, new { @class="form-control", @rows = 2 })
+
diff --git a/Views/Home/_Projects.cshtml b/Views/Home/_Projects.cshtml index 2cb439d..1eb67bc 100644 --- a/Views/Home/_Projects.cshtml +++ b/Views/Home/_Projects.cshtml @@ -4,13 +4,14 @@
- @foreach (var bundle in @Model.Where(b => b.Price != 0)) + @foreach (var bundle in @Model.Where(b => b.Price != 0 && b.Figures.Count > 0) + .OrderByDescending(b => b.Id)) {
- @bundle.Name + @bundle.Name @for (int i = 1; i < @bundle.Figures.Count(); i++) {