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 InventoryContext _context;
private readonly DatabaseCacheService _cache;
private static object _lock = new object();
private const int AMOUNT_SCROLL = 5;
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)
public IEnumerable<Product> Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) {
bool islock = false;
public IEnumerable<ProductViewModel> Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) {
bool iscache = false;
IQueryable<Product> 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<Product> lst = new();
List<ProductViewModel> lst = new();
bool yup = false;
int add = 0;
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) {
if (yup && add < AMOUNT_SCROLL) {
lst.Add(prod);
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++;
}
if (prod.Id == lastId)
if (!yup && prod.Id == lastId)
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;
} 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<Product>();
return new List<ProductViewModel>();
}
}
// Inventory/Delete => Décrémenter un produit. Va aller chercher directement dans la BD.
@ -143,11 +127,10 @@ public class InventoryController : Controller {
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();
}

View File

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