GGMM/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs
2022-10-21 15:07:11 -07:00

147 lines
5.1 KiB
C#

using Microsoft.AspNetCore.Mvc;
using GrossesMitainesAPI.Models;
using System.Linq;
using GrossesMitainesAPI.Data;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Cors;
using GrossesMitainesAPI.Services;
namespace GrossesMitainesAPI.Controllers;
[EnableCors("_myAllowSpecificOrigins")]
[ApiController, Route("api/[controller]")]
public class InventoryController : Controller {
private readonly ILogger<InventoryController> _logger;
private readonly InventoryContext _context;
private readonly DatabaseCacheService _cache;
public InventoryController(ILogger<InventoryController> logger, InventoryContext context, DatabaseCacheService cache) {
_context = context;
_logger = logger;
_cache = cache;
}
[EnableCors("_myAllowSpecificOrigins")]
[HttpGet(Name = "Inventory")] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant)
public IEnumerable<Product> Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) {
const int AMOUNT = 5;
IQueryable<Product> ret;
if (_cache.isOk())
ret = _cache.GetCacheCopy().AsQueryable();
else ret = _context.Products.AsQueryable();
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;
default: break;
}
switch (order) {
case "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);
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<Product> lst = new();
bool yup = false;
int add = 0;
if (lastId.HasValue)
foreach (Product prod in new List<Product>(ret.ToList())) {
if (yup && add < AMOUNT) {
lst.Add(prod);
add++;
}
if (prod.Id == lastId)
yup = true;
}
else if (all.HasValue && all == true)
lst = ret.ToList();
else lst = ret.Take(AMOUNT).ToList();
return lst;
}
// Inventory/Delete => Décrémenter un produit. Va aller chercher directement dans la BD.
[EnableCors("_myAllowSpecificOrigins")]
[HttpDelete(Name = "Inventory")]
public ActionResult<int> Delete(int? id) {
int rid = 0;
if (!id.HasValue) {
_logger.LogError(8, "Delete sans Id.");
return BadRequest();
}
try {
Product prod = _context.Products.First(x => x.Id == id);
rid = prod.Id;
if (prod.Quantity > 0) {
prod.Quantity = prod.Quantity - 1;
if (prod.Quantity == 0)
prod.Status = prod.Status == Product.States.Clearance?
Product.States.Discontinued:
Product.States.BackOrder;
}
else {
_logger.LogError(8, "Vente de produit pas en stock.");
return BadRequest();
}
_context.Products.Update(prod);
_context.SaveChanges();
} catch (Exception e) {
_logger.LogError(8, e.Message);
return BadRequest();
}
_cache.askForRefresh();
return rid;
}
}