48 lines
1.4 KiB
C#
48 lines
1.4 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) {
|
|
if (!lastId.HasValue)
|
|
lastId = 1;
|
|
return _context.Products.Where(x => x.Id >= lastId && x.Id < lastId + 5).ToList();
|
|
}
|
|
|
|
[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);
|
|
}
|
|
}
|
|
}
|
|
|