From 3bade8859bcd938b85c39ab16eaa0dcf8e01535f Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Sat, 3 Jun 2023 07:41:46 +0300 Subject: Mass refactoring --- Pages/Figure.cshtml | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 Pages/Figure.cshtml (limited to 'Pages/Figure.cshtml') diff --git a/Pages/Figure.cshtml b/Pages/Figure.cshtml new file mode 100644 index 0000000..76c2216 --- /dev/null +++ b/Pages/Figure.cshtml @@ -0,0 +1,124 @@ +@* @page "{id}" +@model FigureModel; +@using MyDarling.Models; +@using Microsoft.EntityFrameworkCore; +@using Microsoft.AspNetCore.Mvc.RazorPages + + + + + + Редактирование фотографии + + + + + +
+
+
+
+ + @Model.Figure?.Description + +
+
+ @Html.AntiForgeryToken() +
+ + +
+ +
+
+ +
+
+
+
+ + + + +@functions +{ + public class FigureModel : PageModel + { + private DataContext context; + private IWebHostEnvironment environment; + public string? FilePath { get; set; } + public Figure? Figure { get; set; } + public UnderwearBundle? Bundle { get; set; } + public FigureModel(DataContext context, IWebHostEnvironment environment) + { + this.context = context; + this.environment = environment; + } + + public async Task OnGetAsync(string id) + { + Figure = await context.Figures + .Where(f => f.Id.Equals(id)) + .FirstOrDefaultAsync(); + if (Figure == null) + { + return NotFound(); + } + Bundle = await context.UnderwearBundles + .Where(b => b.Figures.Contains(Figure)) + .FirstOrDefaultAsync(); + if (Bundle == null) + { + return NotFound(); + } + FilePath = $"/Content/{Bundle.Id}/{Figure.Id}.jpg"; + + return Page(); + } + public async Task OnPostAsync(string id, string description) + { + Figure = await context.Figures + .Where(f => f.Id.Equals(id)) + .FirstOrDefaultAsync(); + if (Figure != null) + { + Figure.Description = description ?? string.Empty; + } + await context.SaveChangesAsync(); + return RedirectToPage(); + } + + public async Task OnPostDeleteAsync(string id) + { + Figure = await context.Figures.FindAsync(id); + if (Figure == null) + { + return NotFound(); + } + var parentBundle = await context.UnderwearBundles + .Where(b => b.Figures.Contains(Figure)) + .FirstAsync(); + + try + { + string filePath = $"/Content/{parentBundle.Id}/{Figure}.jpg"; + FileInfo figureFile = new FileInfo(environment.WebRootPath + filePath); + if (figureFile.Exists) + { + figureFile.Delete(); + } + + context.Figures.Remove(Figure); + await context.SaveChangesAsync(); + return RedirectToPage($"/Bundle/{Bundle.Id}"); + } + catch (DbUpdateException) + { + return RedirectToPage($"/Bundle/{Bundle.Id}"); + } + } + } +} *@ \ No newline at end of file -- cgit v1.2.3