This commit is contained in:
MarcEricMartel 2022-10-25 07:54:11 -07:00
parent 16c9adb92b
commit 1fcd149de4
3 changed files with 54 additions and 16 deletions

View File

@ -14,6 +14,7 @@ public class InventoryController : Controller {
private readonly ILogger<InventoryController> _logger;
private readonly InventoryContext _context;
private readonly DatabaseCacheService _cache;
private static object _lock = new object();
public InventoryController(ILogger<InventoryController> logger, InventoryContext context, DatabaseCacheService cache) {
_context = context;
@ -25,11 +26,13 @@ public class InventoryController : Controller {
[HttpGet(Name = "Inventory")] // 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) {
const int AMOUNT = 5;
bool islock = false;
IQueryable<Product> ret;
if (_cache.isOk())
ret = _cache.GetCacheCopy().AsQueryable();
if (_cache.isOk()) {
ret = _cache.queryCache();
islock = true;
}
else ret = _context.Products.AsQueryable();
switch (filterPrice) {
@ -93,20 +96,42 @@ public class InventoryController : Controller {
bool yup = false;
int add = 0;
if (lastId.HasValue)
foreach (Product prod in new List<Product>(ret.ToList())) {
if (yup && add < AMOUNT) {
lst.Add(prod);
add++;
}
if (prod.Id == lastId)
yup = true;
}
else if (all.HasValue && all == true)
lst = ret.ToList();
else lst = ret.Take(AMOUNT).ToList();
try {
return lst;
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) {
lst.Add(prod);
add++;
}
if (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).ToList();
}
else lst = ret.Take(AMOUNT).ToList();
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>();
}
}
// Inventory/Delete => Décrémenter un produit. Va aller chercher directement dans la BD.

View File

@ -86,6 +86,9 @@ public class SearchController : Controller {
query = query.Trim();
if (_searchCache is null)
return products;
try { // Pour faire une liste priorisée.
if (preview.HasValue && preview == true)
products = _searchCache.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList();

View File

@ -60,5 +60,15 @@ namespace GrossesMitainesAPI.Services {
}
return copy;
}
public IQueryable<Product> queryCache() {
if (!_ok) return null;
try {
return _cache.AsQueryable();
} catch (Exception e) {
_logger.LogError(e, "Erreur de cache.");
return null;
}
}
}
}