0
0
MyDarling/Controllers/FigureController.cs

116 lines
2.6 KiB
C#
Raw Normal View History

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-06-05 07:15:14 +03:00
[Authorize]
public class FigureController : Controller
{
private DataContext context;
private IWebHostEnvironment environment;
public FigureController(DataContext context, IWebHostEnvironment environment)
{
this.context = context;
this.environment = environment;
}
2023-02-22 07:56:30 +03:00
2023-06-05 07:15:14 +03:00
public async Task<IActionResult> Details(string id)
{
var figure = await context.Figures
.Where(f => f.Id.Equals(id))
.FirstOrDefaultAsync();
if (figure == null)
{
return NotFound();
}
var product = await context.Products
.Where(b => b.Figures.Contains(figure))
.FirstOrDefaultAsync();
if (product == null)
{
return NotFound();
}
return View(figure);
}
2023-02-22 07:56:30 +03:00
2023-06-05 07:15:14 +03:00
[HttpPost]
public async Task<IActionResult> Edit(string id)
{
if (id == null)
{
return NotFound();
}
2023-02-22 07:56:30 +03:00
2023-06-05 07:15:14 +03:00
var figure = await context.Figures
.Where(f => f.Id.Equals(id))
.FirstOrDefaultAsync();
2023-02-22 07:56:30 +03:00
2023-06-05 07:15:14 +03:00
if (figure == null)
{
return NotFound();
}
2023-02-22 07:56:30 +03:00
2023-06-05 07:15:14 +03:00
var product = await context.Products
.Where(b => b.Figures.Contains(figure))
.FirstOrDefaultAsync();
2023-02-22 07:56:30 +03:00
2023-06-05 07:15:14 +03:00
if (await TryUpdateModelAsync<Figure>(
figure,
"",
f => f.Description))
{
try
{
await context.SaveChangesAsync();
return RedirectToAction("Details", "Products", new { Id = product?.Id });
}
catch (SystemException)
{
ModelState.AddModelError("", "Unable to save changes");
}
}
return View(figure);
}
2023-03-02 07:28:22 +03:00
2023-06-05 07:15:14 +03:00
[HttpPost]
public async Task<ActionResult> Delete(string id)
{
var figure = await context.Figures.FindAsync(id);
if (figure == null)
{
return NotFound();
}
var product = await context.Products
.Where(b => b.Figures.Contains(figure))
.FirstAsync();
2023-06-03 07:41:46 +03:00
2023-06-05 07:15:14 +03:00
try
{
string filePath = $"/Content/{product.Id}/{figure.Id}.jpg";
FileInfo figureFile = new FileInfo(environment.WebRootPath + filePath);
if (figureFile.Exists)
{
figureFile.Delete();
}
string thumbFilePath = $"/Content/{product.Id}/{figure.Id}_thumb.jpg";
FileInfo thumbFile = new FileInfo(environment.WebRootPath + thumbFilePath);
if (thumbFile.Exists)
{
thumbFile.Delete();
}
2023-06-03 07:41:46 +03:00
2023-06-05 07:15:14 +03:00
context.Figures.Remove(figure);
await context.SaveChangesAsync();
return RedirectToAction("Details", "Products", new { Id = product?.Id });
}
catch (DbUpdateException)
{
return RedirectToAction(nameof(Delete), new { id = id, saveChangesError = true });
}
}
}
2023-02-22 07:56:30 +03:00
}