Continuage d'API
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -29,11 +29,14 @@ public class ProductController : Controller {
|
||||
}
|
||||
|
||||
[HttpPost(Name = "Product")]
|
||||
public void Post(string title, string description, decimal price, string imagename) {
|
||||
public void Post(int id, string title, string category, string description, decimal? price, uint? quantity, bool? disc, string imagename) {
|
||||
Product prod = new() {
|
||||
Title = title,
|
||||
Category = category,
|
||||
Description = description,
|
||||
Price = price,
|
||||
Price = price.HasValue? (decimal)price: 0.01M,
|
||||
Quantity = quantity.HasValue ? (uint)quantity : 0,
|
||||
isDiscontinued = disc.HasValue? (bool)disc: false,
|
||||
ImageName = imagename
|
||||
};
|
||||
try {
|
||||
@@ -44,19 +47,28 @@ public class ProductController : Controller {
|
||||
}
|
||||
|
||||
[HttpPut(Name = "Product")]
|
||||
public void Put(int id, string title, string description, decimal? price, string imagename) {
|
||||
public void Put(int id, string title, string category, string description, decimal? price, uint? quantity, bool? disc, string imagename) {
|
||||
try {
|
||||
Product prod = _context.Products.Where(x => x.Id == id).First();
|
||||
|
||||
if (title != null || title != "")
|
||||
prod.Title = title;
|
||||
|
||||
if (category != null || category != "")
|
||||
prod.Category = category;
|
||||
|
||||
if (description != null || description != "")
|
||||
prod.Description = description;
|
||||
|
||||
if (price.HasValue || price > 0)
|
||||
prod.Price = (decimal)price;
|
||||
|
||||
if (quantity.HasValue)
|
||||
prod.Quantity = (uint)quantity;
|
||||
|
||||
if (disc.HasValue)
|
||||
prod.isDiscontinued = (bool)disc;
|
||||
|
||||
if (imagename != null || imagename != "")
|
||||
prod.ImageName = imagename;
|
||||
|
||||
@@ -67,7 +79,7 @@ public class ProductController : Controller {
|
||||
}
|
||||
|
||||
[HttpPatch(Name = "Product")]
|
||||
public void Patch(int id, string title, string description, decimal? price, string imagename) {
|
||||
public void Patch(int id, string title, string category, string description, decimal? price, uint? quantity, bool? disc, string imagename) {
|
||||
try {
|
||||
Product prod = _context.Products.Where(x => x.Id == id).First();
|
||||
|
||||
@@ -75,6 +87,10 @@ public class ProductController : Controller {
|
||||
prod.Title = title;
|
||||
else prod.Title = "";
|
||||
|
||||
if (category != null)
|
||||
prod.Category = category;
|
||||
else prod.Category = "";
|
||||
|
||||
if (description != null)
|
||||
prod.Description = description;
|
||||
else prod.Description = "";
|
||||
@@ -83,6 +99,14 @@ public class ProductController : Controller {
|
||||
prod.Price = (decimal)price;
|
||||
else prod.Price = 0.01M;
|
||||
|
||||
if (quantity.HasValue)
|
||||
prod.Quantity = (uint)quantity;
|
||||
else prod.Quantity = 0;
|
||||
|
||||
if (disc.HasValue)
|
||||
prod.isDiscontinued = (bool)disc;
|
||||
else prod.isDiscontinued = false;
|
||||
|
||||
if (imagename != null)
|
||||
prod.ImageName = imagename;
|
||||
else prod.ImageName = "";
|
||||
|
Reference in New Issue
Block a user