GGMM/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs

165 lines
6.9 KiB
C#
Raw Normal View History

2022-10-30 12:17:10 -04:00
namespace GrossesMitainesAPI.Controllers;
#region Dependencies
using Microsoft.AspNetCore.Mvc;
2022-10-09 14:23:46 -04:00
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
2022-10-30 12:17:10 -04:00
#endregion
2022-10-09 14:23:46 -04:00
2022-10-30 16:58:47 -04:00
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"),
Authorize(AuthenticationSchemes = "Identity.Application")]
2022-10-09 14:23:46 -04:00
public class SearchController : Controller {
2022-10-30 12:17:10 -04:00
#region Constants
private const int PREVIEW = 4;
#endregion
#region DI Fields
2022-10-09 14:23:46 -04:00
private readonly ILogger<SearchController> _logger;
private readonly InventoryContext _context;
private readonly DatabaseCacheService _cache;
2022-10-30 12:17:10 -04:00
private IQueryable<Product>? _searchCache = null;
2022-10-09 14:23:46 -04:00
2022-10-30 12:17:10 -04:00
#endregion
#region Ctor
public SearchController(ILogger<SearchController> logger, InventoryContext context, DatabaseCacheService cache) {
2022-10-09 14:23:46 -04:00
_logger = logger;
_context = context;
_cache = cache;
2022-10-30 12:17:10 -04:00
if (_cache.isOk())
_searchCache = _cache.queryCache();
2022-10-09 14:23:46 -04:00
}
2022-10-30 12:17:10 -04:00
#endregion
#region API Methods
2022-10-27 12:37:13 -04:00
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Search"), AllowAnonymous]
2022-10-30 12:17:10 -04:00
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);
}
2022-10-25 11:51:59 -04:00
2022-10-30 12:17:10 -04:00
#endregion
#region Private Methods
private List<ProductViewModel> Search(string query, string? filterPrice, string? filterState, string? order) {
List<ProductViewModel> products = new();
query = query.Trim();
2022-10-30 12:17:10 -04:00
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()) {
2022-10-26 16:27:24 -04:00
string sTitle = prod.Title.Replace(",", " ").ToLower() + " ",
2022-10-30 12:17:10 -04:00
sCat = " " + prod.Category.ToLower() + " ",
2022-10-26 16:27:24 -04:00
sDesc = prod.Description.Replace(".", " ").Replace(",", " ").ToLower() + " ";
if (sTitle.StartsWith(query))
products.Add(prod);
2022-10-30 16:58:47 -04:00
else if (sTitle.Contains(" " + query))
title.Add(prod);
2022-10-30 16:58:47 -04:00
else if (sDesc.Contains(" " + query))
desc.Add(prod);
2022-10-30 16:58:47 -04:00
else if (sCat.Contains(" " + query))
cat.Add(prod);
}
products.AddRange(title);
products.AddRange(desc);
products.AddRange(cat);
2022-10-30 12:17:10 -04:00
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()) {
2022-10-26 16:27:24 -04:00
string sTitle = prod.Title.Replace(",", " ").ToLower() + " ",
2022-10-30 12:17:10 -04:00
sCat = " " + prod.Category.ToLower() + " ",
2022-10-26 16:27:24 -04:00
sDesc = prod.Description.Replace(".", " ").Replace(",", " ").ToLower() + " ";
2022-10-30 12:17:10 -04:00
if (sTitle.StartsWith(query)
|| sTitle.Contains(" " + query + " ")
|| sDesc.StartsWith(query) || sDesc.Contains(" " + query + " ")
|| sCat.Contains(" " + query + " "))
2022-10-18 14:25:23 -04:00
products.Add(prod);
}
2022-10-30 12:17:10 -04:00
break;
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-30 12:17:10 -04:00
return new List<ProductViewModel>();
2022-10-09 14:23:46 -04:00
}
return products;
}
2022-10-30 12:17:10 -04:00
#endregion
2022-10-09 14:23:46 -04:00
}