140 lines
5.1 KiB
C#
140 lines
5.1 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.Cors;
|
|
using GrossesMitainesAPI.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.Extensions.Hosting;
|
|
#endregion
|
|
|
|
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"),
|
|
Authorize(AuthenticationSchemes = "Identity.Application")]
|
|
public class InventoryController : Controller {
|
|
#region Constants
|
|
private const int AMOUNT_SCROLL = 6;
|
|
|
|
#endregion
|
|
|
|
#region DI Fields
|
|
private readonly ILogger<InventoryController> _logger;
|
|
private readonly InventoryContext _context;
|
|
private readonly DatabaseCacheService _cache;
|
|
private readonly IWebHostEnvironment _hostEnvironment;
|
|
|
|
#endregion
|
|
|
|
#region Ctor
|
|
public InventoryController(ILogger<InventoryController> logger, InventoryContext context, DatabaseCacheService cache, IWebHostEnvironment hostEnvironment) {
|
|
_context = context;
|
|
_logger = logger;
|
|
_cache = cache;
|
|
_hostEnvironment = hostEnvironment;
|
|
}
|
|
|
|
#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<ProductViewModel> ret;
|
|
if (_cache.isOk()) {
|
|
ret = _cache.queryCache().Select(x => new ProductViewModel(x));
|
|
iscache = true;
|
|
}
|
|
else ret = _context.Products.AsQueryable().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 == ProductModel.States.Available);
|
|
break;
|
|
case "isUnavailable":
|
|
ret = ret.Where(x => x.Status == ProductModel.States.Unavailable);
|
|
break;
|
|
case "isBackOrder":
|
|
ret = ret.Where(x => x.Status == ProductModel.States.BackOrder);
|
|
break; ;
|
|
case "isClearance":
|
|
ret = ret.Where(x => x.Status == ProductModel.States.Clearance);
|
|
break;
|
|
case "isDiscontinued":
|
|
ret = ret.Where(x => x.Status == ProductModel.States.Discontinued);
|
|
break;
|
|
case "isPromoted":
|
|
ret = ret.Where(x => x.Status == ProductModel.States.Clearance ||
|
|
x.Status == ProductModel.States.Promotion);
|
|
break;
|
|
default: break;
|
|
}
|
|
switch (order) {
|
|
case "Price":
|
|
ret = ret.OrderBy(x => x.Status == ProductModel.States.Promotion ||
|
|
x.Status == ProductModel.States.Clearance ?
|
|
x.PromoPrice : x.Price);
|
|
break;
|
|
case "PriceDesc":
|
|
ret = ret.OrderByDescending(x => x.Status == ProductModel.States.Promotion ||
|
|
x.Status == ProductModel.States.Clearance ?
|
|
x.PromoPrice : x.Price);
|
|
break;
|
|
case "Title":
|
|
ret = ret.OrderBy(x => x.Title);
|
|
break;
|
|
case "TitleDesc":
|
|
ret = ret.OrderByDescending(x => x.Title);
|
|
break;
|
|
case "Category":
|
|
ret = ret.OrderBy(x => x.Category);
|
|
break;
|
|
case "CategoryDesc":
|
|
ret = ret.OrderByDescending(x => x.Category);
|
|
break;
|
|
default: break;
|
|
}
|
|
List<ProductViewModel> lst = new();
|
|
bool yup = false;
|
|
int add = 0;
|
|
try {
|
|
if (!lastId.HasValue || lastId == 0)
|
|
yup = true;
|
|
|
|
foreach (ProductViewModel prod in ret.ToList()) {
|
|
if (yup && add < AMOUNT_SCROLL || (all.HasValue && all == true)) {
|
|
|
|
lst.Add(prod);
|
|
add++;
|
|
}
|
|
if (!yup && prod.Id == lastId)
|
|
yup = true;
|
|
}
|
|
return lst;
|
|
}
|
|
catch (Exception e) {
|
|
if (iscache)
|
|
_logger.LogError(e, "Erreur d'appel de cache.");
|
|
else _logger.LogError(e, "Erreur d'appel d'API.");
|
|
return new List<ProductViewModel>();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
} |