2022-10-09 14:23:46 -04:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using GrossesMitainesAPI.Models;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using GrossesMitainesAPI.Data;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2022-10-16 11:25:21 -04:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2022-10-18 10:08:23 -04:00
|
|
|
|
using Microsoft.AspNetCore.Cors;
|
2022-10-09 14:23:46 -04:00
|
|
|
|
|
|
|
|
|
namespace GrossesMitainesAPI.Controllers;
|
|
|
|
|
|
2022-10-18 10:08:23 -04:00
|
|
|
|
[EnableCors("_myAllowSpecificOrigins")]
|
2022-10-09 14:23:46 -04:00
|
|
|
|
[ApiController, Route("api/[controller]")]
|
|
|
|
|
public class SearchController : Controller {
|
|
|
|
|
private readonly ILogger<SearchController> _logger;
|
|
|
|
|
private readonly InventoryContext _context;
|
2022-10-18 14:25:23 -04:00
|
|
|
|
private static List<Product> _searchCache = new();
|
|
|
|
|
private static DateTime? _cacheDate;
|
2022-10-09 14:23:46 -04:00
|
|
|
|
|
|
|
|
|
public SearchController(ILogger<SearchController> logger, InventoryContext context) {
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_context = context;
|
2022-10-18 14:25:23 -04:00
|
|
|
|
if (_searchCache.Count == 0 || _cacheDate.HasValue && _cacheDate.Value.AddMinutes(5) < DateTime.Now) {
|
|
|
|
|
_searchCache = _context.Products.ToList();
|
|
|
|
|
_cacheDate = DateTime.Now;
|
|
|
|
|
}
|
2022-10-09 14:23:46 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 10:08:23 -04:00
|
|
|
|
[EnableCors("_myAllowSpecificOrigins")]
|
2022-10-09 14:23:46 -04:00
|
|
|
|
[HttpPost(Name = "Search")]
|
2022-10-18 14:25:23 -04:00
|
|
|
|
public IEnumerable<Product> Post(string query, bool? preview, bool? deep) {
|
2022-10-18 14:37:18 -04:00
|
|
|
|
const int PREVIEW = 4;
|
2022-10-18 13:17:28 -04:00
|
|
|
|
List<Product> products = new();
|
2022-10-09 14:23:46 -04:00
|
|
|
|
|
|
|
|
|
query = query.Trim();
|
|
|
|
|
|
|
|
|
|
try { // Pour faire une liste priorisée.
|
2022-10-16 20:33:58 -04:00
|
|
|
|
if (preview.HasValue && preview == true)
|
2022-10-18 14:25:23 -04:00
|
|
|
|
products = _searchCache.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList();
|
2022-10-16 20:33:58 -04:00
|
|
|
|
else {
|
2022-10-18 14:25:23 -04:00
|
|
|
|
if (deep.HasValue && deep == true) {
|
|
|
|
|
List<Product> title = new(), desc = new(), cat = new();
|
|
|
|
|
query = query.ToLower();
|
|
|
|
|
|
2022-10-18 14:48:34 -04:00
|
|
|
|
foreach (Product prod in _searchCache) {
|
|
|
|
|
string sTitle = prod.Title.Replace(".", " ").Replace(",", " ").ToLower(),
|
2022-10-18 14:25:23 -04:00
|
|
|
|
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 = _searchCache.Where(x => x.Title.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 _searchCache.Where(x => x.Category.Contains(query)).ToList())
|
|
|
|
|
if (!products.Contains(prod))
|
|
|
|
|
products.Add(prod);
|
2022-10-18 13:17:28 -04:00
|
|
|
|
}
|
2022-10-16 20:33:58 -04:00
|
|
|
|
}
|
2022-10-09 14:23:46 -04:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
_logger.LogError(8, e.Message);
|
|
|
|
|
}
|
|
|
|
|
return products;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|