diff options
Diffstat (limited to 'Pages/Figure.cshtml')
-rw-r--r-- | Pages/Figure.cshtml | 124 |
1 files changed, 124 insertions, 0 deletions
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 + +<!DOCTYPE html> +<html> + +<head> + <title>Редактирование фотографии</title> + <link href="/lib/bootstrap/css/bootstrap.min.css" rel="stylesheet" /> +</head> + +<body> + <nav class="navbar navbar-dark bg-primary"> + <a class="navbar-brand mx-4" href="/Bundle"><img height="30" src="/assets/img/logo.svg"></a> + <a href="/Account/logout"><button class="btn btn-outline-light mx-4">Выйти</button></a> + </nav> + <div class="container"> + <div class="row row-cols-xl-3 row-cols-md-2 justify-content-center mt-3"> + <div class="col"> + <div class="thumbnail"> + <a href="@Model.FilePath"> + <img src="@Model.FilePath" class="img-thumbnail img-fluid" alt="@Model.Figure?.Description"> + </a> + </div> + <form method="post" class="m-2"> + @Html.AntiForgeryToken() + <div class="form-group"> + <label class="form-label">Описание:</label> + <input name="description" class="form-control" value="@Model.Figure?.Description" /> + </div> + <button type="submit" class="btn btn-outline-success mt-2">Сохранить</button> + </form> + <form asp-page-handler="Delete" method="post"> + <button type="submit" class="btn btn-outline-danger mt-2">Удалить</button> + </form> + </div> + </div> + </div> +</body> + +</html> + +@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<IActionResult> 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<IActionResult> 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<IActionResult> 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 |