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) [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) { public IEnumerable<Product> Get(int? lastId, string? order, string? filterPrice, string? filterState) {
const uint AMOUNT = 5; const int AMOUNT = 5;
var ret = _context.Products.AsQueryable(); var ret = _context.Products.AsQueryable();
switch (filter) { switch (filterPrice) {
case "PriceUnder20": case "PriceUnder20":
ret = ret.Where(x => x.Price < 20); ret = ret.Where(x => x.Price < 20);
break; break;
@ -35,17 +35,24 @@ public class InventoryController : Controller {
case "PriceOver100": case "PriceOver100":
ret = ret.Where(x => x.Price >= 100); ret = ret.Where(x => x.Price >= 100);
break; break;
default: break;
}
switch (filterState) {
case "isAvailable": 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; break;
case "isBackOrder": case "isBackOrder":
ret = ret.Where(x => x.Quantity == 0 || !x.isDiscontinued); ret = ret.Where(x => x.Status == Product.States.BackOrder);
break; ; break; ;
case "isClearance": case "isClearance":
ret = ret.Where(x => x.Quantity > 0 && x.isDiscontinued); ret = ret.Where(x => x.Status == Product.States.Clearance);
break; break;
case "isDiscontinued": case "isDiscontinued":
ret = ret.Where(x => x.Quantity == 0 && x.isDiscontinued); ret = ret.Where(x => x.Status == Product.States.Discontinued);
break; break;
default: break; default: break;
} }
@ -73,14 +80,12 @@ public class InventoryController : Controller {
} }
if (order is not null && order.Contains("Desc")) { if (order is not null && order.Contains("Desc")) {
if (!lastId.HasValue) if (!lastId.HasValue)
lastId = _context.Products.Max(x => x.Id); lastId = _context.Products.Max(x => x.Id) + 1;
ret = ret.Where(x => x.Id < lastId && x.Id > lastId - AMOUNT); } else
} else {
if (!lastId.HasValue) if (!lastId.HasValue)
lastId = 1; lastId = _context.Products.Min(x => x.Id) - 1;
ret = ret.Where(x => x.Id > lastId && x.Id < lastId + AMOUNT);
} return ret.Where(x => x.Id > lastId).Take(AMOUNT).ToList();
return ret.ToList();
} }
// Inventory/Delete => Décrémenter un produit. // Inventory/Delete => Décrémenter un produit.
@ -93,13 +98,19 @@ public class InventoryController : Controller {
try { try {
Product prod = _context.Products.First(x => x.Id == id); Product prod = _context.Products.First(x => x.Id == id);
if (prod.Quantity > 0) if (prod.Quantity > 0) {
prod.Quantity--; prod.Quantity = prod.Quantity - 1;
if (prod.Quantity == 0)
prod.Status = prod.Status == Product.States.Clearance?
Product.States.Discontinued:
Product.States.Unavailable;
}
else { else {
_logger.LogError(8, "Delete de produit en backorder."); _logger.LogError(8, "Vente de produit pas en stock.");
return; return;
} }
_context.Products.Update(prod); _context.Products.Update(prod);
_context.SaveChanges();
} catch (Exception e) { } catch (Exception e) {
_logger.LogError(8, e.Message); _logger.LogError(8, e.Message);
} }

View File

@ -37,18 +37,18 @@ public class ProductController : Controller {
Description = description, Description = description,
Price = price.HasValue? (decimal)price: 0.01M, Price = price.HasValue? (decimal)price: 0.01M,
Quantity = quantity.HasValue ? (uint)quantity : 0, Quantity = quantity.HasValue ? (uint)quantity : 0,
isDiscontinued = disc.HasValue? (bool)disc: false,
ImageName = imagename ImageName = imagename
}; };
try { try {
_context.Products.Add(prod); _context.Products.Add(prod);
_context.SaveChanges();
} catch (Exception e) { } catch (Exception e) {
_logger.LogError(8, e.Message); _logger.LogError(8, e.Message);
} }
} }
[HttpPut(Name = "Product")] [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 { try {
Product prod = _context.Products.Where(x => x.Id == id).First(); Product prod = _context.Products.Where(x => x.Id == id).First();
@ -67,20 +67,37 @@ public class ProductController : Controller {
if (quantity.HasValue) if (quantity.HasValue)
prod.Quantity = (uint)quantity; prod.Quantity = (uint)quantity;
if (disc.HasValue) switch (status) {
prod.isDiscontinued = (bool)disc; 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 != "") if (imagename != null || imagename != "")
prod.ImageName = imagename; prod.ImageName = imagename;
_context.Products.Update(prod); _context.Products.Update(prod);
_context.SaveChanges();
} catch (Exception e) { } catch (Exception e) {
_logger.LogError(8, e.Message); _logger.LogError(8, e.Message);
} }
} }
[HttpPatch(Name = "Product")] [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 { try {
Product prod = _context.Products.Where(x => x.Id == id).First(); Product prod = _context.Products.Where(x => x.Id == id).First();
@ -104,15 +121,33 @@ public class ProductController : Controller {
prod.Quantity = (uint)quantity; prod.Quantity = (uint)quantity;
else prod.Quantity = 0; else prod.Quantity = 0;
if (disc.HasValue) switch (status) {
prod.isDiscontinued = (bool)disc; case "isAvailable":
else prod.isDiscontinued = false; 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) if (imagename != null)
prod.ImageName = imagename; prod.ImageName = imagename;
else prod.ImageName = ""; else prod.ImageName = "";
_context.Products.Update(prod); _context.Products.Update(prod);
_context.SaveChanges();
} catch (Exception e) { } catch (Exception e) {
_logger.LogError(8, e.Message); _logger.LogError(8, e.Message);
} }

View File

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

View File

@ -16,9 +16,9 @@ public class InventoryContext : DbContext {
Title = $"Ceinture flèchée", Title = $"Ceinture flèchée",
Category = $"Linge", Category = $"Linge",
Description = $"Pour faire votre propre bonhomme de 1837, comme dans le bon vieux temps.", Description = $"Pour faire votre propre bonhomme de 1837, comme dans le bon vieux temps.",
Status = Product.States.Promotion,
Price = 85.86M, Price = 85.86M,
Quantity = 1, Quantity = 1,
isDiscontinued = false,
ImageName = $"ceintureflechee" ImageName = $"ceintureflechee"
}); });
modelBuilder.Entity<Product>().HasData(new Product { modelBuilder.Entity<Product>().HasData(new Product {
@ -26,9 +26,9 @@ public class InventoryContext : DbContext {
Title = $"Pantoufles du Canadien en Phentex", Title = $"Pantoufles du Canadien en Phentex",
Category = $"Linge", Category = $"Linge",
Description = $"Parce que ça sent la coupe!", Description = $"Parce que ça sent la coupe!",
Status = Product.States.Available,
Price = 15.64M, Price = 15.64M,
Quantity = 54, Quantity = 54,
isDiscontinued = false,
ImageName = $"pantouflesCH" ImageName = $"pantouflesCH"
}); });
modelBuilder.Entity<Product>().HasData(new Product { modelBuilder.Entity<Product>().HasData(new Product {
@ -36,9 +36,9 @@ public class InventoryContext : DbContext {
Title = $"Jean-Luc Mongrain", Title = $"Jean-Luc Mongrain",
Category = $"Homme", Category = $"Homme",
Description = $"On ne lui ferait pas mal, en tout cas!!", Description = $"On ne lui ferait pas mal, en tout cas!!",
Status = Product.States.Clearance,
Price = 1453.12M, Price = 1453.12M,
Quantity = 1, Quantity = 1,
isDiscontinued = true,
ImageName = $"jeanlucmongrain" ImageName = $"jeanlucmongrain"
}); });
} }

View File

@ -11,7 +11,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace GrossesMitainesAPI.Migrations namespace GrossesMitainesAPI.Migrations
{ {
[DbContext(typeof(InventoryContext))] [DbContext(typeof(InventoryContext))]
[Migration("20221009190720_Initial-Db")] [Migration("20221016150710_Initial-Db")]
partial class InitialDb partial class InitialDb
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -48,14 +48,14 @@ namespace GrossesMitainesAPI.Migrations
b.Property<long>("Quantity") b.Property<long>("Quantity")
.HasColumnType("bigint"); .HasColumnType("bigint");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<string>("Title") b.Property<string>("Title")
.IsRequired() .IsRequired()
.HasMaxLength(255) .HasMaxLength(255)
.HasColumnType("nvarchar(255)"); .HasColumnType("nvarchar(255)");
b.Property<bool>("isDiscontinued")
.HasColumnType("bit");
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("Products"); b.ToTable("Products");
@ -69,8 +69,8 @@ namespace GrossesMitainesAPI.Migrations
ImageName = "ceintureflechee", ImageName = "ceintureflechee",
Price = 85.86m, Price = 85.86m,
Quantity = 1L, Quantity = 1L,
Title = "Ceinture flèchée", Status = 4,
isDiscontinued = false Title = "Ceinture flèchée"
}, },
new new
{ {
@ -80,8 +80,8 @@ namespace GrossesMitainesAPI.Migrations
ImageName = "pantouflesCH", ImageName = "pantouflesCH",
Price = 15.64m, Price = 15.64m,
Quantity = 54L, Quantity = 54L,
Title = "Pantoufles du Canadien en Phentex", Status = 0,
isDiscontinued = false Title = "Pantoufles du Canadien en Phentex"
}, },
new new
{ {
@ -91,8 +91,8 @@ namespace GrossesMitainesAPI.Migrations
ImageName = "jeanlucmongrain", ImageName = "jeanlucmongrain",
Price = 1453.12m, Price = 1453.12m,
Quantity = 1L, Quantity = 1L,
Title = "Jean-Luc Mongrain", Status = 3,
isDiscontinued = true Title = "Jean-Luc Mongrain"
}); });
}); });
#pragma warning restore 612, 618 #pragma warning restore 612, 618

View File

@ -19,7 +19,7 @@ namespace GrossesMitainesAPI.Migrations
Description = table.Column<string>(type: "nvarchar(max)", nullable: false), Description = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<decimal>(type: "decimal(18,2)", nullable: false), Price = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
Quantity = table.Column<long>(type: "bigint", nullable: false), Quantity = table.Column<long>(type: "bigint", nullable: false),
isDiscontinued = table.Column<bool>(type: "bit", nullable: false), Status = table.Column<int>(type: "int", nullable: false),
ImageName = table.Column<string>(type: "nvarchar(max)", nullable: true) ImageName = table.Column<string>(type: "nvarchar(max)", nullable: true)
}, },
constraints: table => constraints: table =>
@ -29,18 +29,18 @@ namespace GrossesMitainesAPI.Migrations
migrationBuilder.InsertData( migrationBuilder.InsertData(
table: "Products", table: "Products",
columns: new[] { "Id", "Category", "Description", "ImageName", "Price", "Quantity", "Title", "isDiscontinued" }, columns: new[] { "Id", "Category", "Description", "ImageName", "Price", "Quantity", "Status", "Title" },
values: new object[] { 1, "Linge", "Pour faire votre propre bonhomme de 1837, comme dans le bon vieux temps.", "ceintureflechee", 85.86m, 1L, "Ceinture flèchée", false }); values: new object[] { 1, "Linge", "Pour faire votre propre bonhomme de 1837, comme dans le bon vieux temps.", "ceintureflechee", 85.86m, 1L, 4, "Ceinture flèchée" });
migrationBuilder.InsertData( migrationBuilder.InsertData(
table: "Products", table: "Products",
columns: new[] { "Id", "Category", "Description", "ImageName", "Price", "Quantity", "Title", "isDiscontinued" }, columns: new[] { "Id", "Category", "Description", "ImageName", "Price", "Quantity", "Status", "Title" },
values: new object[] { 2, "Linge", "Parce que ça sent la coupe!", "pantouflesCH", 15.64m, 54L, "Pantoufles du Canadien en Phentex", false }); values: new object[] { 2, "Linge", "Parce que ça sent la coupe!", "pantouflesCH", 15.64m, 54L, 0, "Pantoufles du Canadien en Phentex" });
migrationBuilder.InsertData( migrationBuilder.InsertData(
table: "Products", table: "Products",
columns: new[] { "Id", "Category", "Description", "ImageName", "Price", "Quantity", "Title", "isDiscontinued" }, columns: new[] { "Id", "Category", "Description", "ImageName", "Price", "Quantity", "Status", "Title" },
values: new object[] { 3, "Homme", "On ne lui ferait pas mal, en tout cas!!", "jeanlucmongrain", 1453.12m, 1L, "Jean-Luc Mongrain", true }); values: new object[] { 3, "Homme", "On ne lui ferait pas mal, en tout cas!!", "jeanlucmongrain", 1453.12m, 1L, 3, "Jean-Luc Mongrain" });
} }
protected override void Down(MigrationBuilder migrationBuilder) protected override void Down(MigrationBuilder migrationBuilder)

View File

@ -46,14 +46,14 @@ namespace GrossesMitainesAPI.Migrations
b.Property<long>("Quantity") b.Property<long>("Quantity")
.HasColumnType("bigint"); .HasColumnType("bigint");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<string>("Title") b.Property<string>("Title")
.IsRequired() .IsRequired()
.HasMaxLength(255) .HasMaxLength(255)
.HasColumnType("nvarchar(255)"); .HasColumnType("nvarchar(255)");
b.Property<bool>("isDiscontinued")
.HasColumnType("bit");
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("Products"); b.ToTable("Products");
@ -67,8 +67,8 @@ namespace GrossesMitainesAPI.Migrations
ImageName = "ceintureflechee", ImageName = "ceintureflechee",
Price = 85.86m, Price = 85.86m,
Quantity = 1L, Quantity = 1L,
Title = "Ceinture flèchée", Status = 4,
isDiscontinued = false Title = "Ceinture flèchée"
}, },
new new
{ {
@ -78,8 +78,8 @@ namespace GrossesMitainesAPI.Migrations
ImageName = "pantouflesCH", ImageName = "pantouflesCH",
Price = 15.64m, Price = 15.64m,
Quantity = 54L, Quantity = 54L,
Title = "Pantoufles du Canadien en Phentex", Status = 0,
isDiscontinued = false Title = "Pantoufles du Canadien en Phentex"
}, },
new new
{ {
@ -89,8 +89,8 @@ namespace GrossesMitainesAPI.Migrations
ImageName = "jeanlucmongrain", ImageName = "jeanlucmongrain",
Price = 1453.12m, Price = 1453.12m,
Quantity = 1L, Quantity = 1L,
Title = "Jean-Luc Mongrain", Status = 3,
isDiscontinued = true Title = "Jean-Luc Mongrain"
}); });
}); });
#pragma warning restore 612, 618 #pragma warning restore 612, 618

View File

@ -6,6 +6,14 @@ namespace GrossesMitainesAPI.Models;
// catégories, description, quantité disponible, images, prix normal et // catégories, description, quantité disponible, images, prix normal et
// autres informations pertinentes // autres informations pertinentes
public class Product { public class Product {
public enum States {
Available,
BackOrder,
Unavailable,
Clearance,
Promotion,
Discontinued
}
[Key] [Key]
public int Id { get; set; } public int Id { get; set; }
[Required, MaxLength(255)] [Required, MaxLength(255)]
@ -17,6 +25,6 @@ public class Product {
[Required, Range(0.01, (double)decimal.MaxValue)] // Range qui prend pas les decimals! [Required, Range(0.01, (double)decimal.MaxValue)] // Range qui prend pas les decimals!
public decimal Price { get; set; } = 0; public decimal Price { get; set; } = 0;
public uint Quantity { get; set; } = 0; public uint Quantity { get; set; } = 0;
public bool isDiscontinued { get; set; } = false; public States Status { get; set; } = States.Available;
public string? ImageName { get; set; } // Base pour sortir les images ({ImageName}.jpg , {ImageName}_thumbnail.jpg, etc...) public string? ImageName { get; set; } // Base pour sortir les images ({ImageName}.jpg , {ImageName}_thumbnail.jpg, etc...)
} }

View File

@ -7,6 +7,6 @@
}, },
"AllowedHosts": "*", "AllowedHosts": "*",
"ConnectionStrings": { "ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb; Database=GrossesMitainesDB; Trusted_Connection=True; MultipleActiveResultSets=true" "DefaultConnection": "Server=(localdb)\\mssqllocaldb; Database=GrossesMitainesDB0; Trusted_Connection=True; MultipleActiveResultSets=true"
} }
} }