GGMM/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs

48 lines
1.4 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-09 15:07:35 -04:00
[HttpGet(Name ="Inventory")] // Pour faire des calls async par paquet de 5 (pour du loading en scrollant)
2022-10-08 16:05:23 -04:00
public IEnumerable<Product> Get(int? lastId) {
if (!lastId.HasValue)
lastId = 1;
return _context.Products.Where(x => x.Id >= lastId && x.Id < lastId + 5).ToList();
2022-10-08 13:22:12 -04:00
}
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
}