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

View File

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

View File

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