2022-10-30 12:17:10 -04:00
|
|
|
|
namespace GrossesMitainesAPI.Controllers;
|
|
|
|
|
|
|
|
|
|
#region Dependencies
|
|
|
|
|
using GrossesMitainesAPI.Models;
|
2022-10-08 15:04:29 -04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using GrossesMitainesAPI.Data;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2022-10-16 10:45:18 -04:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2022-10-17 17:33:45 -04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2022-10-18 10:08:23 -04:00
|
|
|
|
using Microsoft.AspNetCore.Cors;
|
2022-10-18 11:50:06 -04:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2022-10-21 17:52:25 -04:00
|
|
|
|
using GrossesMitainesAPI.Services;
|
2022-11-01 18:50:32 -04:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Storage;
|
2022-11-05 12:01:05 -04:00
|
|
|
|
using Microsoft.AspNetCore.Routing;
|
2022-10-30 12:17:10 -04:00
|
|
|
|
#endregion
|
|
|
|
|
|
2022-10-21 17:58:12 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ce contrôleur ne va pas chercher dans la cache,
|
|
|
|
|
/// mais les changements dans celui-ci entrainera
|
|
|
|
|
/// une demande de mise à jour dans cette dernière
|
|
|
|
|
/// qui sera effectuée dans les 10 secondes après
|
|
|
|
|
/// l'éxécution d'une modification de la BD.
|
|
|
|
|
/// </summary>
|
2022-10-31 15:11:18 -04:00
|
|
|
|
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"),
|
2022-11-01 14:07:49 -04:00
|
|
|
|
Authorize(AuthenticationSchemes = "Identity.Application", Roles = "Administrateur")]
|
2022-10-18 11:50:06 -04:00
|
|
|
|
public class ProductController : ControllerBase {
|
2022-10-30 12:17:10 -04:00
|
|
|
|
#region DI Fields
|
2022-10-08 15:04:29 -04:00
|
|
|
|
private readonly ILogger<ProductController> _logger;
|
|
|
|
|
private readonly InventoryContext _context;
|
2022-10-21 17:52:25 -04:00
|
|
|
|
private readonly DatabaseCacheService _cache;
|
2022-10-31 15:11:18 -04:00
|
|
|
|
private readonly IWebHostEnvironment _hostEnvironment;
|
2022-10-08 15:04:29 -04:00
|
|
|
|
|
2022-10-30 12:17:10 -04:00
|
|
|
|
#endregion
|
2022-11-02 12:40:09 -04:00
|
|
|
|
|
2022-10-30 12:17:10 -04:00
|
|
|
|
#region Ctor
|
2022-10-31 15:11:18 -04:00
|
|
|
|
public ProductController(ILogger<ProductController> logger, InventoryContext context, DatabaseCacheService cache, IWebHostEnvironment hostEnvironment) {
|
2022-10-08 15:04:29 -04:00
|
|
|
|
_logger = logger;
|
|
|
|
|
_context = context;
|
2022-10-21 17:52:25 -04:00
|
|
|
|
_cache = cache;
|
2022-10-31 15:11:18 -04:00
|
|
|
|
_hostEnvironment = hostEnvironment;
|
2022-10-08 15:04:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-30 12:17:10 -04:00
|
|
|
|
#endregion
|
2022-11-05 10:55:09 -04:00
|
|
|
|
|
2022-10-30 12:17:10 -04:00
|
|
|
|
#region API Methods
|
2022-11-05 12:01:05 -04:00
|
|
|
|
[EnableCors("_myAllowSpecificOrigins"), HttpGet("Quantity"), AllowAnonymous]
|
2022-11-05 10:55:09 -04:00
|
|
|
|
public ActionResult<uint> ProdCount(int id) {
|
|
|
|
|
return _context.Products.FirstOrDefault(x => x.Id == id).Quantity;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-05 12:01:05 -04:00
|
|
|
|
[EnableCors("_myAllowSpecificOrigins"), HttpGet(), AllowAnonymous]
|
2022-10-27 12:37:13 -04:00
|
|
|
|
public ActionResult<ProductViewModel> Get(int id) {
|
2022-11-01 13:33:08 -04:00
|
|
|
|
ProductModel prod;
|
2022-10-08 15:04:29 -04:00
|
|
|
|
try {
|
|
|
|
|
prod = _context.Products.Where(x => x.Id == id).First();
|
2022-10-18 11:50:06 -04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
2022-10-08 15:04:29 -04:00
|
|
|
|
_logger.LogError(8, e.Message);
|
2022-10-18 11:55:49 -04:00
|
|
|
|
return NotFound();
|
2022-10-08 15:04:29 -04:00
|
|
|
|
}
|
2022-10-27 12:37:13 -04:00
|
|
|
|
_cache.addHit((uint)id);
|
|
|
|
|
return new ProductViewModel(prod);
|
2022-10-08 15:04:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-05 12:01:05 -04:00
|
|
|
|
[EnableCors("_myAllowSpecificOrigins"), HttpPost()]
|
2022-11-01 18:50:32 -04:00
|
|
|
|
public async Task<ActionResult<ProductModel>> Post([FromForm] ProductModel prod) {
|
2022-10-16 20:33:58 -04:00
|
|
|
|
if (prod.Price <= prod.PromoPrice)
|
|
|
|
|
prod.PromoPrice = prod.Price - 0.01M;
|
2022-10-08 15:04:29 -04:00
|
|
|
|
try {
|
2022-10-31 15:11:18 -04:00
|
|
|
|
if (prod.ImageFile != null)
|
|
|
|
|
prod.ImageName = await SaveImage(prod.ImageFile);
|
|
|
|
|
|
2022-10-08 15:04:29 -04:00
|
|
|
|
_context.Products.Add(prod);
|
2022-10-16 11:25:21 -04:00
|
|
|
|
_context.SaveChanges();
|
2022-10-18 11:50:06 -04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
2022-10-08 15:04:29 -04:00
|
|
|
|
_logger.LogError(8, e.Message);
|
2022-10-18 11:55:49 -04:00
|
|
|
|
return BadRequest(e.Message);
|
2022-10-08 15:04:29 -04:00
|
|
|
|
}
|
2022-10-21 17:58:12 -04:00
|
|
|
|
_cache.askForRefresh();
|
2022-10-18 11:50:06 -04:00
|
|
|
|
return prod;
|
2022-10-08 15:04:29 -04:00
|
|
|
|
}
|
2022-10-08 16:05:23 -04:00
|
|
|
|
|
2022-11-05 12:01:05 -04:00
|
|
|
|
[EnableCors("_myAllowSpecificOrigins"), HttpPatch()]
|
2022-11-05 10:55:09 -04:00
|
|
|
|
public async Task<ActionResult<ProductModel>> Patch([FromForm] ProductModel prod) {
|
|
|
|
|
string? oldImage = "";
|
2022-11-05 13:41:45 -04:00
|
|
|
|
if (prod.Price <= prod.PromoPrice)
|
|
|
|
|
prod.PromoPrice = prod.Price - 0.01M;
|
2022-10-18 11:50:06 -04:00
|
|
|
|
try {
|
2022-11-05 13:41:45 -04:00
|
|
|
|
if (prod.ImageFile is not null) {
|
|
|
|
|
oldImage = prod.ImageName;
|
2022-11-05 10:55:09 -04:00
|
|
|
|
prod.ImageName = await SaveImage(prod.ImageFile);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 11:50:06 -04:00
|
|
|
|
_context.Products.Update(prod);
|
|
|
|
|
_context.SaveChanges();
|
2022-11-05 10:55:09 -04:00
|
|
|
|
if (oldImage is not null and not "")
|
|
|
|
|
DeleteImages(oldImage);
|
2022-10-18 11:50:06 -04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
_logger.LogError(8, e.Message);
|
2022-10-18 11:55:49 -04:00
|
|
|
|
return BadRequest(e.Message);
|
2022-10-18 11:50:06 -04:00
|
|
|
|
}
|
2022-10-21 17:58:12 -04:00
|
|
|
|
_cache.askForRefresh();
|
2022-10-18 11:50:06 -04:00
|
|
|
|
return prod;
|
2022-10-17 17:33:45 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-05 12:01:05 -04:00
|
|
|
|
[EnableCors("_myAllowSpecificOrigins"), HttpDelete()]
|
2022-11-05 10:55:09 -04:00
|
|
|
|
public ActionResult<int> Delete(int id) {
|
2022-10-18 11:01:16 -04:00
|
|
|
|
try {
|
2022-11-05 10:55:09 -04:00
|
|
|
|
var prod = _context.Products.Where(x => x.Id == id).First();
|
|
|
|
|
string imageName = prod.ImageName;
|
|
|
|
|
_context.Products.Remove(prod);
|
2022-10-18 11:01:16 -04:00
|
|
|
|
_context.SaveChanges();
|
2022-11-05 10:55:09 -04:00
|
|
|
|
DeleteImages(imageName);
|
2022-10-18 11:50:06 -04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
2022-10-18 11:01:16 -04:00
|
|
|
|
_logger.LogError(8, e.Message);
|
2022-10-18 11:55:49 -04:00
|
|
|
|
return BadRequest(e.Message);
|
2022-10-18 11:01:16 -04:00
|
|
|
|
}
|
2022-10-21 17:58:12 -04:00
|
|
|
|
_cache.askForRefresh();
|
2022-10-18 11:55:49 -04:00
|
|
|
|
return id;
|
2022-10-18 11:01:16 -04:00
|
|
|
|
}
|
2022-10-30 12:17:10 -04:00
|
|
|
|
|
|
|
|
|
#endregion
|
2022-10-31 15:11:18 -04:00
|
|
|
|
|
2022-11-03 12:45:59 -04:00
|
|
|
|
#region Internal Methods
|
2022-10-31 15:11:18 -04:00
|
|
|
|
private async Task<string> SaveImage(IFormFile imageFile) {
|
2022-11-07 19:53:38 -05:00
|
|
|
|
string imageName = new String(Path.GetFileNameWithoutExtension(imageFile.FileName).Take(10).ToArray()).Replace(' ', '-').Replace("$","");
|
2022-10-31 15:11:18 -04:00
|
|
|
|
imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(imageFile.FileName);
|
|
|
|
|
var imagePath = Path.Combine(_hostEnvironment.ContentRootPath, "Images", imageName);
|
2022-11-01 18:50:32 -04:00
|
|
|
|
|
2022-10-31 15:11:18 -04:00
|
|
|
|
using (var fileStream = new FileStream(imagePath, FileMode.Create)) {
|
|
|
|
|
await imageFile.CopyToAsync(fileStream);
|
2022-11-01 18:50:32 -04:00
|
|
|
|
SaveImageThumbnail(fileStream, imageName);
|
2022-10-31 15:11:18 -04:00
|
|
|
|
}
|
|
|
|
|
return imageName;
|
|
|
|
|
}
|
2022-11-01 18:50:32 -04:00
|
|
|
|
|
|
|
|
|
private void SaveImageThumbnail(FileStream stream, string imageName) {
|
|
|
|
|
try {
|
2022-11-07 19:53:38 -05:00
|
|
|
|
const float maxSize = 300f;
|
2022-11-01 18:50:32 -04:00
|
|
|
|
Image image = Image.FromStream(stream);
|
|
|
|
|
|
2022-11-07 19:53:38 -05:00
|
|
|
|
//Choisi le bon ratio de division pour ne pas dépasser le 300px ni dans height ni dans width
|
2022-11-01 18:50:32 -04:00
|
|
|
|
float ratio = image.Width / (image.Height / maxSize) <= maxSize ? image.Height / maxSize : image.Width / maxSize;
|
|
|
|
|
|
|
|
|
|
Bitmap resize = new Bitmap(image, new Size((int)(image.Width / ratio), (int)(image.Height / ratio)));
|
|
|
|
|
string path = Path.Combine(_hostEnvironment.ContentRootPath, "Images", Path.GetFileNameWithoutExtension(imageName) + "_thumbnail" + Path.GetExtension(imageName));
|
|
|
|
|
resize.Save(path);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
_logger.LogError(8, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-05 10:55:09 -04:00
|
|
|
|
private void DeleteImages(string imageName) {
|
2022-11-05 13:41:45 -04:00
|
|
|
|
|
2022-11-07 19:53:38 -05:00
|
|
|
|
if (imageName == "default.jpg" || imageName == "default_thumbnail.jpg" || imageName.Contains("$"))
|
2022-11-05 13:41:45 -04:00
|
|
|
|
return;
|
|
|
|
|
|
2022-11-05 10:55:09 -04:00
|
|
|
|
var files = System.IO.Directory.GetFiles(_hostEnvironment.ContentRootPath + "/Images")
|
2022-11-05 13:41:45 -04:00
|
|
|
|
.Where(x => x.Contains(Path.GetFileNameWithoutExtension(imageName))).ToArray();
|
2022-11-05 10:55:09 -04:00
|
|
|
|
|
|
|
|
|
foreach (var file in files)
|
2022-11-05 13:41:45 -04:00
|
|
|
|
System.IO.File.Delete(file);
|
2022-11-05 10:55:09 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-31 15:11:18 -04:00
|
|
|
|
#endregion
|
2022-10-17 17:33:45 -04:00
|
|
|
|
}
|