114 lines
3.9 KiB
C#
114 lines
3.9 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 5 (pour du loading en scrollant)
|
|
public IEnumerable<Product> Get(int? lastId, string order, string filter) {
|
|
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:
|
|
|
|
break;
|
|
}
|
|
if (order.Contains("Desc")) {
|
|
if (!lastId.HasValue)
|
|
lastId = _context.Products.Max(x => x.Id);
|
|
ret = ret.Where(x => x.Id < lastId && x.Id > lastId - 5);
|
|
} else {
|
|
if (!lastId.HasValue)
|
|
lastId = 1;
|
|
ret = ret.Where(x => x.Id > lastId && x.Id < lastId + 5);
|
|
}
|
|
return ret.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--;
|
|
else {
|
|
_logger.LogError(8, "Delete de produit en backorder.");
|
|
return;
|
|
}
|
|
_context.Products.Update(prod);
|
|
} catch (Exception e) {
|
|
_logger.LogError(8, e.Message);
|
|
}
|
|
}
|
|
}
|
|
|