This commit is contained in:
MarcEricMartel 2022-10-27 11:32:06 -07:00
parent 83331a4a08
commit f3dcc0c4d8
2 changed files with 25 additions and 40 deletions

View File

@ -14,7 +14,6 @@ public class InventoryController : Controller {
private readonly ILogger<InventoryController> _logger; private readonly ILogger<InventoryController> _logger;
private readonly InventoryContext _context; private readonly InventoryContext _context;
private readonly DatabaseCacheService _cache; private readonly DatabaseCacheService _cache;
private static object _lock = new object();
private const int AMOUNT_SCROLL = 5; private const int AMOUNT_SCROLL = 5;
public InventoryController(ILogger<InventoryController> logger, InventoryContext context, DatabaseCacheService cache) { public InventoryController(ILogger<InventoryController> logger, InventoryContext context, DatabaseCacheService cache) {
@ -24,12 +23,12 @@ public class InventoryController : Controller {
} }
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Inventory"), AllowAnonymous] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant) [EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Inventory"), AllowAnonymous] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant)
public IEnumerable<Product> Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) { public IEnumerable<ProductViewModel> Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) {
bool islock = false; bool iscache = false;
IQueryable<Product> ret; IQueryable<Product> ret;
if (_cache.isOk()) { if (_cache.isOk()) {
ret = _cache.queryCache(); ret = _cache.queryCache();
islock = true; iscache = true;
} }
else ret = _context.Products.AsQueryable(); else ret = _context.Products.AsQueryable();
switch (filterPrice) { switch (filterPrice) {
@ -89,42 +88,27 @@ public class InventoryController : Controller {
break; break;
default: break; default: break;
} }
List<Product> lst = new(); List<ProductViewModel> lst = new();
bool yup = false; bool yup = false;
int add = 0; int add = 0;
try { try {
if (lastId.HasValue) { // Pour avoir les prochains peu importe l'ordre. if (!lastId.HasValue || lastId == 0)
List<Product> prods; yup = true;
if (islock)
lock (_lock) { foreach (Product prod in ret.ToList()) {
prods = ret.ToList(); if (yup && add < AMOUNT_SCROLL || (all.HasValue && all == true)) {
} lst.Add(new ProductViewModel(prod));
else prods = ret.ToList();
foreach (Product prod in prods) {
if (yup && add < AMOUNT_SCROLL) {
lst.Add(prod);
add++; add++;
} }
if (prod.Id == lastId) if (!yup && prod.Id == lastId)
yup = true; yup = true;
} }
} else if (all.HasValue && all == true) {
if (islock)
lock (_lock) {
lst = ret.ToList();
}
else lst = ret.ToList();
} else if (islock)
lock (_lock) {
lst = ret.Take(AMOUNT_SCROLL).ToList();
}
else lst = ret.Take(AMOUNT_SCROLL).ToList();
return lst; return lst;
} catch (Exception e) { } catch (Exception e) {
if (islock) if (iscache)
_logger.LogError(e, "Erreur d'appel de cache."); _logger.LogError(e, "Erreur d'appel de cache.");
else _logger.LogError(e, "Erreur d'appel d'API."); else _logger.LogError(e, "Erreur d'appel d'API.");
return new List<Product>(); return new List<ProductViewModel>();
} }
} }
// Inventory/Delete => Décrémenter un produit. Va aller chercher directement dans la BD. // Inventory/Delete => Décrémenter un produit. Va aller chercher directement dans la BD.
@ -146,8 +130,7 @@ public class InventoryController : Controller {
prod.Status = prod.Status == Product.States.Clearance ? prod.Status = prod.Status == Product.States.Clearance ?
Product.States.Discontinued : Product.States.Discontinued :
Product.States.BackOrder; Product.States.BackOrder;
} } else {
else {
_logger.LogError(8, "Vente de produit pas en stock."); _logger.LogError(8, "Vente de produit pas en stock.");
return BadRequest(); return BadRequest();
} }

View File

@ -26,8 +26,10 @@ namespace GrossesMitainesAPI.Services {
_ok = UpdateCache(); _ok = UpdateCache();
_needUpd = !_ok; _needUpd = !_ok;
} }
if (_hits.Count > 0 && _ok) if (_hits.Count > 0 && _ok) {
UpdateMetrics(); UpdateMetrics();
//_needUpd = true;
}
} }
} }
private bool UpdateCache() { private bool UpdateCache() {