GGMM/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs

170 lines
6.3 KiB
C#
Raw Normal View History

2022-10-30 12:17:10 -04:00
namespace GrossesMitainesAPI.Controllers;
#region Dependencies
using Microsoft.AspNetCore.Mvc;
2022-10-08 13:22:12 -04:00
using GrossesMitainesAPI.Models;
using System.Linq;
using GrossesMitainesAPI.Data;
using Microsoft.Extensions.Logging;
2022-10-18 10:08:23 -04:00
using Microsoft.AspNetCore.Cors;
using GrossesMitainesAPI.Services;
2022-10-27 12:37:13 -04:00
using Microsoft.AspNetCore.Authorization;
2022-10-08 13:22:12 -04:00
2022-10-30 12:17:10 -04:00
#endregion
2022-10-08 13:22:12 -04:00
2022-10-30 16:58:47 -04:00
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"),
Authorize(AuthenticationSchemes = "Identity.Application")]
2022-10-08 13:22:12 -04:00
public class InventoryController : Controller {
2022-10-30 12:17:10 -04:00
#region Constants
2022-10-31 10:12:25 -04:00
private const int AMOUNT_SCROLL = 6;
2022-10-30 12:17:10 -04:00
#endregion
#region DI Fields
2022-10-09 15:07:35 -04:00
private readonly ILogger<InventoryController> _logger;
2022-10-08 13:22:12 -04:00
private readonly InventoryContext _context;
private readonly DatabaseCacheService _cache;
2022-10-25 11:57:41 -04:00
2022-10-30 12:17:10 -04:00
#endregion
#region Ctor
public InventoryController(ILogger<InventoryController> logger, InventoryContext context, DatabaseCacheService cache) {
2022-10-08 13:22:12 -04:00
_context = context;
2022-10-09 15:07:35 -04:00
_logger = logger;
_cache = cache;
2022-10-08 13:22:12 -04:00
}
2022-10-30 12:17:10 -04:00
#endregion
#region API Methods
2022-10-27 12:37:13 -04:00
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Inventory"), AllowAnonymous] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant)
2022-10-27 14:32:06 -04:00
public IEnumerable<ProductViewModel> Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) {
bool iscache = false;
2022-10-30 12:17:10 -04:00
IQueryable<ProductViewModel> ret;
2022-10-25 10:54:11 -04:00
if (_cache.isOk()) {
2022-10-30 12:17:10 -04:00
ret = _cache.queryCache().Select(x => new ProductViewModel(x));
2022-10-27 14:32:06 -04:00
iscache = true;
2022-10-25 10:54:11 -04:00
}
2022-10-30 12:17:10 -04:00
else ret = _context.Products.AsQueryable().Select(x => new ProductViewModel(x));
2022-10-16 11:25:21 -04:00
switch (filterPrice) {
2022-10-16 10:33:16 -04:00
case "PriceUnder20":
ret = ret.Where(x => x.Price < 20);
break;
case "Price20to49":
ret = ret.Where(x => x.Price >= 20 && x.Price < 50);
break;
case "Price50to99":
ret = ret.Where(x => x.Price >= 50 && x.Price < 100);
break;
case "PriceOver100":
ret = ret.Where(x => x.Price >= 100);
break;
2022-10-16 11:25:21 -04:00
default: break;
}
switch (filterState) {
2022-10-16 10:33:16 -04:00
case "isAvailable":
2022-10-16 11:25:21 -04:00
ret = ret.Where(x => x.Status == Product.States.Available);
break;
case "isUnavailable":
ret = ret.Where(x => x.Status == Product.States.Unavailable);
2022-10-16 10:33:16 -04:00
break;
case "isBackOrder":
2022-10-16 11:25:21 -04:00
ret = ret.Where(x => x.Status == Product.States.BackOrder);
2022-10-16 10:33:16 -04:00
break; ;
case "isClearance":
2022-10-16 11:25:21 -04:00
ret = ret.Where(x => x.Status == Product.States.Clearance);
2022-10-16 10:33:16 -04:00
break;
case "isDiscontinued":
2022-10-16 11:25:21 -04:00
ret = ret.Where(x => x.Status == Product.States.Discontinued);
2022-10-16 10:33:16 -04:00
break;
case "isPromoted":
2022-10-30 12:17:10 -04:00
ret = ret.Where(x => x.Status == Product.States.Clearance ||
x.Status == Product.States.Promotion);
break;
2022-10-16 10:45:18 -04:00
default: break;
2022-10-16 10:33:16 -04:00
}
switch (order) {
case "Price":
2022-10-30 12:17:10 -04:00
ret = ret.OrderBy(x => x.Status == Product.States.Promotion ||
x.Status == Product.States.Clearance ?
x.PromoPrice : x.Price);
2022-10-16 10:33:16 -04:00
break;
case "PriceDesc":
2022-10-30 12:17:10 -04:00
ret = ret.OrderByDescending(x => x.Status == Product.States.Promotion ||
x.Status == Product.States.Clearance ?
x.PromoPrice : x.Price);
2022-10-16 10:33:16 -04:00
break;
case "Title":
2022-10-16 10:45:18 -04:00
ret = ret.OrderBy(x => x.Title);
2022-10-16 10:33:16 -04:00
break;
case "TitleDesc":
2022-10-16 10:45:18 -04:00
ret = ret.OrderByDescending(x => x.Title);
2022-10-16 10:33:16 -04:00
break;
case "Category":
2022-10-16 10:45:18 -04:00
ret = ret.OrderBy(x => x.Category);
2022-10-16 10:33:16 -04:00
break;
case "CategoryDesc":
2022-10-16 10:45:18 -04:00
ret = ret.OrderByDescending(x => x.Category);
2022-10-16 10:33:16 -04:00
break;
2022-10-16 10:45:18 -04:00
default: break;
2022-10-16 10:33:16 -04:00
}
2022-10-27 14:32:06 -04:00
List<ProductViewModel> lst = new();
2022-10-18 10:43:52 -04:00
bool yup = false;
int add = 0;
2022-10-25 10:54:11 -04:00
try {
2022-10-27 14:32:06 -04:00
if (!lastId.HasValue || lastId == 0)
yup = true;
2022-10-30 12:17:10 -04:00
foreach (ProductViewModel prod in ret.ToList()) {
2022-10-27 14:32:06 -04:00
if (yup && add < AMOUNT_SCROLL || (all.HasValue && all == true)) {
2022-10-30 12:17:10 -04:00
lst.Add(prod);
2022-10-27 14:32:06 -04:00
add++;
2022-10-18 10:43:52 -04:00
}
2022-10-27 14:32:06 -04:00
if (!yup && prod.Id == lastId)
yup = true;
}
2022-10-25 10:54:11 -04:00
return lst;
} catch (Exception e) {
2022-10-27 14:32:06 -04:00
if (iscache)
2022-10-25 10:54:11 -04:00
_logger.LogError(e, "Erreur d'appel de cache.");
else _logger.LogError(e, "Erreur d'appel d'API.");
2022-10-27 14:32:06 -04:00
return new List<ProductViewModel>();
2022-10-25 10:54:11 -04:00
}
2022-10-08 13:22:12 -04:00
}
2022-10-21 18:07:11 -04:00
// Inventory/Delete => Décrémenter un produit. Va aller chercher directement dans la BD.
2022-10-27 12:37:13 -04:00
[EnableCors("_myAllowSpecificOrigins"), HttpDelete(Name = "Inventory"), AllowAnonymous]
2022-10-18 14:53:57 -04:00
public ActionResult<int> Delete(int? id) {
int rid = 0;
2022-10-09 15:07:35 -04:00
if (!id.HasValue) {
2022-10-31 10:12:25 -04:00
_logger.LogError(8, "Tentative de vente sans Id.");
2022-10-18 14:53:57 -04:00
return BadRequest();
2022-10-09 15:07:35 -04:00
}
try {
Product prod = _context.Products.First(x => x.Id == id);
2022-10-18 14:53:57 -04:00
rid = prod.Id;
2022-10-27 14:32:06 -04:00
if (prod.Quantity > 0) {
2022-10-16 11:25:21 -04:00
prod.Quantity = prod.Quantity - 1;
2022-10-27 12:37:13 -04:00
prod.Sales = prod.Sales + 1;
prod.LastSale = DateTime.Now;
2022-10-16 11:25:21 -04:00
if (prod.Quantity == 0)
2022-10-27 14:32:06 -04:00
prod.Status = prod.Status == Product.States.Clearance ?
Product.States.Discontinued :
2022-10-18 14:48:34 -04:00
Product.States.BackOrder;
2022-10-27 14:32:06 -04:00
} else {
2022-10-31 10:12:25 -04:00
_logger.LogError(8, $"Vente de produit pas en stock. Id Produit: {prod.Id}");
2022-10-18 14:53:57 -04:00
return BadRequest();
2022-10-09 15:07:35 -04:00
}
_context.Products.Update(prod);
2022-10-16 11:25:21 -04:00
_context.SaveChanges();
2022-10-09 15:07:35 -04:00
} catch (Exception e) {
_logger.LogError(8, e.Message);
2022-10-18 14:53:57 -04:00
return BadRequest();
2022-10-09 15:07:35 -04:00
}
2022-10-21 18:07:11 -04:00
_cache.askForRefresh();
2022-10-18 14:53:57 -04:00
return rid;
2022-10-09 15:07:35 -04:00
}
2022-10-30 12:17:10 -04:00
#endregion
2022-10-08 13:22:12 -04:00
}