GGMM/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs

102 lines
3.1 KiB
C#
Raw Normal View History

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;
using Microsoft.AspNetCore.Mvc;
using GrossesMitainesAPI.Services;
2022-10-08 15:04:29 -04:00
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-30 16:58:47 -04:00
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"),
Authorize(AuthenticationSchemes = "Identity.Application")]
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;
private readonly DatabaseCacheService _cache;
2022-10-08 15:04:29 -04:00
2022-10-30 12:17:10 -04:00
#endregion
#region Ctor
public ProductController(ILogger<ProductController> logger, InventoryContext context, DatabaseCacheService cache) {
2022-10-08 15:04:29 -04:00
_logger = logger;
_context = context;
_cache = cache;
2022-10-08 15:04:29 -04:00
}
2022-10-30 12:17:10 -04:00
#endregion
#region API Methods
2022-10-25 12:02:27 -04:00
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Product"), AllowAnonymous]
2022-10-27 12:37:13 -04:00
public ActionResult<ProductViewModel> Get(int id) {
2022-10-08 15:04:29 -04:00
Product prod;
try {
prod = _context.Products.Where(x => x.Id == id).First();
}
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-10-25 12:02:27 -04:00
[EnableCors("_myAllowSpecificOrigins"), HttpPost(Name = "Product")]
public ActionResult<Product> Post(Product 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 {
_context.Products.Add(prod);
2022-10-16 11:25:21 -04:00
_context.SaveChanges();
}
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();
return prod;
2022-10-08 15:04:29 -04:00
}
2022-10-08 16:05:23 -04:00
2022-10-25 12:02:27 -04:00
[EnableCors("_myAllowSpecificOrigins"), HttpPatch(Name = "Product")]
2022-10-18 12:19:56 -04:00
public ActionResult<Product> Patch(Product prod) {
try {
_context.Products.Update(prod);
_context.SaveChanges();
}
catch (Exception e) {
_logger.LogError(8, e.Message);
2022-10-18 11:55:49 -04:00
return BadRequest(e.Message);
}
2022-10-21 17:58:12 -04:00
_cache.askForRefresh();
return prod;
2022-10-17 17:33:45 -04:00
}
2022-10-25 12:02:27 -04:00
[EnableCors("_myAllowSpecificOrigins"), HttpDelete(Name = "Product")]
2022-10-18 11:55:49 -04:00
public ActionResult<int> DeleteProduct(int id) {
2022-10-18 11:01:16 -04:00
try {
_context.Products.Remove(_context.Products.Where(x => x.Id == id).First());
_context.SaveChanges();
}
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-17 17:33:45 -04:00
}