summaryrefslogtreecommitdiff
path: root/Controllers
diff options
context:
space:
mode:
authorSergey Chebotar <s.chebotar@gmail.com>2023-06-03 07:41:46 +0300
committerSergey Chebotar <s.chebotar@gmail.com>2023-06-03 07:41:46 +0300
commit3bade8859bcd938b85c39ab16eaa0dcf8e01535f (patch)
tree9157e3f27d8007206c12d5ec0cf5f2936295c0e7 /Controllers
parent81c1fc0c14253457c3c4fc24735e787ace1db70b (diff)
Mass refactoring
Diffstat (limited to 'Controllers')
-rw-r--r--Controllers/BundleController.cs147
-rw-r--r--Controllers/FigureController.cs166
-rw-r--r--Controllers/ProductsController.cs133
3 files changed, 226 insertions, 220 deletions
diff --git a/Controllers/BundleController.cs b/Controllers/BundleController.cs
deleted file mode 100644
index b1d61fd..0000000
--- a/Controllers/BundleController.cs
+++ /dev/null
@@ -1,147 +0,0 @@
-using System.Data;
-using Microsoft.AspNetCore.Authorization;
-using Microsoft.AspNetCore.Mvc;
-using Microsoft.EntityFrameworkCore;
-using MyDarling.Models;
-
-namespace MyDarling.Controllers
-{
- [Authorize]
- 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
- {
- var bundleDirPath = String.Concat(environment.WebRootPath,
- "/Content/",
- bundleToDelete.Id);
-
- if (Directory.Exists(bundleDirPath))
- {
- Directory.Delete(bundleDirPath, true);
- }
- // foreach (var figure in bundleToDelete.Figures)
- // {
- // using FigureController controller = new(context, environment);
- // await controller.Delete(figure.Id);
- // }
-
- 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 9f6cf0c..bad8791 100644
--- a/Controllers/FigureController.cs
+++ b/Controllers/FigureController.cs
@@ -6,84 +6,104 @@ using MyDarling.Models;
namespace MyDarling.Controllers
{
- [Authorize]
- public class FigureController : Controller
- {
- private DataContext context;
- private IWebHostEnvironment environment;
- public FigureController(DataContext context, IWebHostEnvironment environment)
- {
- this.context = context;
- this.environment = environment;
- }
+ [Authorize]
+ public class FigureController : Controller
+ {
+ private DataContext context;
+ private IWebHostEnvironment environment;
+ public FigureController(DataContext context, IWebHostEnvironment environment)
+ {
+ this.context = context;
+ this.environment = environment;
+ }
- public async Task<IActionResult> Details(int id)
- {
- return View(await context.Figures.Where(f => f.Id == id).FirstOrDefaultAsync());
- }
+ public async Task<IActionResult> Details(string id)
+ {
+ var figure = await context.Figures
+ .Where(f => f.Id.Equals(id))
+ .FirstOrDefaultAsync();
+ if (figure == null)
+ {
+ return NotFound();
+ }
+ var product = await context.Products
+ .Where(b => b.Figures.Contains(figure))
+ .FirstOrDefaultAsync();
+ if (product == null)
+ {
+ return NotFound();
+ }
+ return View(figure);
+ }
- [HttpPost]
- public async Task<IActionResult> Edit(int? id)
- {
- if (id == null)
- {
- return NotFound();
- }
+ [HttpPost]
+ public async Task<IActionResult> Edit(string id)
+ {
+ if (id == null)
+ {
+ return NotFound();
+ }
- var figure = await context.Figures.FindAsync(id);
+ var figure = await context.Figures
+ .Where(f => f.Id.Equals(id))
+ .FirstOrDefaultAsync();
- if (figure == null)
- {
- return NotFound();
- }
+ if (figure == null)
+ {
+ return NotFound();
+ }
- var bundle = await context.UnderwearBundles
- .Where(b => b.Figures.Contains(figure))
- .FirstOrDefaultAsync();
+ var product = await context.Products
+ .Where(b => b.Figures.Contains(figure))
+ .FirstOrDefaultAsync();
- if (await TryUpdateModelAsync<Figure>(
- figure,
- "",
- f => f.Description))
- {
- try
- {
- await context.SaveChangesAsync();
- return RedirectToAction("Details", "Bundle", new { Id = bundle?.Id});
- }
- catch (SystemException)
- {
- ModelState.AddModelError("", "Unable to save changes");
- }
- }
- return View(figure);
- }
-
- [HttpPost]
- public async Task<ActionResult> Delete(int id)
- {
- var figureToDelete = await context.Figures.FindAsync(id);
- if (figureToDelete == null)
- {
- return NotFound();
- }
+ if (await TryUpdateModelAsync<Figure>(
+ figure,
+ "",
+ f => f.Description))
+ {
+ try
+ {
+ await context.SaveChangesAsync();
+ return RedirectToAction("Details", "Products", new { Id = product?.Id });
+ }
+ catch (SystemException)
+ {
+ ModelState.AddModelError("", "Unable to save changes");
+ }
+ }
+ return View(figure);
+ }
- try
- {
- FileInfo figureFile = new FileInfo(environment.WebRootPath + figureToDelete.FilePath);
- if (figureFile.Exists)
- {
- figureFile.Delete();
- }
-
- context.Figures.Remove(figureToDelete);
- await context.SaveChangesAsync();
- return RedirectToAction(nameof(Index), "Bundle");
- }
- catch (DbUpdateException)
- {
- return RedirectToAction(nameof(Delete), new { id = id, saveChangesError = true });
- }
- }
- }
+ [HttpPost]
+ public async Task<ActionResult> Delete(string id)
+ {
+ var figure = await context.Figures.FindAsync(id);
+ if (figure == null)
+ {
+ return NotFound();
+ }
+ var product = await context.Products
+ .Where(b => b.Figures.Contains(figure))
+ .FirstAsync();
+
+ try
+ {
+ string filePath = $"/Content/{product.Id}/{figure.Id}.jpg";
+ FileInfo figureFile = new FileInfo(environment.WebRootPath + filePath);
+ if (figureFile.Exists)
+ {
+ figureFile.Delete();
+ }
+
+ context.Figures.Remove(figure);
+ await context.SaveChangesAsync();
+ return RedirectToAction("Details", "Products", new { Id = product?.Id });
+ }
+ catch (DbUpdateException)
+ {
+ return RedirectToAction(nameof(Delete), new { id = id, saveChangesError = true });
+ }
+ }
+ }
} \ No newline at end of file
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<IActionResult> 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<IActionResult> Details(string id)
+ {
+ return View(await context.Products.Include(b => b.Figures).Where(b => b.Id.Equals(id)).FirstOrDefaultAsync());
+ }
+
+ [HttpPost]
+ public async Task<IActionResult> 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>(
+ 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<IActionResult> 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