yeehaw
This commit is contained in:
parent
83331a4a08
commit
f3dcc0c4d8
@ -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) {
|
||||
@ -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.
|
||||
@ -146,8 +130,7 @@ public class InventoryController : Controller {
|
||||
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();
|
||||
}
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user