Search upgraded
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
namespace GrossesMitainesAPI.Controllers;
|
||||
|
||||
#region Dependencies
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using GrossesMitainesAPI.Models;
|
||||
using System.Linq;
|
||||
using GrossesMitainesAPI.Data;
|
||||
@@ -8,118 +11,153 @@ using Microsoft.AspNetCore.Cors;
|
||||
using GrossesMitainesAPI.Services;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace GrossesMitainesAPI.Controllers;
|
||||
#endregion
|
||||
|
||||
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]")]
|
||||
public class SearchController : Controller {
|
||||
#region Constants
|
||||
private const int PREVIEW = 4;
|
||||
|
||||
#endregion
|
||||
|
||||
#region DI Fields
|
||||
private readonly ILogger<SearchController> _logger;
|
||||
private readonly InventoryContext _context;
|
||||
private readonly DatabaseCacheService _cache;
|
||||
private Product[]? _searchCache = null;
|
||||
private const int PREVIEW = 4;
|
||||
private IQueryable<Product>? _searchCache = null;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Ctor
|
||||
public SearchController(ILogger<SearchController> logger, InventoryContext context, DatabaseCacheService cache) {
|
||||
_logger = logger;
|
||||
_context = context;
|
||||
_cache = cache;
|
||||
if (_cache.isOk()) // Se fait une copie de la cache si elle est fonctionnelle.
|
||||
_searchCache = _cache.GetCacheCopy();
|
||||
if (_cache.isOk())
|
||||
_searchCache = _cache.queryCache();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region API Methods
|
||||
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Search"), AllowAnonymous]
|
||||
public IEnumerable<Product> Get(string query, bool? preview, bool? deep) {
|
||||
if (_searchCache is not null)
|
||||
return SearchCached(query, preview, deep);
|
||||
else return SearchDirect(query, preview, deep);
|
||||
public IEnumerable<ProductViewModel> Get(string query, bool? preview, string? filterPrice, string? filterState, string? order = "Relevance") {
|
||||
if (preview.HasValue && preview == true)
|
||||
return _searchCache is null ?
|
||||
_context.Products.Where(x => x.Title.Contains(query)).Take(PREVIEW).Select(x => new ProductViewModel(x)).ToList() :
|
||||
_searchCache.Where(x => x.Title.Contains(query)).Take(PREVIEW).Select(x => new ProductViewModel(x)).ToList();
|
||||
return Search(query, filterPrice, filterState, order);
|
||||
}
|
||||
|
||||
private List<Product> SearchDirect(string query, bool? preview, bool? deep) {
|
||||
List<Product> products = new();
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
private List<ProductViewModel> Search(string query, string? filterPrice, string? filterState, string? order) {
|
||||
List<ProductViewModel> 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();
|
||||
else {
|
||||
if (deep.HasValue && deep == true) {
|
||||
List<Product> title = new(), desc = new(), cat = new();
|
||||
query = query.ToLower();
|
||||
foreach (Product prod in _context.Products.ToArray()) {
|
||||
try {
|
||||
query = query.ToLower();
|
||||
IQueryable<ProductViewModel> ret;
|
||||
if (_searchCache is null) {
|
||||
_logger.LogError(8, "Erreur de cache.");
|
||||
ret = _context.Products.AsQueryable().Select(x => new ProductViewModel(x));
|
||||
} else ret = _searchCache.Select(x => new ProductViewModel(x));
|
||||
|
||||
switch (filterPrice) {
|
||||
case "PriceUnder20":
|
||||
ret = ret.Where(x => x.Price < 20);
|
||||
break;
|
||||
case "Price20to49":
|
||||
ret = ret.Where(x => x.Price >= 20 && x.Price < 50);
|
||||
break;
|
||||
case "Price50to99":
|
||||
ret = ret.Where(x => x.Price >= 50 && x.Price < 100);
|
||||
break;
|
||||
case "PriceOver100":
|
||||
ret = ret.Where(x => x.Price >= 100);
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
switch (filterState) {
|
||||
case "isAvailable":
|
||||
ret = ret.Where(x => x.Status == Product.States.Available);
|
||||
break;
|
||||
case "isUnavailable":
|
||||
ret = ret.Where(x => x.Status == Product.States.Unavailable);
|
||||
break;
|
||||
case "isBackOrder":
|
||||
ret = ret.Where(x => x.Status == Product.States.BackOrder);
|
||||
break; ;
|
||||
case "isClearance":
|
||||
ret = ret.Where(x => x.Status == Product.States.Clearance);
|
||||
break;
|
||||
case "isDiscontinued":
|
||||
ret = ret.Where(x => x.Status == Product.States.Discontinued);
|
||||
break;
|
||||
case "isPromoted":
|
||||
ret = ret.Where(x => x.Status == Product.States.Clearance || x.Status == Product.States.Promotion);
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
switch (order) {
|
||||
case "Relevance":
|
||||
List<ProductViewModel> title = new(), desc = new(), cat = new();
|
||||
foreach (ProductViewModel prod in ret.ToList()) {
|
||||
string sTitle = prod.Title.Replace(",", " ").ToLower() + " ",
|
||||
sCat = prod.Category.ToLower() + " ",
|
||||
sCat = " " + prod.Category.ToLower() + " ",
|
||||
sDesc = prod.Description.Replace(".", " ").Replace(",", " ").ToLower() + " ";
|
||||
if (sTitle.StartsWith(query))
|
||||
products.Add(prod);
|
||||
else if (sTitle.Contains(" " + query + " "))
|
||||
title.Add(prod);
|
||||
else if (sDesc.StartsWith(query) || sDesc.Contains(" " + query + " "))
|
||||
else if (sDesc.Contains(" " + query + " "))
|
||||
desc.Add(prod);
|
||||
else if (sCat.Contains(query))
|
||||
else if (sCat.Contains(" " + query + " "))
|
||||
cat.Add(prod);
|
||||
}
|
||||
products.AddRange(title);
|
||||
products.AddRange(desc);
|
||||
products.AddRange(cat);
|
||||
} else {
|
||||
products = _context.Products.Where(x => x.Title.Contains(query)).ToList();
|
||||
foreach (Product prod in _context.Products.Where(x => x.Description.Contains(query)).ToArray())
|
||||
if (!products.Contains(prod))
|
||||
break;
|
||||
case "Price":
|
||||
ret = ret.OrderBy(x => x.Status == Product.States.Promotion || x.Status == Product.States.Clearance ? x.PromoPrice : x.Price);
|
||||
goto default;
|
||||
case "PriceDesc":
|
||||
ret = ret.OrderByDescending(x => x.Status == Product.States.Promotion || x.Status == Product.States.Clearance ? x.PromoPrice : x.Price);
|
||||
goto default;
|
||||
case "Title":
|
||||
ret = ret.OrderBy(x => x.Title);
|
||||
goto default;
|
||||
case "TitleDesc":
|
||||
ret = ret.OrderByDescending(x => x.Title);
|
||||
goto default;
|
||||
case "Category":
|
||||
ret = ret.OrderBy(x => x.Category);
|
||||
goto default;
|
||||
case "CategoryDesc":
|
||||
ret = ret.OrderByDescending(x => x.Category);
|
||||
goto default;
|
||||
default:
|
||||
foreach (ProductViewModel prod in ret.ToList()) {
|
||||
string sTitle = prod.Title.Replace(",", " ").ToLower() + " ",
|
||||
sCat = " " + prod.Category.ToLower() + " ",
|
||||
sDesc = prod.Description.Replace(".", " ").Replace(",", " ").ToLower() + " ";
|
||||
if (sTitle.StartsWith(query)
|
||||
|| sTitle.Contains(" " + query + " ")
|
||||
|| sDesc.StartsWith(query) || sDesc.Contains(" " + query + " ")
|
||||
|| sCat.Contains(" " + query + " "))
|
||||
products.Add(prod);
|
||||
foreach (Product prod in _context.Products.Where(x => x.Category.Contains(query)).ToArray())
|
||||
if (!products.Contains(prod))
|
||||
products.Add(prod);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
_logger.LogError(8, e.Message);
|
||||
return new List<ProductViewModel>();
|
||||
}
|
||||
return products;
|
||||
}
|
||||
|
||||
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 SearchDirect(query, preview, deep); // Fallback vers version non-cached en cas d'erreur.
|
||||
}
|
||||
try { // Pour faire une liste priorisée.
|
||||
if (preview.HasValue && preview == true)
|
||||
products = _searchCache.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList();
|
||||
else {
|
||||
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() + " ",
|
||||
sDesc = prod.Description.Replace(".", " ").Replace(",", " ").ToLower() + " ";
|
||||
if (sTitle.StartsWith(query))
|
||||
products.Add(prod);
|
||||
else if (sTitle.Contains(" " + query + " "))
|
||||
title.Add(prod);
|
||||
else if (sDesc.StartsWith(query) || sDesc.Contains(" " + query + " "))
|
||||
desc.Add(prod);
|
||||
else if (sCat.Contains(query))
|
||||
cat.Add(prod);
|
||||
}
|
||||
products.AddRange(title);
|
||||
products.AddRange(desc);
|
||||
products.AddRange(cat);
|
||||
} else {
|
||||
products = _searchCache.Where(x => x.Title.Contains(query)).ToList();
|
||||
foreach (Product prod in _searchCache.Where(x => x.Description.Contains(query)).ToArray())
|
||||
if (!products.Contains(prod))
|
||||
products.Add(prod);
|
||||
foreach (Product prod in _searchCache.Where(x => x.Category.Contains(query)).ToArray())
|
||||
if (!products.Contains(prod))
|
||||
products.Add(prod);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
_logger.LogError(8, e.Message);
|
||||
return SearchDirect(query, preview, deep); // Fallback vers version non-cached en cas d'erreur.
|
||||
}
|
||||
return products;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user