Search upgraded
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
namespace GrossesMitainesAPI.Controllers;
|
||||
|
||||
#region Dependencies
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using GrossesMitainesAPI.Models;
|
||||
using System.Linq;
|
||||
using GrossesMitainesAPI.Data;
|
||||
@@ -7,30 +10,41 @@ using Microsoft.AspNetCore.Cors;
|
||||
using GrossesMitainesAPI.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
namespace GrossesMitainesAPI.Controllers;
|
||||
#endregion
|
||||
|
||||
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]")]
|
||||
public class InventoryController : Controller {
|
||||
#region Constants
|
||||
private const int AMOUNT_SCROLL = 5;
|
||||
|
||||
#endregion
|
||||
|
||||
#region DI Fields
|
||||
private readonly ILogger<InventoryController> _logger;
|
||||
private readonly InventoryContext _context;
|
||||
private readonly DatabaseCacheService _cache;
|
||||
private const int AMOUNT_SCROLL = 5;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Ctor
|
||||
public InventoryController(ILogger<InventoryController> logger, InventoryContext context, DatabaseCacheService cache) {
|
||||
_context = context;
|
||||
_logger = logger;
|
||||
_cache = cache;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region API Methods
|
||||
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Inventory"), AllowAnonymous] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant)
|
||||
public IEnumerable<ProductViewModel> Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) {
|
||||
bool iscache = false;
|
||||
IQueryable<Product> ret;
|
||||
IQueryable<ProductViewModel> ret;
|
||||
if (_cache.isOk()) {
|
||||
ret = _cache.queryCache();
|
||||
ret = _cache.queryCache().Select(x => new ProductViewModel(x));
|
||||
iscache = true;
|
||||
}
|
||||
else ret = _context.Products.AsQueryable();
|
||||
else ret = _context.Products.AsQueryable().Select(x => new ProductViewModel(x));
|
||||
switch (filterPrice) {
|
||||
case "PriceUnder20":
|
||||
ret = ret.Where(x => x.Price < 20);
|
||||
@@ -63,16 +77,21 @@ public class InventoryController : Controller {
|
||||
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);
|
||||
ret = ret.Where(x => x.Status == Product.States.Clearance ||
|
||||
x.Status == Product.States.Promotion);
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
switch (order) {
|
||||
case "Price":
|
||||
ret = ret.OrderBy(x => x.Status == Product.States.Promotion || x.Status == Product.States.Clearance ? x.PromoPrice : x.Price);
|
||||
ret = ret.OrderBy(x => x.Status == Product.States.Promotion ||
|
||||
x.Status == Product.States.Clearance ?
|
||||
x.PromoPrice : x.Price);
|
||||
break;
|
||||
case "PriceDesc":
|
||||
ret = ret.OrderByDescending(x => x.Status == Product.States.Promotion || x.Status == Product.States.Clearance ? x.PromoPrice : x.Price);
|
||||
ret = ret.OrderByDescending(x => x.Status == Product.States.Promotion ||
|
||||
x.Status == Product.States.Clearance ?
|
||||
x.PromoPrice : x.Price);
|
||||
break;
|
||||
case "Title":
|
||||
ret = ret.OrderBy(x => x.Title);
|
||||
@@ -95,9 +114,9 @@ public class InventoryController : Controller {
|
||||
if (!lastId.HasValue || lastId == 0)
|
||||
yup = true;
|
||||
|
||||
foreach (Product prod in ret.ToList()) {
|
||||
foreach (ProductViewModel prod in ret.ToList()) {
|
||||
if (yup && add < AMOUNT_SCROLL || (all.HasValue && all == true)) {
|
||||
lst.Add(new ProductViewModel(prod));
|
||||
lst.Add(prod);
|
||||
add++;
|
||||
}
|
||||
if (!yup && prod.Id == lastId)
|
||||
@@ -143,5 +162,7 @@ public class InventoryController : Controller {
|
||||
_cache.askForRefresh();
|
||||
return rid;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user