summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Controllers/BundleController.cs225
-rw-r--r--Controllers/FigureController.cs21
-rw-r--r--Models/SeedData.cs118
-rw-r--r--Program.cs4
-rw-r--r--Views/Bundle/Details.cshtml7
-rw-r--r--Views/Figure/Details.cshtml1
-rw-r--r--Views/Home/_Projects.cshtml9
7 files changed, 217 insertions, 168 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 BundleController(DataContext context)
- {
- this.context = context;
- }
-
- public ActionResult Index()
- {
- return View(context.UnderwearBundles.Include(b => b.Figures));
- }
-
- public ActionResult Create()
- {
- return View();
- }
-
- [HttpPost]
- public async Task<ActionResult> 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<ActionResult> Details(int id)
- {
- return View(await context.UnderwearBundles.Include(b => b.Figures).Where(b => b.Id == id).FirstOrDefaultAsync());
- }
-
- public async Task<ActionResult> Edit(int id)
- {
- return View(nameof(Details), await context.UnderwearBundles.FindAsync(id));
- }
-
- [HttpPost]
- public async Task<ActionResult> Edit(int? id)
- {
- if (id == null)
- {
- return NotFound();
- }
-
- var bundle = await context.UnderwearBundles.FindAsync(id);
- if (bundle == null)
- {
- return NotFound();
- }
-
- if (await TryUpdateModelAsync<UnderwearBundle>(
- 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);
- }
-
- [HttpPost]
- public async Task<ActionResult> 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});
- }
- }
- }
+ public class BundleController : Controller
+ {
+ private DataContext context;
+ private IWebHostEnvironment environment;
+
+ 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 Create()
+ {
+ return View();
+ }
+
+ [HttpPost]
+ public async Task<ActionResult> 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<ActionResult> Details(int id)
+ {
+ return View(await context.UnderwearBundles.Include(b => b.Figures).Where(b => b.Id == id).FirstOrDefaultAsync());
+ }
+
+ public async Task<ActionResult> Edit(int id)
+ {
+ return View(nameof(Details), await context.UnderwearBundles.FindAsync(id));
+ }
+
+ [HttpPost]
+ public async Task<ActionResult> 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<UnderwearBundle>(
+ 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<ActionResult> 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<ActionResult> 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<Figure>
- {
- 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<Figure>
+// {
+// 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<Figure>
- {
- 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<Figure>
+// {
+// 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<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/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 @@
<body>
<container>
- <form asp-action="Edit" asp-route-id="@Model.Id" method="post" class="m-2">
+ <form asp-action="Edit" asp-route-id="@Model.Id" method="post" enctype="multipart/form-data" class="m-2">
<div asp-validation-summary="All"></div>
<div class="form-group">
<label asp-for="Name" class="form-label">Name:</label>
@@ -33,10 +33,13 @@
</a>
</div>
</div>
- }
+ }
</div>
</div>
<div class="form-group">
+ <input type="file" name="file" />
+ </div>
+ <div class="form-group">
<label asp-for="Price" class="form-label">Price:</label>
<input asp-for="Price" class="form-control" />
</div>
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 })
</div>
<button type="submit" class="btn btn-primary mt-3">Save</button>
+ <button asp-action="Delete" asp-route-id="@Model.Id" method="post" class="btn btn-primary mt-3">Delete</button>
</form>
</body>
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 @@
<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.Where(b => b.Price != 0))
+ @foreach (var bundle in @Model.Where(b => b.Price != 0 && b.Figures.Count > 0)
+ .OrderByDescending(b => b.Id))
{
<div class="col mb-5">
<div class="card h-100">
- <a data-src="@bundle.Figures.FirstOrDefault()?.FilePath" data-fancybox="@bundle.Id"
- data-caption="@bundle.Figures.FirstOrDefault()?.Description"><img class="card-img-top"
- src="@bundle.Figures.FirstOrDefault()?.FilePath" alt="@bundle.Name" /></a>
+ <a data-src="@bundle.Figures[0].FilePath" data-fancybox="@bundle.Id"
+ data-caption="@bundle.Figures[0].Description"><img class="card-img-top"
+ src="@bundle.Figures[0].FilePath" alt="@bundle.Name" /></a>
@for (int i = 1; i < @bundle.Figures.Count(); i++)
{
<a data-src="@bundle.Figures[i].FilePath" data-fancybox="@bundle.Id"