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;
|
2022-10-21 17:52:25 -04:00
|
|
|
|
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;
|
2022-10-21 17:52:25 -04:00
|
|
|
|
private readonly DatabaseCacheService _cache;
|
2022-11-01 13:33:08 -04:00
|
|
|
|
private IQueryable<ProductModel>? _searchCache = null;
|
2022-10-09 14:23:46 -04:00
|
|
|
|
|
2022-10-30 12:17:10 -04:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Ctor
|
2022-10-21 17:52:25 -04:00
|
|
|
|
public SearchController(ILogger<SearchController> logger, InventoryContext context, DatabaseCacheService cache) {
|
2022-10-09 14:23:46 -04:00
|
|
|
|
_logger = logger;
|
|
|
|
|
_context = context;
|
2022-10-21 17:52:25 -04:00
|
|
|
|
_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-21 17:52:25 -04:00
|
|
|
|
}
|
2022-10-25 11:51:59 -04:00
|
|
|
|
|
2022-10-30 12:17:10 -04:00
|
|
|
|
#endregion
|
|
|
|
|
|
2022-11-03 12:45:59 -04:00
|
|
|
|
#region Internal Methods
|
2022-10-30 12:17:10 -04:00
|
|
|
|
private List<ProductViewModel> Search(string query, string? filterPrice, string? filterState, string? order) {
|
|
|
|
|
List<ProductViewModel> products = new();
|
2022-10-21 17:52:25 -04:00
|
|
|
|
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":
|
2022-11-01 13:33:08 -04:00
|
|
|
|
ret = ret.Where(x => x.Status == ProductModel.States.Available);
|
2022-10-30 12:17:10 -04:00
|
|
|
|
break;
|
|
|
|
|
case "isUnavailable":
|
2022-11-01 13:33:08 -04:00
|
|
|
|
ret = ret.Where(x => x.Status == ProductModel.States.Unavailable);
|
2022-10-30 12:17:10 -04:00
|
|
|
|
break;
|
|
|
|
|
case "isBackOrder":
|
2022-11-01 13:33:08 -04:00
|
|
|
|
ret = ret.Where(x => x.Status == ProductModel.States.BackOrder);
|
2022-10-30 12:17:10 -04:00
|
|
|
|
break; ;
|
|
|
|
|
case "isClearance":
|
2022-11-01 13:33:08 -04:00
|
|
|
|
ret = ret.Where(x => x.Status == ProductModel.States.Clearance);
|
2022-10-30 12:17:10 -04:00
|
|
|
|
break;
|
|
|
|
|
case "isDiscontinued":
|
2022-11-01 13:33:08 -04:00
|
|
|
|
ret = ret.Where(x => x.Status == ProductModel.States.Discontinued);
|
2022-10-30 12:17:10 -04:00
|
|
|
|
break;
|
|
|
|
|
case "isPromoted":
|
2022-11-01 13:33:08 -04:00
|
|
|
|
ret = ret.Where(x => x.Status == ProductModel.States.Clearance || x.Status == ProductModel.States.Promotion);
|
2022-10-30 12:17:10 -04:00
|
|
|
|
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() + " ";
|
2022-10-21 17:52:25 -04:00
|
|
|
|
if (sTitle.StartsWith(query))
|
|
|
|
|
products.Add(prod);
|
2022-10-30 16:58:47 -04:00
|
|
|
|
else if (sTitle.Contains(" " + query))
|
2022-10-21 17:52:25 -04:00
|
|
|
|
title.Add(prod);
|
2022-10-30 16:58:47 -04:00
|
|
|
|
else if (sDesc.Contains(" " + query))
|
2022-10-21 17:52:25 -04:00
|
|
|
|
desc.Add(prod);
|
2022-10-30 16:58:47 -04:00
|
|
|
|
else if (sCat.Contains(" " + query))
|
2022-10-21 17:52:25 -04:00
|
|
|
|
cat.Add(prod);
|
|
|
|
|
}
|
|
|
|
|
products.AddRange(title);
|
|
|
|
|
products.AddRange(desc);
|
|
|
|
|
products.AddRange(cat);
|
2022-10-30 12:17:10 -04:00
|
|
|
|
break;
|
|
|
|
|
case "Price":
|
2022-11-01 13:33:08 -04:00
|
|
|
|
ret = ret.OrderBy(x => x.Status == ProductModel.States.Promotion || x.Status == ProductModel.States.Clearance ? x.PromoPrice : x.Price);
|
2022-10-30 12:17:10 -04:00
|
|
|
|
goto default;
|
|
|
|
|
case "PriceDesc":
|
2022-11-01 13:33:08 -04:00
|
|
|
|
ret = ret.OrderByDescending(x => x.Status == ProductModel.States.Promotion || x.Status == ProductModel.States.Clearance ? x.PromoPrice : x.Price);
|
2022-10-30 12:17:10 -04:00
|
|
|
|
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-11-02 12:40:09 -04:00
|
|
|
|
}
|