SearchCache va call Search s'il plante

This commit is contained in:
MarcEricMartel
2022-10-25 09:02:27 -07:00
parent 1180e404aa
commit 6371d350fa
3 changed files with 12 additions and 39 deletions

View File

@@ -8,14 +8,12 @@ using GrossesMitainesAPI.Services;
namespace GrossesMitainesAPI.Controllers;
[EnableCors("_myAllowSpecificOrigins")]
[ApiController, Route("api/[controller]")]
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]")]
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,18 +22,15 @@ public class InventoryController : Controller {
_cache = cache;
}
[EnableCors("_myAllowSpecificOrigins")]
[HttpGet(Name = "Inventory")] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant)
[EnableCors("_myAllowSpecificOrigins"), 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) {
bool islock = false;
IQueryable<Product> ret;
if (_cache.isOk()) {
ret = _cache.queryCache();
islock = true;
}
else ret = _context.Products.AsQueryable();
switch (filterPrice) {
case "PriceUnder20":
ret = ret.Where(x => x.Price < 20);
@@ -51,7 +46,6 @@ public class InventoryController : Controller {
break;
default: break;
}
switch (filterState) {
case "isAvailable":
ret = ret.Where(x => x.Status == Product.States.Available);
@@ -70,7 +64,6 @@ public class InventoryController : Controller {
break;
default: break;
}
switch (order) {
case "Price":
ret = ret.OrderBy(x => x.Status == Product.States.Promotion || x.Status == Product.States.Clearance? x.PromoPrice: x.Price);
@@ -92,13 +85,10 @@ public class InventoryController : Controller {
break;
default: break;
}
List<Product> 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)
@@ -125,7 +115,6 @@ public class InventoryController : Controller {
lst = ret.Take(AMOUNT_SCROLL).ToList();
}
else lst = ret.Take(AMOUNT_SCROLL).ToList();
return lst;
} catch (Exception e) {
if (islock)
@@ -134,17 +123,14 @@ public class InventoryController : Controller {
return new List<Product>();
}
}
// Inventory/Delete => Décrémenter un produit. Va aller chercher directement dans la BD.
[EnableCors("_myAllowSpecificOrigins")]
[HttpDelete(Name = "Inventory")]
[EnableCors("_myAllowSpecificOrigins"), HttpDelete(Name = "Inventory")]
public ActionResult<int> Delete(int? id) {
int rid = 0;
if (!id.HasValue) {
_logger.LogError(8, "Delete sans Id.");
return BadRequest();
}
try {
Product prod = _context.Products.First(x => x.Id == id);
rid = prod.Id;