This commit is contained in:
MarcEricMartel
2022-10-16 08:25:21 -07:00
parent 0cc291b2a8
commit dbc1c78f07
9 changed files with 111 additions and 56 deletions

View File

@@ -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);
}

View File

@@ -37,18 +37,18 @@ public class ProductController : Controller {
Description = description,
Price = price.HasValue? (decimal)price: 0.01M,
Quantity = quantity.HasValue ? (uint)quantity : 0,
isDiscontinued = disc.HasValue? (bool)disc: false,
ImageName = imagename
};
try {
_context.Products.Add(prod);
_context.SaveChanges();
} catch (Exception e) {
_logger.LogError(8, e.Message);
}
}
[HttpPut(Name = "Product")]
public void Put(int id, string title, string category, string description, decimal? price, uint? quantity, bool? disc, string imagename) {
public void Put(int id, string title, string category, string description, decimal? price, uint? quantity, string? status, string imagename) {
try {
Product prod = _context.Products.Where(x => x.Id == id).First();
@@ -67,20 +67,37 @@ public class ProductController : Controller {
if (quantity.HasValue)
prod.Quantity = (uint)quantity;
if (disc.HasValue)
prod.isDiscontinued = (bool)disc;
switch (status) {
case "isAvailable":
prod.Status = Product.States.Available;
break;
case "isUnavailable":
prod.Status = Product.States.Unavailable;
break;
case "isBackOrder":
prod.Status = Product.States.BackOrder;
break; ;
case "isClearance":
prod.Status = Product.States.Clearance;
break;
case "isDiscontinued":
prod.Status = Product.States.Discontinued;
break;
default: break;
}
if (imagename != null || imagename != "")
prod.ImageName = imagename;
_context.Products.Update(prod);
_context.SaveChanges();
} catch (Exception e) {
_logger.LogError(8, e.Message);
}
}
[HttpPatch(Name = "Product")]
public void Patch(int id, string title, string category, string description, decimal? price, uint? quantity, bool? disc, string imagename) {
public void Patch(int id, string title, string category, string description, decimal? price, uint? quantity, string? status, string imagename) {
try {
Product prod = _context.Products.Where(x => x.Id == id).First();
@@ -104,15 +121,33 @@ public class ProductController : Controller {
prod.Quantity = (uint)quantity;
else prod.Quantity = 0;
if (disc.HasValue)
prod.isDiscontinued = (bool)disc;
else prod.isDiscontinued = false;
switch (status) {
case "isAvailable":
prod.Status = Product.States.Available;
break;
case "isUnavailable":
prod.Status = Product.States.Unavailable;
break;
case "isBackOrder":
prod.Status = Product.States.BackOrder;
break; ;
case "isClearance":
prod.Status = Product.States.Clearance;
break;
case "isDiscontinued":
prod.Status = Product.States.Discontinued;
break;
default:
prod.Status = prod.Quantity > 0 ? Product.States.Available : Product.States.Unavailable;
break;
}
if (imagename != null)
prod.ImageName = imagename;
else prod.ImageName = "";
_context.Products.Update(prod);
_context.SaveChanges();
} catch (Exception e) {
_logger.LogError(8, e.Message);
}

View File

@@ -3,6 +3,7 @@ using GrossesMitainesAPI.Models;
using System.Linq;
using GrossesMitainesAPI.Data;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;
namespace GrossesMitainesAPI.Controllers;