diff options
Diffstat (limited to 'Controllers/FigureController.cs')
-rw-r--r-- | Controllers/FigureController.cs | 166 |
1 files changed, 93 insertions, 73 deletions
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 |