2023-02-22 07:56:30 +03:00
|
|
|
using System.Data;
|
2023-03-06 07:41:35 +03:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-02-22 07:56:30 +03:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using MyDarling.Models;
|
|
|
|
|
|
|
|
namespace MyDarling.Controllers
|
|
|
|
{
|
2023-03-06 07:41:35 +03:00
|
|
|
[Authorize]
|
2023-02-22 07:56:30 +03:00
|
|
|
public class FigureController : Controller
|
|
|
|
{
|
|
|
|
private DataContext context;
|
2023-03-02 08:02:02 +03:00
|
|
|
private IWebHostEnvironment environment;
|
|
|
|
public FigureController(DataContext context, IWebHostEnvironment environment)
|
2023-02-22 07:56:30 +03:00
|
|
|
{
|
|
|
|
this.context = context;
|
2023-03-02 08:02:02 +03:00
|
|
|
this.environment = environment;
|
2023-02-22 07:56:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<IActionResult> Details(int id)
|
|
|
|
{
|
|
|
|
return View(await context.Figures.Where(f => f.Id == id).FirstOrDefaultAsync());
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
public async Task<IActionResult> Edit(int? id)
|
|
|
|
{
|
|
|
|
if (id == null)
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
var figure = await context.Figures.FindAsync(id);
|
|
|
|
|
|
|
|
if (figure == null)
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
var bundle = await context.UnderwearBundles
|
|
|
|
.Where(b => b.Figures.Contains(figure))
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
|
|
|
|
if (await TryUpdateModelAsync<Figure>(
|
|
|
|
figure,
|
|
|
|
"",
|
|
|
|
f => f.Description))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
return RedirectToAction("Details", "Bundle", new { Id = bundle?.Id});
|
|
|
|
}
|
|
|
|
catch (SystemException)
|
|
|
|
{
|
|
|
|
ModelState.AddModelError("", "Unable to save changes");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return View(figure);
|
|
|
|
}
|
2023-03-02 07:28:22 +03:00
|
|
|
|
|
|
|
[HttpPost]
|
2023-03-02 08:02:02 +03:00
|
|
|
public async Task<ActionResult> Delete(int id)
|
|
|
|
{
|
|
|
|
var figureToDelete = await context.Figures.FindAsync(id);
|
|
|
|
if (figureToDelete == null)
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
2023-03-02 07:28:22 +03:00
|
|
|
|
2023-03-02 08:02:02 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
FileInfo figureFile = new FileInfo(environment.WebRootPath + figureToDelete.FilePath);
|
|
|
|
if (figureFile.Exists)
|
|
|
|
{
|
|
|
|
figureFile.Delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
context.Figures.Remove(figureToDelete);
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
return RedirectToAction(nameof(Index), "Bundle");
|
|
|
|
}
|
|
|
|
catch (DbUpdateException)
|
|
|
|
{
|
|
|
|
return RedirectToAction(nameof(Delete), new { id = id, saveChangesError = true });
|
|
|
|
}
|
|
|
|
}
|
2023-02-22 07:56:30 +03:00
|
|
|
}
|
|
|
|
}
|