react-version #1
@ -13,15 +13,21 @@ namespace GrossesMitainesAPI.Controllers;
|
||||
public class SearchController : Controller {
|
||||
private readonly ILogger<SearchController> _logger;
|
||||
private readonly InventoryContext _context;
|
||||
private static List<Product> _searchCache = new();
|
||||
private static DateTime? _cacheDate;
|
||||
|
||||
public SearchController(ILogger<SearchController> logger, InventoryContext context) {
|
||||
_logger = logger;
|
||||
_context = context;
|
||||
if (_searchCache.Count == 0 || _cacheDate.HasValue && _cacheDate.Value.AddMinutes(5) < DateTime.Now) {
|
||||
_searchCache = _context.Products.ToList();
|
||||
_cacheDate = DateTime.Now;
|
||||
}
|
||||
}
|
||||
|
||||
[EnableCors("_myAllowSpecificOrigins")]
|
||||
[HttpPost(Name = "Search")]
|
||||
public IEnumerable<Product> Post(string query, bool? preview) {
|
||||
public IEnumerable<Product> Post(string query, bool? preview, bool? deep) {
|
||||
const int PREVIEW = 3;
|
||||
List<Product> products = new();
|
||||
|
||||
@ -29,21 +35,45 @@ public class SearchController : Controller {
|
||||
|
||||
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 {
|
||||
products = _context.Products.Where(x => x.Title.Replace(".", " ")
|
||||
.Replace(",", " ")
|
||||
.ToUpper()
|
||||
.Contains(" " + query.ToUpper() + " "))
|
||||
.ToList();
|
||||
//products = _context.Products.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList();
|
||||
products = _searchCache.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList();
|
||||
|
||||
foreach (Product prod in _context.Products.Where(x => x.Category.Contains(query)).ToList()) {
|
||||
if (!products.Contains(prod))
|
||||
products.Add(prod);
|
||||
}
|
||||
foreach (Product prod in _context.Products.Where(x => x.Description.Contains(query)).ToList()) {
|
||||
if (!products.Contains(prod))
|
||||
products.Add(prod);
|
||||
else {
|
||||
if (deep.HasValue && deep == true) {
|
||||
List<Product> title = new(), desc = new(), cat = new();
|
||||
query = query.ToLower();
|
||||
|
||||
// foreach (Product prod in _context.Products.ToList()) {
|
||||
foreach (Product prod in _searchCache) {
|
||||
string sTitle = prod.Title.Replace(".", " ").Replace(",", " ").ToLower(),
|
||||
sCat = prod.Category.Replace(".", " ").Replace(",", " ").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.Contains(" " + query + " "))
|
||||
desc.Add(prod);
|
||||
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();
|
||||
products = _searchCache.Where(x => x.Title.Contains(query)).ToList();
|
||||
|
||||
// foreach (Product prod in _context.Products.Where(x => x.Description.Contains(query)).ToList())
|
||||
foreach (Product prod in _searchCache.Where(x => x.Description.Contains(query)).ToList())
|
||||
if (!products.Contains(prod))
|
||||
products.Add(prod);
|
||||
// foreach (Product prod in _context.Products.Where(x => x.Category.Contains(query)).ToList())
|
||||
foreach (Product prod in _searchCache.Where(x => x.Category.Contains(query)).ToList())
|
||||
if (!products.Contains(prod))
|
||||
products.Add(prod);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
Loading…
Reference in New Issue
Block a user