Continuage d'API

This commit is contained in:
MarcEricMartel
2022-10-09 12:07:35 -07:00
parent f5351340d8
commit 1f7c97949d
7 changed files with 117 additions and 19 deletions

View File

@@ -8,17 +8,40 @@ namespace GrossesMitainesAPI.Controllers;
[ApiController, Route("api/[controller]")]
public class InventoryController : Controller {
private readonly ILogger<InventoryController> _logger;
private readonly InventoryContext _context;
public InventoryController(InventoryContext context) {
public InventoryController(ILogger<InventoryController> logger, InventoryContext context) {
_context = context;
_logger = logger;
}
[HttpGet(Name ="Inventory")]
[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);
}
}
}