GGMM/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs

159 lines
6.0 KiB
C#
Raw Normal View History

2022-10-08 13:22:12 -04:00
using Microsoft.AspNetCore.Mvc;
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-08 13:22:12 -04:00
namespace GrossesMitainesAPI.Controllers;
2022-10-25 12:02:27 -04:00
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]")]
2022-10-08 13:22:12 -04:00
public class InventoryController : Controller {
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 10:54:11 -04:00
private static object _lock = new object();
2022-10-25 11:57:41 -04:00
private const int AMOUNT_SCROLL = 5;
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-25 12:02:27 -04:00
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Inventory")] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant)
2022-10-18 13:25:08 -04:00
public IEnumerable<Product> Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) {
2022-10-25 10:54:11 -04:00
bool islock = false;
IQueryable<Product> ret;
2022-10-25 10:54:11 -04:00
if (_cache.isOk()) {
ret = _cache.queryCache();
islock = true;
}
else ret = _context.Products.AsQueryable();
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;
2022-10-16 10:45:18 -04:00
default: break;
2022-10-16 10:33:16 -04:00
}
switch (order) {
case "Price":
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":
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-18 10:43:52 -04:00
List<Product> lst = new();
bool yup = false;
int add = 0;
2022-10-25 10:54:11 -04:00
try {
if (lastId.HasValue) { // Pour avoir les prochains peu importe l'ordre.
List<Product> prods;
if (islock)
lock (_lock) {
prods = ret.ToList();
}
else prods = ret.ToList();
foreach (Product prod in prods) {
2022-10-25 11:57:41 -04:00
if (yup && add < AMOUNT_SCROLL) {
2022-10-25 10:54:11 -04:00
lst.Add(prod);
add++;
}
if (prod.Id == lastId)
yup = true;
2022-10-18 10:43:52 -04:00
}
2022-10-25 10:54:11 -04:00
} else if (all.HasValue && all == true) {
if (islock)
lock (_lock) {
lst = ret.ToList();
}
else lst = ret.ToList();
} else if (islock)
lock (_lock) {
2022-10-25 11:57:41 -04:00
lst = ret.Take(AMOUNT_SCROLL).ToList();
2022-10-25 10:54:11 -04:00
}
2022-10-25 11:57:41 -04:00
else lst = ret.Take(AMOUNT_SCROLL).ToList();
2022-10-25 10:54:11 -04:00
return lst;
} catch (Exception e) {
if (islock)
_logger.LogError(e, "Erreur d'appel de cache.");
else _logger.LogError(e, "Erreur d'appel d'API.");
return new List<Product>();
}
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-25 12:02:27 -04:00
[EnableCors("_myAllowSpecificOrigins"), HttpDelete(Name = "Inventory")]
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) {
_logger.LogError(8, "Delete 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-16 11:25:21 -04:00
if (prod.Quantity > 0) {
prod.Quantity = prod.Quantity - 1;
if (prod.Quantity == 0)
prod.Status = prod.Status == Product.States.Clearance?
Product.States.Discontinued:
2022-10-18 14:48:34 -04:00
Product.States.BackOrder;
2022-10-16 11:25:21 -04:00
}
2022-10-09 15:07:35 -04:00
else {
2022-10-16 11:25:21 -04:00
_logger.LogError(8, "Vente de produit pas en stock.");
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-08 13:22:12 -04:00
}