GGMM/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs

139 lines
4.8 KiB
C#
Raw Normal View History

2022-10-08 13:22:12 -04:00
using Microsoft.AspNetCore.Mvc;
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;
2022-10-08 13:22:12 -04:00
namespace GrossesMitainesAPI.Controllers;
2022-10-18 10:08:23 -04:00
[EnableCors("_myAllowSpecificOrigins")]
2022-10-08 15:04:29 -04:00
[ApiController, Route("api/[controller]")]
2022-10-08 13:22:12 -04:00
public class InventoryController : Controller {
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;
2022-10-09 15:07:35 -04:00
public InventoryController(ILogger<InventoryController> logger, InventoryContext context) {
2022-10-08 13:22:12 -04:00
_context = context;
2022-10-09 15:07:35 -04:00
_logger = logger;
2022-10-08 13:22:12 -04:00
}
2022-10-18 10:08:23 -04:00
[EnableCors("_myAllowSpecificOrigins")]
2022-10-16 10:45:18 -04:00
[HttpGet(Name = "Inventory")] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant)
2022-10-18 13:25:08 -04:00
public IEnumerable<Product> Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) {
2022-10-16 11:25:21 -04:00
const int AMOUNT = 5;
2022-10-16 10:45:18 -04:00
2022-10-16 10:33:16 -04:00
var ret = _context.Products.AsQueryable();
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":
2022-10-16 11:25:21 -04:00
ret = ret.Where(x => x.Status == Product.States.Available);
break;
case "isUnavailable":
ret = ret.Where(x => x.Status == Product.States.Unavailable);
2022-10-16 10:33:16 -04:00
break;
case "isBackOrder":
2022-10-16 11:25:21 -04:00
ret = ret.Where(x => x.Status == Product.States.BackOrder);
2022-10-16 10:33:16 -04:00
break; ;
case "isClearance":
2022-10-16 11:25:21 -04:00
ret = ret.Where(x => x.Status == Product.States.Clearance);
2022-10-16 10:33:16 -04:00
break;
case "isDiscontinued":
2022-10-16 11:25:21 -04:00
ret = ret.Where(x => x.Status == Product.States.Discontinued);
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
}
switch (order) {
case "Price":
ret = ret.OrderBy(x => x.Status == Product.States.Promotion || x.Status == Product.States.Clearance? x.PromoPrice: x.Price);
2022-10-16 10:33:16 -04:00
break;
case "PriceDesc":
ret = ret.OrderByDescending(x => x.Status == Product.States.Promotion || x.Status == Product.States.Clearance ? 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-18 10:36:13 -04:00
2022-10-18 10:43:52 -04:00
List<Product> lst = new();
bool yup = false;
int add = 0;
2022-10-18 10:36:13 -04:00
if (lastId.HasValue)
2022-10-18 10:43:52 -04:00
foreach (Product prod in new List<Product>(ret.ToList())) {
if (yup && add < AMOUNT) {
lst.Add(prod);
add++;
}
2022-10-18 10:36:13 -04:00
if (prod.Id == lastId)
2022-10-18 10:43:52 -04:00
yup = true;
2022-10-18 10:36:13 -04:00
}
2022-10-18 13:25:08 -04:00
else if (all.HasValue && all == true)
lst = ret.ToList();
2022-10-18 10:46:01 -04:00
else lst = ret.Take(AMOUNT).ToList();
2022-10-18 10:43:52 -04:00
2022-10-18 10:44:47 -04:00
return lst;
2022-10-08 13:22:12 -04:00
}
2022-10-09 15:07:35 -04:00
2022-10-16 10:33:16 -04:00
// Inventory/Delete => Décrémenter un produit.
2022-10-18 10:08:23 -04:00
[EnableCors("_myAllowSpecificOrigins")]
2022-10-09 15:07:35 -04:00
[HttpDelete(Name = "Inventory")]
2022-10-18 14:53:57 -04:00
public ActionResult<int> Delete(int? id) {
int rid = 0;
2022-10-09 15:07:35 -04:00
if (!id.HasValue) {
_logger.LogError(8, "Delete sans Id.");
2022-10-18 14:53:57 -04:00
return BadRequest();
2022-10-09 15:07:35 -04:00
}
try {
Product prod = _context.Products.First(x => x.Id == id);
2022-10-18 14:53:57 -04:00
rid = prod.Id;
2022-10-16 11:25:21 -04:00
if (prod.Quantity > 0) {
prod.Quantity = prod.Quantity - 1;
if (prod.Quantity == 0)
prod.Status = prod.Status == Product.States.Clearance?
Product.States.Discontinued:
2022-10-18 14:48:34 -04:00
Product.States.BackOrder;
2022-10-16 11:25:21 -04:00
}
2022-10-09 15:07:35 -04:00
else {
2022-10-16 11:25:21 -04:00
_logger.LogError(8, "Vente de produit pas en stock.");
2022-10-18 14:53:57 -04:00
return BadRequest();
2022-10-09 15:07:35 -04:00
}
_context.Products.Update(prod);
2022-10-16 11:25:21 -04:00
_context.SaveChanges();
2022-10-09 15:07:35 -04:00
} catch (Exception e) {
_logger.LogError(8, e.Message);
2022-10-18 14:53:57 -04:00
return BadRequest();
2022-10-09 15:07:35 -04:00
}
2022-10-18 14:53:57 -04:00
return rid;
2022-10-09 15:07:35 -04:00
}
2022-10-08 13:22:12 -04:00
}