From 3bade8859bcd938b85c39ab16eaa0dcf8e01535f Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Sat, 3 Jun 2023 07:41:46 +0300 Subject: Mass refactoring --- Controllers/ProductsController.cs | 133 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 Controllers/ProductsController.cs (limited to 'Controllers/ProductsController.cs') diff --git a/Controllers/ProductsController.cs b/Controllers/ProductsController.cs new file mode 100644 index 0000000..14a4b27 --- /dev/null +++ b/Controllers/ProductsController.cs @@ -0,0 +1,133 @@ +using System.Data; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using MyDarling.Models; + +namespace MyDarling.Controllers +{ + [Authorize] + public class ProductsController : Controller + { + private DataContext context; + private IWebHostEnvironment environment; + + public ProductsController(DataContext context, IWebHostEnvironment environment) + { + this.environment = environment; + this.context = context; + } + + public IActionResult Index() + { + return View(context.Products.Include(b => b.Figures)); + } + + public IActionResult Create() + { + return View(); + } + + [HttpPost] + public async Task Create([Bind] Product bundle) + { + try + { + if (ModelState.IsValid) + { + await context.Products.AddAsync(bundle); + context.SaveChanges(); + return RedirectToAction(nameof(Index)); + } + } + catch (DataException) + { + ModelState.AddModelError("", "Unable to save changes"); + } + return View(bundle); + } + + public async Task Details(string id) + { + return View(await context.Products.Include(b => b.Figures).Where(b => b.Id.Equals(id)).FirstOrDefaultAsync()); + } + + [HttpPost] + public async Task Edit(string id) + { + if (string.IsNullOrEmpty(id)) + { + return NotFound(); + } + + var product = await context.Products.FindAsync(id); + if (product == null) + { + return NotFound(); + } + + var file = Request.Form.Files.FirstOrDefault(); + + if (await TryUpdateModelAsync( + product, + "", + b => b.Name, b => b.Description, b => b.Figures, b => b.Price)) + { + if (file != null) + { + var newFigure = new Figure(string.Empty, product.Id); + product.Figures.Add(newFigure); + string filePath = $"/Content/{product.Id}/{newFigure.Id}.jpg"; + var savePath = environment.WebRootPath + "/Content/" + product.Id + "/"; + if (!Directory.Exists(savePath)) + { + Directory.CreateDirectory(savePath); + } + using var fileStream = new FileStream(environment.WebRootPath + filePath, FileMode.Create); + await file.CopyToAsync(fileStream); + } + + try + { + await context.SaveChangesAsync(); + return RedirectToAction("Details", "Products", new { Id = product?.Id}); + } + + catch (System.Exception) + { + ModelState.AddModelError("", "Unable to save changes"); + } + } + return View(product); + } + + [HttpPost] + public async Task Delete(string id) + { + var productToDelete = await context.Products.FindAsync(id); + if (productToDelete == null) + { + return NotFound(); + } + + try + { + var bundleDirPath = String.Concat(environment.WebRootPath, + "/Content/", + productToDelete.Id); + + if (Directory.Exists(bundleDirPath)) + { + Directory.Delete(bundleDirPath, true); + } + context.Products.Remove(productToDelete); + await context.SaveChangesAsync(); + return RedirectToAction(nameof(Index)); + } + catch (DbUpdateException) + { + return RedirectToAction(nameof(Delete), new { id = id, saveChangesError = true }); + } + } + } +} \ No newline at end of file -- cgit v1.2.3