GGMM/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs
MarcEricMartel dbc1c78f07 Woohoo
2022-10-16 08:25:21 -07:00

120 lines
4.1 KiB
C#

using Microsoft.AspNetCore.Mvc;
using GrossesMitainesAPI.Models;
using System.Linq;
using GrossesMitainesAPI.Data;
using Microsoft.Extensions.Logging;
namespace GrossesMitainesAPI.Controllers;
[ApiController, Route("api/[controller]")]
public class InventoryController : Controller {
private readonly ILogger<InventoryController> _logger;
private readonly InventoryContext _context;
public InventoryController(ILogger<InventoryController> logger, InventoryContext context) {
_context = context;
_logger = logger;
}
[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) {
const int AMOUNT = 5;
var 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.Price);
break;
case "PriceDesc":
ret = ret.OrderByDescending(x => 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;
}
if (order is not null && order.Contains("Desc")) {
if (!lastId.HasValue)
lastId = _context.Products.Max(x => x.Id) + 1;
} else
if (!lastId.HasValue)
lastId = _context.Products.Min(x => x.Id) - 1;
return ret.Where(x => x.Id > lastId).Take(AMOUNT).ToList();
}
// Inventory/Delete => Décrémenter un produit.
[HttpDelete(Name = "Inventory")]
public void Delete(int? id) {
if (!id.HasValue) {
_logger.LogError(8, "Delete sans Id.");
return;
}
try {
Product prod = _context.Products.First(x => x.Id == 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.Unavailable;
}
else {
_logger.LogError(8, "Vente de produit pas en stock.");
return;
}
_context.Products.Update(prod);
_context.SaveChanges();
} catch (Exception e) {
_logger.LogError(8, e.Message);
}
}
}