GGMM/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs

140 lines
5.1 KiB
C#
Raw Permalink Normal View History

2022-10-30 12:17:10 -04:00
namespace GrossesMitainesAPI.Controllers;
#region Dependencies
using Microsoft.AspNetCore.Mvc;
2022-10-08 13:22:12 -04:00
using GrossesMitainesAPI.Models;
using System.Linq;
using GrossesMitainesAPI.Data;
using Microsoft.Extensions.Logging;
2022-10-18 10:08:23 -04:00
using Microsoft.AspNetCore.Cors;
using GrossesMitainesAPI.Services;
2022-10-27 12:37:13 -04:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Hosting;
2022-10-30 12:17:10 -04:00
#endregion
2022-10-08 13:22:12 -04:00
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"),
2022-10-30 16:58:47 -04:00
Authorize(AuthenticationSchemes = "Identity.Application")]
2022-10-08 13:22:12 -04:00
public class InventoryController : Controller {
2022-10-30 12:17:10 -04:00
#region Constants
2022-10-31 10:12:25 -04:00
private const int AMOUNT_SCROLL = 6;
2022-10-30 12:17:10 -04:00
#endregion
#region DI Fields
2022-10-09 15:07:35 -04:00
private readonly ILogger<InventoryController> _logger;
2022-10-08 13:22:12 -04:00
private readonly InventoryContext _context;
private readonly DatabaseCacheService _cache;
private readonly IWebHostEnvironment _hostEnvironment;
2022-10-25 11:57:41 -04:00
2022-10-30 12:17:10 -04:00
#endregion
#region Ctor
public InventoryController(ILogger<InventoryController> logger, InventoryContext context, DatabaseCacheService cache, IWebHostEnvironment hostEnvironment) {
2022-10-08 13:22:12 -04:00
_context = context;
2022-10-09 15:07:35 -04:00
_logger = logger;
_cache = cache;
_hostEnvironment = hostEnvironment;
2022-10-08 13:22:12 -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 = "Inventory"), AllowAnonymous] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant)
2022-10-27 14:32:06 -04:00
public IEnumerable<ProductViewModel> Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) {
bool iscache = false;
2022-10-30 12:17:10 -04:00
IQueryable<ProductViewModel> ret;
if (_cache.isOk()) {
2022-10-30 12:17:10 -04:00
ret = _cache.queryCache().Select(x => new ProductViewModel(x));
2022-10-27 14:32:06 -04:00
iscache = true;
2022-10-25 10:54:11 -04:00
}
2022-10-30 12:17:10 -04:00
else ret = _context.Products.AsQueryable().Select(x => new ProductViewModel(x));
2022-10-16 11:25:21 -04:00
switch (filterPrice) {
2022-10-16 10:33:16 -04:00
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;
2022-10-16 11:25:21 -04:00
default: break;
}
switch (filterState) {
2022-10-16 10:33:16 -04:00
case "isAvailable":
ret = ret.Where(x => x.Status == ProductModel.States.Available);
2022-10-16 11:25:21 -04:00
break;
case "isUnavailable":
ret = ret.Where(x => x.Status == ProductModel.States.Unavailable);
2022-10-16 10:33:16 -04:00
break;
case "isBackOrder":
ret = ret.Where(x => x.Status == ProductModel.States.BackOrder);
2022-10-16 10:33:16 -04:00
break; ;
case "isClearance":
ret = ret.Where(x => x.Status == ProductModel.States.Clearance);
2022-10-16 10:33:16 -04:00
break;
case "isDiscontinued":
ret = ret.Where(x => x.Status == ProductModel.States.Discontinued);
2022-10-16 10:33:16 -04:00
break;
case "isPromoted":
ret = ret.Where(x => x.Status == ProductModel.States.Clearance ||
x.Status == ProductModel.States.Promotion);
break;
2022-10-16 10:45:18 -04:00
default: break;
2022-10-16 10:33:16 -04:00
}
switch (order) {
case "Price":
ret = ret.OrderBy(x => x.Status == ProductModel.States.Promotion ||
x.Status == ProductModel.States.Clearance ?
2022-10-30 12:17:10 -04:00
x.PromoPrice : x.Price);
2022-10-16 10:33:16 -04:00
break;
case "PriceDesc":
ret = ret.OrderByDescending(x => x.Status == ProductModel.States.Promotion ||
x.Status == ProductModel.States.Clearance ?
2022-10-30 12:17:10 -04:00
x.PromoPrice : x.Price);
2022-10-16 10:33:16 -04:00
break;
case "Title":
2022-10-16 10:45:18 -04:00
ret = ret.OrderBy(x => x.Title);
2022-10-16 10:33:16 -04:00
break;
case "TitleDesc":
2022-10-16 10:45:18 -04:00
ret = ret.OrderByDescending(x => x.Title);
2022-10-16 10:33:16 -04:00
break;
case "Category":
2022-10-16 10:45:18 -04:00
ret = ret.OrderBy(x => x.Category);
2022-10-16 10:33:16 -04:00
break;
case "CategoryDesc":
2022-10-16 10:45:18 -04:00
ret = ret.OrderByDescending(x => x.Category);
2022-10-16 10:33:16 -04:00
break;
2022-10-16 10:45:18 -04:00
default: break;
2022-10-16 10:33:16 -04:00
}
2022-10-27 14:32:06 -04:00
List<ProductViewModel> lst = new();
2022-10-18 10:43:52 -04:00
bool yup = false;
int add = 0;
2022-10-25 10:54:11 -04:00
try {
2022-10-27 14:32:06 -04:00
if (!lastId.HasValue || lastId == 0)
yup = true;
2022-10-30 12:17:10 -04:00
foreach (ProductViewModel prod in ret.ToList()) {
2022-10-27 14:32:06 -04:00
if (yup && add < AMOUNT_SCROLL || (all.HasValue && all == true)) {
2022-10-30 12:17:10 -04:00
lst.Add(prod);
2022-10-27 14:32:06 -04:00
add++;
2022-10-18 10:43:52 -04:00
}
2022-10-27 14:32:06 -04:00
if (!yup && prod.Id == lastId)
yup = true;
}
2022-10-25 10:54:11 -04:00
return lst;
}
catch (Exception e) {
2022-10-27 14:32:06 -04:00
if (iscache)
2022-10-25 10:54:11 -04:00
_logger.LogError(e, "Erreur d'appel de cache.");
else _logger.LogError(e, "Erreur d'appel d'API.");
2022-10-27 14:32:06 -04:00
return new List<ProductViewModel>();
2022-10-25 10:54:11 -04:00
}
2022-10-08 13:22:12 -04:00
}
2022-10-30 12:17:10 -04:00
#endregion
2022-11-02 12:40:09 -04:00
}