Woohoo
This commit is contained in:
@@ -17,12 +17,12 @@ public class InventoryController : Controller {
|
||||
}
|
||||
|
||||
[HttpGet(Name = "Inventory")] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant)
|
||||
public IEnumerable<Product> Get(int? lastId, string order, string filter) {
|
||||
const uint AMOUNT = 5;
|
||||
public IEnumerable<Product> Get(int? lastId, string? order, string? filterPrice, string? filterState) {
|
||||
const int AMOUNT = 5;
|
||||
|
||||
var ret = _context.Products.AsQueryable();
|
||||
|
||||
switch (filter) {
|
||||
switch (filterPrice) {
|
||||
case "PriceUnder20":
|
||||
ret = ret.Where(x => x.Price < 20);
|
||||
break;
|
||||
@@ -35,17 +35,24 @@ public class InventoryController : Controller {
|
||||
case "PriceOver100":
|
||||
ret = ret.Where(x => x.Price >= 100);
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
switch (filterState) {
|
||||
case "isAvailable":
|
||||
ret = ret.Where(x => x.Quantity > 0);
|
||||
ret = ret.Where(x => x.Status == Product.States.Available);
|
||||
break;
|
||||
case "isUnavailable":
|
||||
ret = ret.Where(x => x.Status == Product.States.Unavailable);
|
||||
break;
|
||||
case "isBackOrder":
|
||||
ret = ret.Where(x => x.Quantity == 0 || !x.isDiscontinued);
|
||||
ret = ret.Where(x => x.Status == Product.States.BackOrder);
|
||||
break; ;
|
||||
case "isClearance":
|
||||
ret = ret.Where(x => x.Quantity > 0 && x.isDiscontinued);
|
||||
ret = ret.Where(x => x.Status == Product.States.Clearance);
|
||||
break;
|
||||
case "isDiscontinued":
|
||||
ret = ret.Where(x => x.Quantity == 0 && x.isDiscontinued);
|
||||
ret = ret.Where(x => x.Status == Product.States.Discontinued);
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
@@ -73,14 +80,12 @@ public class InventoryController : Controller {
|
||||
}
|
||||
if (order is not null && order.Contains("Desc")) {
|
||||
if (!lastId.HasValue)
|
||||
lastId = _context.Products.Max(x => x.Id);
|
||||
ret = ret.Where(x => x.Id < lastId && x.Id > lastId - AMOUNT);
|
||||
} else {
|
||||
lastId = _context.Products.Max(x => x.Id) + 1;
|
||||
} else
|
||||
if (!lastId.HasValue)
|
||||
lastId = 1;
|
||||
ret = ret.Where(x => x.Id > lastId && x.Id < lastId + AMOUNT);
|
||||
}
|
||||
return ret.ToList();
|
||||
lastId = _context.Products.Min(x => x.Id) - 1;
|
||||
|
||||
return ret.Where(x => x.Id > lastId).Take(AMOUNT).ToList();
|
||||
}
|
||||
|
||||
// Inventory/Delete => Décrémenter un produit.
|
||||
@@ -93,13 +98,19 @@ public class InventoryController : Controller {
|
||||
|
||||
try {
|
||||
Product prod = _context.Products.First(x => x.Id == id);
|
||||
if (prod.Quantity > 0)
|
||||
prod.Quantity--;
|
||||
if (prod.Quantity > 0) {
|
||||
prod.Quantity = prod.Quantity - 1;
|
||||
if (prod.Quantity == 0)
|
||||
prod.Status = prod.Status == Product.States.Clearance?
|
||||
Product.States.Discontinued:
|
||||
Product.States.Unavailable;
|
||||
}
|
||||
else {
|
||||
_logger.LogError(8, "Delete de produit en backorder.");
|
||||
_logger.LogError(8, "Vente de produit pas en stock.");
|
||||
return;
|
||||
}
|
||||
_context.Products.Update(prod);
|
||||
_context.SaveChanges();
|
||||
} catch (Exception e) {
|
||||
_logger.LogError(8, e.Message);
|
||||
}
|
||||
|
Reference in New Issue
Block a user