101 lines
3.0 KiB
C#
101 lines
3.0 KiB
C#
namespace GrossesMitainesAPI.Controllers;
|
|
|
|
#region Dependencies
|
|
using GrossesMitainesAPI.Models;
|
|
using System.Linq;
|
|
using GrossesMitainesAPI.Data;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.AspNetCore.Cors;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using GrossesMitainesAPI.Services;
|
|
|
|
#endregion
|
|
|
|
/// <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>
|
|
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]")]
|
|
public class ProductController : ControllerBase {
|
|
#region DI Fields
|
|
private readonly ILogger<ProductController> _logger;
|
|
private readonly InventoryContext _context;
|
|
private readonly DatabaseCacheService _cache;
|
|
|
|
#endregion
|
|
|
|
#region Ctor
|
|
public ProductController(ILogger<ProductController> logger, InventoryContext context, DatabaseCacheService cache) {
|
|
_logger = logger;
|
|
_context = context;
|
|
_cache = cache;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region API Methods
|
|
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Product"), AllowAnonymous]
|
|
public ActionResult<ProductViewModel> Get(int id) {
|
|
Product prod;
|
|
try {
|
|
prod = _context.Products.Where(x => x.Id == id).First();
|
|
}
|
|
catch (Exception e) {
|
|
_logger.LogError(8, e.Message);
|
|
return NotFound();
|
|
}
|
|
_cache.addHit((uint)id);
|
|
return new ProductViewModel(prod);
|
|
}
|
|
|
|
[EnableCors("_myAllowSpecificOrigins"), HttpPost(Name = "Product")]
|
|
public ActionResult<Product> Post(Product prod) {
|
|
if (prod.Price <= prod.PromoPrice)
|
|
prod.PromoPrice = prod.Price - 0.01M;
|
|
try {
|
|
_context.Products.Add(prod);
|
|
_context.SaveChanges();
|
|
}
|
|
catch (Exception e) {
|
|
_logger.LogError(8, e.Message);
|
|
return BadRequest(e.Message);
|
|
}
|
|
_cache.askForRefresh();
|
|
return prod;
|
|
}
|
|
|
|
[EnableCors("_myAllowSpecificOrigins"), HttpPatch(Name = "Product")]
|
|
public ActionResult<Product> Patch(Product prod) {
|
|
try {
|
|
_context.Products.Update(prod);
|
|
_context.SaveChanges();
|
|
}
|
|
catch (Exception e) {
|
|
_logger.LogError(8, e.Message);
|
|
return BadRequest(e.Message);
|
|
}
|
|
_cache.askForRefresh();
|
|
return prod;
|
|
}
|
|
|
|
[EnableCors("_myAllowSpecificOrigins"), HttpDelete(Name = "Product")]
|
|
public ActionResult<int> DeleteProduct(int id) {
|
|
try {
|
|
_context.Products.Remove(_context.Products.Where(x => x.Id == id).First());
|
|
_context.SaveChanges();
|
|
}
|
|
catch (Exception e) {
|
|
_logger.LogError(8, e.Message);
|
|
return BadRequest(e.Message);
|
|
}
|
|
_cache.askForRefresh();
|
|
return id;
|
|
}
|
|
|
|
#endregion
|
|
} |