GGMM/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs

107 lines
3.7 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;
namespace GrossesMitainesAPI.Controllers;
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-16 10:33:16 -04:00
[HttpGet(Name = "Inventory")] // Pour faire des calls async par paquet de 5 (pour du loading en scrollant)
public IEnumerable<Product> Get(int? lastId, string order, string filter) {
2022-10-08 16:05:23 -04:00
if (!lastId.HasValue)
lastId = 1;
2022-10-16 10:33:16 -04:00
var ret = _context.Products.AsQueryable();
switch (filter) {
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;
case "isAvailable":
ret = ret.Where(x => x.Quantity > 0);
break;
case "isBackOrder":
ret = ret.Where(x => x.Quantity == 0 || !x.isDiscontinued);
break; ;
case "isClearance":
ret = ret.Where(x => x.Quantity > 0 && x.isDiscontinued);
break;
case "isDiscontinued":
ret = ret.Where(x => x.Quantity == 0 && x.isDiscontinued);
break;
}
switch (order) {
case "Price":
ret = ret.OrderBy(x => x.Price)
.Where(x => x.Id > lastId && x.Id < lastId + 5);
break;
case "PriceDesc":
ret = ret.OrderByDescending(x => x.Price)
.Where(x => x.Id < lastId && x.Id > lastId - 5);
break;
case "Title":
ret = ret.OrderBy(x => x.Title)
.Where(x => x.Id > lastId && x.Id < lastId + 5);
break;
case "TitleDesc":
ret = ret.OrderByDescending(x => x.Title)
.Where(x => x.Id < lastId && x.Id > lastId - 5);
break;
case "Category":
ret = ret.OrderBy(x => x.Category)
.Where(x => x.Id > lastId && x.Id < lastId + 5);
break;
case "CategoryDesc":
ret = ret.OrderByDescending(x => x.Category)
.Where(x => x.Id < lastId && x.Id > lastId - 5);
break;
default:
ret = ret.Where(x => x.Id > lastId && x.Id < lastId + 5);
break;
}
return ret.ToList();
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-09 15:07:35 -04:00
[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--;
else {
_logger.LogError(8, "Delete de produit en backorder.");
return;
}
_context.Products.Update(prod);
} catch (Exception e) {
_logger.LogError(8, e.Message);
}
}
2022-10-08 13:22:12 -04:00
}