From f3dcc0c4d8562d82beac6c2a6f6cf942fdfc730a Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Thu, 27 Oct 2022 11:32:06 -0700 Subject: [PATCH] yeehaw --- .../Controllers/InventoryController.cs | 59 +++++++------------ .../Services/DatabaseCacheService.cs | 6 +- 2 files changed, 25 insertions(+), 40 deletions(-) diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs index 065d28d..274fd81 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs @@ -14,7 +14,6 @@ public class InventoryController : Controller { private readonly ILogger _logger; private readonly InventoryContext _context; private readonly DatabaseCacheService _cache; - private static object _lock = new object(); private const int AMOUNT_SCROLL = 5; public InventoryController(ILogger 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) - public IEnumerable Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) { - bool islock = false; + public IEnumerable Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) { + bool iscache = false; IQueryable ret; if (_cache.isOk()) { ret = _cache.queryCache(); - islock = true; + iscache = true; } else ret = _context.Products.AsQueryable(); switch (filterPrice) { @@ -70,7 +69,7 @@ public class InventoryController : Controller { } switch (order) { case "Price": - ret = ret.OrderBy(x => x.Status == Product.States.Promotion || x.Status == Product.States.Clearance? x.PromoPrice: x.Price); + ret = ret.OrderBy(x => x.Status == Product.States.Promotion || x.Status == Product.States.Clearance ? x.PromoPrice : x.Price); break; case "PriceDesc": ret = ret.OrderByDescending(x => x.Status == Product.States.Promotion || x.Status == Product.States.Clearance ? x.PromoPrice : x.Price); @@ -89,42 +88,27 @@ public class InventoryController : Controller { break; default: break; } - List lst = new(); + List lst = new(); bool yup = false; int add = 0; try { - if (lastId.HasValue) { // Pour avoir les prochains peu importe l'ordre. - List prods; - if (islock) - lock (_lock) { - prods = ret.ToList(); - } - else prods = ret.ToList(); - foreach (Product prod in prods) { - if (yup && add < AMOUNT_SCROLL) { - lst.Add(prod); - add++; - } - if (prod.Id == lastId) - yup = true; + if (!lastId.HasValue || lastId == 0) + yup = true; + + foreach (Product prod in ret.ToList()) { + if (yup && add < AMOUNT_SCROLL || (all.HasValue && all == true)) { + lst.Add(new ProductViewModel(prod)); + add++; } - } 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(); + if (!yup && prod.Id == lastId) + yup = true; + } return lst; } catch (Exception e) { - if (islock) + if (iscache) _logger.LogError(e, "Erreur d'appel de cache."); else _logger.LogError(e, "Erreur d'appel d'API."); - return new List(); + return new List(); } } // Inventory/Delete => Décrémenter un produit. Va aller chercher directement dans la BD. @@ -138,16 +122,15 @@ public class InventoryController : Controller { try { Product prod = _context.Products.First(x => x.Id == id); rid = prod.Id; - if (prod.Quantity > 0) { + if (prod.Quantity > 0) { prod.Quantity = prod.Quantity - 1; prod.Sales = prod.Sales + 1; prod.LastSale = DateTime.Now; if (prod.Quantity == 0) - prod.Status = prod.Status == Product.States.Clearance? - Product.States.Discontinued: + prod.Status = prod.Status == Product.States.Clearance ? + Product.States.Discontinued : Product.States.BackOrder; - } - else { + } else { _logger.LogError(8, "Vente de produit pas en stock."); return BadRequest(); } diff --git a/GrossesMitaines/GrossesMitainesAPI/Services/DatabaseCacheService.cs b/GrossesMitaines/GrossesMitainesAPI/Services/DatabaseCacheService.cs index 6603b22..39dc74d 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Services/DatabaseCacheService.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Services/DatabaseCacheService.cs @@ -25,9 +25,11 @@ namespace GrossesMitainesAPI.Services { if (_needUpd) { _ok = UpdateCache(); _needUpd = !_ok; - } - if (_hits.Count > 0 && _ok) + } + if (_hits.Count > 0 && _ok) { UpdateMetrics(); + //_needUpd = true; + } } } private bool UpdateCache() {