@* @page "{id}"
@model FigureModel;
@using MyDarling.Models;
@using Microsoft.EntityFrameworkCore;
@using Microsoft.AspNetCore.Mvc.RazorPages
Редактирование фотографии
@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}");
}
}
}
} *@