From 0f5cbb4d87388f6490915a53a2e83c9b93961c25 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Mon, 5 Jun 2023 07:15:14 +0300 Subject: [PATCH] Delete thumb file --- Controllers/FigureController.cs | 189 +++++++++++++++++--------------- 1 file changed, 98 insertions(+), 91 deletions(-) diff --git a/Controllers/FigureController.cs b/Controllers/FigureController.cs index bad8791..76edb55 100644 --- a/Controllers/FigureController.cs +++ b/Controllers/FigureController.cs @@ -6,104 +6,111 @@ 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 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); - } + public async Task 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 Edit(string id) - { - if (id == null) - { - return NotFound(); - } + [HttpPost] + public async Task Edit(string id) + { + if (id == null) + { + return NotFound(); + } - var figure = await context.Figures - .Where(f => f.Id.Equals(id)) - .FirstOrDefaultAsync(); + var figure = await context.Figures + .Where(f => f.Id.Equals(id)) + .FirstOrDefaultAsync(); - if (figure == null) - { - return NotFound(); - } + if (figure == null) + { + return NotFound(); + } - var product = await context.Products - .Where(b => b.Figures.Contains(figure)) - .FirstOrDefaultAsync(); + var product = await context.Products + .Where(b => b.Figures.Contains(figure)) + .FirstOrDefaultAsync(); - if (await TryUpdateModelAsync
( - 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); - } + if (await TryUpdateModelAsync
( + 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); + } - [HttpPost] - public async Task 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(); + [HttpPost] + public async Task 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(); - } + try + { + string filePath = $"/Content/{product.Id}/{figure.Id}.jpg"; + FileInfo figureFile = new FileInfo(environment.WebRootPath + filePath); + if (figureFile.Exists) + { + figureFile.Delete(); + } + + string thumbFilePath = $"/Content/{product.Id}/{figure.Id}_thumb.jpg"; + FileInfo thumbFile = new FileInfo(environment.WebRootPath + thumbFilePath); + if (thumbFile.Exists) + { + thumbFile.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 }); - } - } - } + 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