GGMM/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs

126 lines
5.8 KiB
C#
Raw Normal View History

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;
using GrossesMitainesAPI.Services;
2022-10-25 12:06:06 -04:00
using System.Collections.Immutable;
2022-10-09 14:23:46 -04:00
namespace GrossesMitainesAPI.Controllers;
2022-10-25 12:02:27 -04:00
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]")]
2022-10-09 14:23:46 -04:00
public class SearchController : Controller {
private readonly ILogger<SearchController> _logger;
private readonly InventoryContext _context;
private readonly DatabaseCacheService _cache;
private Product[]? _searchCache = null;
private const int PREVIEW = 4;
2022-10-09 14:23:46 -04:00
public SearchController(ILogger<SearchController> logger, InventoryContext context, DatabaseCacheService cache) {
2022-10-09 14:23:46 -04:00
_logger = logger;
_context = context;
_cache = cache;
if (_cache.isOk())
_searchCache = _cache.GetCacheCopy();
2022-10-09 14:23:46 -04:00
}
2022-10-25 12:02:27 -04:00
[EnableCors("_myAllowSpecificOrigins"), HttpPost(Name = "Search")]
2022-10-18 14:25:23 -04:00
public IEnumerable<Product> Post(string query, bool? preview, bool? deep) {
if (_searchCache is not null)
return SearchCached(query, preview, deep);
else return Search(query, preview, deep);
}
2022-10-25 11:51:59 -04:00
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();
else {
if (deep.HasValue && deep == true) {
List<Product> title = new(), desc = new(), cat = new();
query = query.ToLower();
foreach (Product prod in _context.Products.ToList()) {
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 = _context.Products.Where(x => x.Title.Contains(query)).ToList();
foreach (Product prod in _context.Products.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())
if (!products.Contains(prod))
products.Add(prod);
}
}
} catch (Exception e) {
_logger.LogError(8, e.Message);
}
return products;
}
private List<Product> SearchCached(string query, bool? preview, bool? deep) {
2022-10-18 13:17:28 -04:00
List<Product> products = new();
2022-10-09 14:23:46 -04:00
query = query.Trim();
2022-10-25 11:51:59 -04:00
if (_searchCache is null) {
_logger.LogError(8, "Erreur de cache.");
2022-10-25 13:19:30 -04:00
return Search(query, preview, deep); // Fallback vers version non-cached.
2022-10-25 11:51:59 -04:00
}
2022-10-09 14:23:46 -04:00
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) {
2022-10-18 15:19:19 -04:00
string sTitle = prod.Title.Replace(",", " ").ToLower(),
sCat = prod.Category.ToLower(),
2022-10-18 14:25:23 -04:00
sDesc = prod.Description.Replace(".", " ").Replace(",", " ").ToLower();
if (sTitle.StartsWith(query))
products.Add(prod);
else if (sTitle.Contains(" " + query + " "))
title.Add(prod);
2022-10-18 15:19:19 -04:00
else if (sDesc.StartsWith(query) || sDesc.Contains(" " + query + " "))
2022-10-18 14:25:23 -04:00
desc.Add(prod);
2022-10-18 15:19:19 -04:00
else if (sCat.Contains(query))
2022-10-18 14:25:23 -04:00
cat.Add(prod);
}
products.AddRange(title);
products.AddRange(desc);
products.AddRange(cat);
} else {
products = _searchCache.Where(x => x.Title.Contains(query)).ToList();
2022-10-25 13:17:54 -04:00
foreach (Product prod in _searchCache.Where(x => x.Description.Contains(query)).ToArray())
2022-10-18 14:25:23 -04:00
if (!products.Contains(prod))
products.Add(prod);
2022-10-25 13:17:54 -04:00
foreach (Product prod in _searchCache.Where(x => x.Category.Contains(query)).ToArray())
2022-10-18 14:25:23 -04:00
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);
2022-10-25 13:19:30 -04:00
return Search(query, preview, deep); // Fallback vers version non-cached.
2022-10-09 14:23:46 -04:00
}
return products;
}
}