GGMM/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs
2022-10-30 13:58:47 -07:00

165 lines
6.9 KiB
C#

namespace GrossesMitainesAPI.Controllers;
#region Dependencies
using Microsoft.AspNetCore.Mvc;
using GrossesMitainesAPI.Models;
using System.Linq;
using GrossesMitainesAPI.Data;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using GrossesMitainesAPI.Services;
using System.Collections.Immutable;
#endregion
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"),
Authorize(AuthenticationSchemes = "Identity.Application")]
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 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())
_searchCache = _cache.queryCache();
}
#endregion
#region API Methods
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Search"), AllowAnonymous]
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);
}
#endregion
#region Private Methods
private List<ProductViewModel> Search(string query, string? filterPrice, string? filterState, string? order) {
List<ProductViewModel> products = new();
query = query.Trim();
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() + " ",
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);
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);
}
break;
}
} catch (Exception e) {
_logger.LogError(8, e.Message);
return new List<ProductViewModel>();
}
return products;
}
#endregion
}