0
0
MyDarling/Controllers/ProductsController.cs

139 lines
3.2 KiB
C#
Raw Normal View History

2023-06-03 07:41:46 +03:00
using System.Data;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MyDarling.Models;
2023-06-05 07:01:44 +03:00
using MyDarling.Services;
2023-06-03 07:41:46 +03:00
namespace MyDarling.Controllers
{
2023-06-05 07:01:44 +03:00
[Authorize]
public class ProductsController : Controller
{
private DataContext context;
private IWebHostEnvironment environment;
private IImageResizer resizer;
2023-06-03 07:41:46 +03:00
2023-06-05 07:01:44 +03:00
public ProductsController(DataContext context, IImageResizer resizer, IWebHostEnvironment environment)
{
this.environment = environment;
this.context = context;
this.resizer = resizer;
}
2023-06-03 07:41:46 +03:00
2023-06-05 07:01:44 +03:00
public IActionResult Index()
{
return View(context.Products.Include(b => b.Figures));
}
2023-06-03 07:41:46 +03:00
2023-06-05 07:01:44 +03:00
public IActionResult Create()
{
return View();
}
2023-06-03 07:41:46 +03:00
2023-06-05 07:01:44 +03:00
[HttpPost]
public async Task<IActionResult> Create([Bind] Product bundle)
{
try
{
if (ModelState.IsValid)
{
await context.Products.AddAsync(bundle);
context.SaveChanges();
return RedirectToAction(nameof(Index));
}
}
catch (DataException)
{
ModelState.AddModelError("", "Unable to save changes");
}
return View(bundle);
}
2023-06-03 07:41:46 +03:00
2023-06-05 07:01:44 +03:00
public async Task<IActionResult> Details(string id)
{
return View(await context.Products.Include(b => b.Figures).Where(b => b.Id.Equals(id)).FirstOrDefaultAsync());
}
2023-06-03 07:41:46 +03:00
2023-06-05 07:01:44 +03:00
[HttpPost]
public async Task<IActionResult> Edit(string id)
{
if (string.IsNullOrEmpty(id))
{
return NotFound();
}
2023-06-03 07:41:46 +03:00
2023-06-05 07:01:44 +03:00
var product = await context.Products.FindAsync(id);
if (product == null)
{
return NotFound();
}
2023-06-03 07:41:46 +03:00
2023-06-05 07:01:44 +03:00
var file = Request.Form.Files.FirstOrDefault();
string fullPath = string.Empty;
2023-06-03 07:41:46 +03:00
2023-06-05 07:01:44 +03:00
if (await TryUpdateModelAsync<Product>(
product,
"",
b => b.Name, b => b.Description, b => b.Figures, b => b.Price))
{
if (file != null)
{
var newFigure = new Figure(string.Empty, product.Id);
product.Figures.Add(newFigure);
string filePath = $"/Content/{product.Id}/{newFigure.Id}.jpg";
string directoryPath = environment.WebRootPath + "/Content/" + product.Id + "/";
fullPath = environment.WebRootPath + filePath;
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
2023-06-05 17:11:36 +03:00
resizer.WriteResized(file, fullPath);
2023-06-05 07:01:44 +03:00
resizer.CreateThumbnail(fullPath);
}
2023-06-06 09:49:17 +03:00
2023-06-05 07:01:44 +03:00
try
{
await context.SaveChangesAsync();
2023-06-03 07:41:46 +03:00
return RedirectToAction("Details", "Products", new { Id = product?.Id});
2023-06-05 07:01:44 +03:00
}
2023-06-03 07:41:46 +03:00
2023-06-05 07:01:44 +03:00
catch (System.Exception)
{
ModelState.AddModelError("", "Unable to save changes");
}
}
return View(product);
}
2023-06-03 07:41:46 +03:00
2023-06-05 07:01:44 +03:00
[HttpPost]
public async Task<IActionResult> Delete(string id)
{
var productToDelete = await context.Products.FindAsync(id);
if (productToDelete == null)
{
return NotFound();
}
2023-06-03 07:41:46 +03:00
2023-06-05 07:01:44 +03:00
try
{
var bundleDirPath = String.Concat(environment.WebRootPath,
"/Content/",
productToDelete.Id);
2023-06-03 07:41:46 +03:00
2023-06-05 07:01:44 +03:00
if (Directory.Exists(bundleDirPath))
{
Directory.Delete(bundleDirPath, true);
}
context.Products.Remove(productToDelete);
await context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
catch (DbUpdateException)
{
return RedirectToAction(nameof(Delete), new { id = id, saveChangesError = true });
}
}
}
2023-06-03 07:41:46 +03:00
}