diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs index 98760ad..3809681 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs @@ -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 Get(int? lastId, string order, string filter) { - const uint AMOUNT = 5; + public IEnumerable 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); } diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs index 61f4e8e..4fd403a 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs @@ -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); } diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs index 8c0cfb1..65abd4e 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs @@ -3,6 +3,7 @@ using GrossesMitainesAPI.Models; using System.Linq; using GrossesMitainesAPI.Data; using Microsoft.Extensions.Logging; +using Microsoft.AspNetCore.Authorization; namespace GrossesMitainesAPI.Controllers; diff --git a/GrossesMitaines/GrossesMitainesAPI/Data/InventoryContext.cs b/GrossesMitaines/GrossesMitainesAPI/Data/InventoryContext.cs index 9916206..8bd92ba 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Data/InventoryContext.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Data/InventoryContext.cs @@ -16,9 +16,9 @@ public class InventoryContext : DbContext { Title = $"Ceinture flèchée", Category = $"Linge", Description = $"Pour faire votre propre bonhomme de 1837, comme dans le bon vieux temps.", + Status = Product.States.Promotion, Price = 85.86M, Quantity = 1, - isDiscontinued = false, ImageName = $"ceintureflechee" }); modelBuilder.Entity().HasData(new Product { @@ -26,9 +26,9 @@ public class InventoryContext : DbContext { Title = $"Pantoufles du Canadien en Phentex", Category = $"Linge", Description = $"Parce que ça sent la coupe!", + Status = Product.States.Available, Price = 15.64M, Quantity = 54, - isDiscontinued = false, ImageName = $"pantouflesCH" }); modelBuilder.Entity().HasData(new Product { @@ -36,9 +36,9 @@ public class InventoryContext : DbContext { Title = $"Jean-Luc Mongrain", Category = $"Homme", Description = $"On ne lui ferait pas mal, en tout cas!!", + Status = Product.States.Clearance, Price = 1453.12M, Quantity = 1, - isDiscontinued = true, ImageName = $"jeanlucmongrain" }); } diff --git a/GrossesMitaines/GrossesMitainesAPI/Migrations/20221009190720_Initial-Db.Designer.cs b/GrossesMitaines/GrossesMitainesAPI/Migrations/20221016150710_Initial-Db.Designer.cs similarity index 88% rename from GrossesMitaines/GrossesMitainesAPI/Migrations/20221009190720_Initial-Db.Designer.cs rename to GrossesMitaines/GrossesMitainesAPI/Migrations/20221016150710_Initial-Db.Designer.cs index f9bffa1..228c8dd 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Migrations/20221009190720_Initial-Db.Designer.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Migrations/20221016150710_Initial-Db.Designer.cs @@ -11,7 +11,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace GrossesMitainesAPI.Migrations { [DbContext(typeof(InventoryContext))] - [Migration("20221009190720_Initial-Db")] + [Migration("20221016150710_Initial-Db")] partial class InitialDb { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -48,14 +48,14 @@ namespace GrossesMitainesAPI.Migrations b.Property("Quantity") .HasColumnType("bigint"); + b.Property("Status") + .HasColumnType("int"); + b.Property("Title") .IsRequired() .HasMaxLength(255) .HasColumnType("nvarchar(255)"); - b.Property("isDiscontinued") - .HasColumnType("bit"); - b.HasKey("Id"); b.ToTable("Products"); @@ -69,8 +69,8 @@ namespace GrossesMitainesAPI.Migrations ImageName = "ceintureflechee", Price = 85.86m, Quantity = 1L, - Title = "Ceinture flèchée", - isDiscontinued = false + Status = 4, + Title = "Ceinture flèchée" }, new { @@ -80,8 +80,8 @@ namespace GrossesMitainesAPI.Migrations ImageName = "pantouflesCH", Price = 15.64m, Quantity = 54L, - Title = "Pantoufles du Canadien en Phentex", - isDiscontinued = false + Status = 0, + Title = "Pantoufles du Canadien en Phentex" }, new { @@ -91,8 +91,8 @@ namespace GrossesMitainesAPI.Migrations ImageName = "jeanlucmongrain", Price = 1453.12m, Quantity = 1L, - Title = "Jean-Luc Mongrain", - isDiscontinued = true + Status = 3, + Title = "Jean-Luc Mongrain" }); }); #pragma warning restore 612, 618 diff --git a/GrossesMitaines/GrossesMitainesAPI/Migrations/20221009190720_Initial-Db.cs b/GrossesMitaines/GrossesMitainesAPI/Migrations/20221016150710_Initial-Db.cs similarity index 79% rename from GrossesMitaines/GrossesMitainesAPI/Migrations/20221009190720_Initial-Db.cs rename to GrossesMitaines/GrossesMitainesAPI/Migrations/20221016150710_Initial-Db.cs index 3f92185..1293d3f 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Migrations/20221009190720_Initial-Db.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Migrations/20221016150710_Initial-Db.cs @@ -19,7 +19,7 @@ namespace GrossesMitainesAPI.Migrations Description = table.Column(type: "nvarchar(max)", nullable: false), Price = table.Column(type: "decimal(18,2)", nullable: false), Quantity = table.Column(type: "bigint", nullable: false), - isDiscontinued = table.Column(type: "bit", nullable: false), + Status = table.Column(type: "int", nullable: false), ImageName = table.Column(type: "nvarchar(max)", nullable: true) }, constraints: table => @@ -29,18 +29,18 @@ namespace GrossesMitainesAPI.Migrations migrationBuilder.InsertData( table: "Products", - columns: new[] { "Id", "Category", "Description", "ImageName", "Price", "Quantity", "Title", "isDiscontinued" }, - 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 }); + 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, 4, "Ceinture flèchée" }); migrationBuilder.InsertData( table: "Products", - columns: new[] { "Id", "Category", "Description", "ImageName", "Price", "Quantity", "Title", "isDiscontinued" }, - values: new object[] { 2, "Linge", "Parce que ça sent la coupe!", "pantouflesCH", 15.64m, 54L, "Pantoufles du Canadien en Phentex", false }); + 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, 0, "Pantoufles du Canadien en Phentex" }); migrationBuilder.InsertData( table: "Products", - columns: new[] { "Id", "Category", "Description", "ImageName", "Price", "Quantity", "Title", "isDiscontinued" }, - values: new object[] { 3, "Homme", "On ne lui ferait pas mal, en tout cas!!", "jeanlucmongrain", 1453.12m, 1L, "Jean-Luc Mongrain", true }); + 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, 3, "Jean-Luc Mongrain" }); } protected override void Down(MigrationBuilder migrationBuilder) diff --git a/GrossesMitaines/GrossesMitainesAPI/Migrations/InventoryContextModelSnapshot.cs b/GrossesMitaines/GrossesMitainesAPI/Migrations/InventoryContextModelSnapshot.cs index a33e0c5..3fad637 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Migrations/InventoryContextModelSnapshot.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Migrations/InventoryContextModelSnapshot.cs @@ -46,14 +46,14 @@ namespace GrossesMitainesAPI.Migrations b.Property("Quantity") .HasColumnType("bigint"); + b.Property("Status") + .HasColumnType("int"); + b.Property("Title") .IsRequired() .HasMaxLength(255) .HasColumnType("nvarchar(255)"); - b.Property("isDiscontinued") - .HasColumnType("bit"); - b.HasKey("Id"); b.ToTable("Products"); @@ -67,8 +67,8 @@ namespace GrossesMitainesAPI.Migrations ImageName = "ceintureflechee", Price = 85.86m, Quantity = 1L, - Title = "Ceinture flèchée", - isDiscontinued = false + Status = 4, + Title = "Ceinture flèchée" }, new { @@ -78,8 +78,8 @@ namespace GrossesMitainesAPI.Migrations ImageName = "pantouflesCH", Price = 15.64m, Quantity = 54L, - Title = "Pantoufles du Canadien en Phentex", - isDiscontinued = false + Status = 0, + Title = "Pantoufles du Canadien en Phentex" }, new { @@ -89,8 +89,8 @@ namespace GrossesMitainesAPI.Migrations ImageName = "jeanlucmongrain", Price = 1453.12m, Quantity = 1L, - Title = "Jean-Luc Mongrain", - isDiscontinued = true + Status = 3, + Title = "Jean-Luc Mongrain" }); }); #pragma warning restore 612, 618 diff --git a/GrossesMitaines/GrossesMitainesAPI/Models/Product.cs b/GrossesMitaines/GrossesMitainesAPI/Models/Product.cs index 2635bf2..363d64d 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Models/Product.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Models/Product.cs @@ -6,6 +6,14 @@ namespace GrossesMitainesAPI.Models; // catégories, description, quantité disponible, images, prix normal et // autres informations pertinentes public class Product { + public enum States { + Available, + BackOrder, + Unavailable, + Clearance, + Promotion, + Discontinued + } [Key] public int Id { get; set; } [Required, MaxLength(255)] @@ -17,6 +25,6 @@ public class Product { [Required, Range(0.01, (double)decimal.MaxValue)] // Range qui prend pas les decimals! public decimal Price { 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...) } \ No newline at end of file diff --git a/GrossesMitaines/GrossesMitainesAPI/appsettings.json b/GrossesMitaines/GrossesMitainesAPI/appsettings.json index 84d6132..c4178a3 100644 --- a/GrossesMitaines/GrossesMitainesAPI/appsettings.json +++ b/GrossesMitaines/GrossesMitainesAPI/appsettings.json @@ -7,6 +7,6 @@ }, "AllowedHosts": "*", "ConnectionStrings": { - "DefaultConnection": "Server=(localdb)\\mssqllocaldb; Database=GrossesMitainesDB; Trusted_Connection=True; MultipleActiveResultSets=true" + "DefaultConnection": "Server=(localdb)\\mssqllocaldb; Database=GrossesMitainesDB0; Trusted_Connection=True; MultipleActiveResultSets=true" } }