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 ++++++++++++++++++++++++++++++++++++++++ Pages/Index.cshtml | 2 +- Pages/Shared/_Navigation.cshtml | 2 +- Pages/Shared/_Products.cshtml | 44 ++++++++++++++ Pages/Shared/_Projects.cshtml | 39 ------------- 5 files changed, 170 insertions(+), 41 deletions(-) create mode 100644 Pages/Figure.cshtml create mode 100644 Pages/Shared/_Products.cshtml delete mode 100644 Pages/Shared/_Projects.cshtml (limited to 'Pages') 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 diff --git a/Pages/Index.cshtml b/Pages/Index.cshtml index a3ec3a5..9a887a6 100644 --- a/Pages/Index.cshtml +++ b/Pages/Index.cshtml @@ -9,5 +9,5 @@ - + \ No newline at end of file diff --git a/Pages/Shared/_Navigation.cshtml b/Pages/Shared/_Navigation.cshtml index 7c29a6b..736fd61 100644 --- a/Pages/Shared/_Navigation.cshtml +++ b/Pages/Shared/_Navigation.cshtml @@ -10,7 +10,7 @@ diff --git a/Pages/Shared/_Products.cshtml b/Pages/Shared/_Products.cshtml new file mode 100644 index 0000000..a82ac1c --- /dev/null +++ b/Pages/Shared/_Products.cshtml @@ -0,0 +1,44 @@ +@using System.Globalization +@using Microsoft.EntityFrameworkCore +@using MyDarling.Models +@inject DataContext context + +@{ + var products = context.Products + .Include(b => b.Figures) + .Where(b => b.Price != 0 && b.Figures.Count > 0) + .OrderByDescending(b => b.Id); +} + +
+
+
+ @foreach (var product in products) + { +
+
+ @{ + var figure = product.Figures.First(); + var filePath = $"/Content/{product.Id}/{figure.Id}.jpg"; + } + @product.Name + @for (int i = 1; i < product.Figures.Count(); i++) + { + filePath = $"/Content/{product.Id}/{product.Figures[i].Id}.jpg"; + + } +
+
+
@product.Name
+ @String.Format(new CultureInfo("ru-RU"), "{0:C0}", product.Price) +
+
+
+
+ } +
+
+
\ No newline at end of file diff --git a/Pages/Shared/_Projects.cshtml b/Pages/Shared/_Projects.cshtml deleted file mode 100644 index 4b9a62e..0000000 --- a/Pages/Shared/_Projects.cshtml +++ /dev/null @@ -1,39 +0,0 @@ -@using System.Globalization -@using Microsoft.EntityFrameworkCore -@using MyDarling.Models -@inject DataContext context - -@{ - var bundles = context.UnderwearBundles - .Include(b => b.Figures) - .Where(b => b.Price != 0 && b.Figures.Count > 0) - .OrderByDescending(b => b.Id); -} - -
-
-
- @foreach (var bundle in bundles) - { -
-
- @bundle.Name - @for (int i = 1; i < @bundle.Figures.Count(); i++) - { - - } -
-
-
@bundle.Name
- @String.Format(new CultureInfo("ru-RU"), "{0:C0}", @bundle.Price) -
-
-
-
- } -
-
-
\ No newline at end of file -- cgit v1.2.3