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

@@ -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;
}