Correction

This commit is contained in:
MarcEricMartel
2022-11-01 11:07:49 -07:00
parent d20f2f3615
commit 4eb8bb7353
6 changed files with 108 additions and 51 deletions

View File

@@ -132,37 +132,37 @@ public class InventoryController : Controller {
}
}
// Inventory/Delete => Décrémenter un produit. Va aller chercher directement dans la BD.
[EnableCors("_myAllowSpecificOrigins"), HttpDelete(Name = "Inventory"), AllowAnonymous]
public ActionResult<int> Delete(int? id) {
int rid = 0;
if (!id.HasValue) {
_logger.LogError(8, "Tentative de vente sans Id.");
return BadRequest();
}
try {
ProductModel prod = _context.Products.First(x => x.Id == id);
rid = prod.Id;
if (prod.Quantity > 0) {
prod.Quantity = prod.Quantity - 1;
prod.Sales = prod.Sales + 1;
prod.LastSale = DateTime.Now;
if (prod.Quantity == 0)
prod.Status = prod.Status == ProductModel.States.Clearance ?
ProductModel.States.Discontinued :
ProductModel.States.BackOrder;
} else {
_logger.LogError(8, $"Vente de produit pas en stock. Id Produit: {prod.Id}");
return BadRequest();
}
_context.Products.Update(prod);
_context.SaveChanges();
} catch (Exception e) {
_logger.LogError(8, e.Message);
return BadRequest();
}
_cache.askForRefresh();
return rid;
}
//[EnableCors("_myAllowSpecificOrigins"), HttpDelete(Name = "Inventory"), AllowAnonymous]
//public ActionResult<int> Delete(int? id) {
// int rid = 0;
// if (!id.HasValue) {
// _logger.LogError(8, "Tentative de vente sans Id.");
// return BadRequest();
// }
// try {
// ProductModel prod = _context.Products.First(x => x.Id == id);
// rid = prod.Id;
// if (prod.Quantity > 0) {
// prod.Quantity = prod.Quantity - 1;
// prod.Sales = prod.Sales + 1;
// prod.LastSale = DateTime.Now;
// if (prod.Quantity == 0)
// prod.Status = prod.Status == ProductModel.States.Clearance ?
// ProductModel.States.Discontinued :
// ProductModel.States.BackOrder;
// } else {
// _logger.LogError(8, $"Vente de produit pas en stock. Id Produit: {prod.Id}");
// return BadRequest();
// }
// _context.Products.Update(prod);
// _context.SaveChanges();
// } catch (Exception e) {
// _logger.LogError(8, e.Message);
// return BadRequest();
// }
// _cache.askForRefresh();
// return rid;
//}
#endregion
}