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;

View File

@ -16,8 +16,7 @@ namespace GrossesMitainesAPI.Controllers;
/// qui sera effectuée dans les 10 secondes après
/// l'éxécution d'une modification de la BD.
/// </summary>
[EnableCors("_myAllowSpecificOrigins")]
[ApiController, Route("api/[controller]")]
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]")]
public class ProductController : ControllerBase {
private readonly ILogger<ProductController> _logger;
private readonly InventoryContext _context;
@ -29,8 +28,7 @@ public class ProductController : ControllerBase {
_cache = cache;
}
[EnableCors("_myAllowSpecificOrigins")]
[HttpGet(Name = "Product"), AllowAnonymous]
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Product"), AllowAnonymous]
public ActionResult<Product> Get(int id) {
Product prod;
try {
@ -43,8 +41,7 @@ public class ProductController : ControllerBase {
return prod;
}
[EnableCors("_myAllowSpecificOrigins")]
[HttpPost(Name = "Product")]
[EnableCors("_myAllowSpecificOrigins"), HttpPost(Name = "Product")]
public ActionResult<Product> Post(Product prod) {
if (prod.Price <= prod.PromoPrice)
prod.PromoPrice = prod.Price - 0.01M;
@ -60,8 +57,7 @@ public class ProductController : ControllerBase {
return prod;
}
[EnableCors("_myAllowSpecificOrigins")]
[HttpPatch(Name = "Product")]
[EnableCors("_myAllowSpecificOrigins"), HttpPatch(Name = "Product")]
public ActionResult<Product> Patch(Product prod) {
try {
_context.Products.Update(prod);
@ -75,8 +71,7 @@ public class ProductController : ControllerBase {
return prod;
}
[EnableCors("_myAllowSpecificOrigins")]
[HttpDelete(Name = "Product")]
[EnableCors("_myAllowSpecificOrigins"), HttpDelete(Name = "Product")]
public ActionResult<int> DeleteProduct(int id) {
try {
_context.Products.Remove(_context.Products.Where(x => x.Id == id).First());

View File

@ -9,8 +9,7 @@ using GrossesMitainesAPI.Services;
namespace GrossesMitainesAPI.Controllers;
[EnableCors("_myAllowSpecificOrigins")]
[ApiController, Route("api/[controller]")]
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]")]
public class SearchController : Controller {
private readonly ILogger<SearchController> _logger;
private readonly InventoryContext _context;
@ -18,7 +17,6 @@ public class SearchController : Controller {
private Product[]? _searchCache = null;
private const int PREVIEW = 4;
public SearchController(ILogger<SearchController> logger, InventoryContext context, DatabaseCacheService cache) {
_logger = logger;
_context = context;
@ -28,8 +26,7 @@ public class SearchController : Controller {
_searchCache = _cache.GetCacheCopy();
}
[EnableCors("_myAllowSpecificOrigins")]
[HttpPost(Name = "Search")]
[EnableCors("_myAllowSpecificOrigins"), HttpPost(Name = "Search")]
public IEnumerable<Product> Post(string query, bool? preview, bool? deep) {
if (_searchCache is not null)
return SearchCached(query, preview, deep);
@ -38,9 +35,7 @@ public class SearchController : Controller {
private List<Product> Search(string query, bool? preview, bool? deep) {
List<Product> products = new();
query = query.Trim();
try { // Pour faire une liste priorisée.
if (preview.HasValue && preview == true)
products = _context.Products.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList();
@ -83,14 +78,11 @@ public class SearchController : Controller {
private List<Product> SearchCached(string query, bool? preview, bool? deep) {
List<Product> products = new();
query = query.Trim();
if (_searchCache is null) {
_logger.LogError(8, "Erreur de cache.");
return products;
return Search(query, preview, deep);
}
try { // Pour faire une liste priorisée.
if (preview.HasValue && preview == true)
products = _searchCache.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList();
@ -98,7 +90,6 @@ public class SearchController : Controller {
if (deep.HasValue && deep == true) {
List<Product> title = new(), desc = new(), cat = new();
query = query.ToLower();
foreach (Product prod in _searchCache) {
string sTitle = prod.Title.Replace(",", " ").ToLower(),
sCat = prod.Category.ToLower(),
@ -127,6 +118,7 @@ public class SearchController : Controller {
}
} catch (Exception e) {
_logger.LogError(8, e.Message);
return Search(query, preview, deep);
}
return products;
}