diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs index c18992d..9c34b3c 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs @@ -8,17 +8,40 @@ namespace GrossesMitainesAPI.Controllers; [ApiController, Route("api/[controller]")] public class InventoryController : Controller { + private readonly ILogger _logger; private readonly InventoryContext _context; - public InventoryController(InventoryContext context) { + public InventoryController(ILogger logger, InventoryContext context) { _context = context; + _logger = logger; } - [HttpGet(Name ="Inventory")] + [HttpGet(Name ="Inventory")] // Pour faire des calls async par paquet de 5 (pour du loading en scrollant) public IEnumerable Get(int? lastId) { if (!lastId.HasValue) lastId = 1; return _context.Products.Where(x => x.Id >= lastId && x.Id < lastId + 5).ToList(); } + + [HttpDelete(Name = "Inventory")] + public void Delete(int? id) { + if (!id.HasValue) { + _logger.LogError(8, "Delete sans Id."); + return; + } + + try { + Product prod = _context.Products.First(x => x.Id == id); + if (prod.Quantity > 0) + prod.Quantity--; + else { + _logger.LogError(8, "Delete de produit en backorder."); + return; + } + _context.Products.Update(prod); + } catch (Exception e) { + _logger.LogError(8, e.Message); + } + } } diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs index c61899b..27d6b18 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs @@ -29,11 +29,14 @@ public class ProductController : Controller { } [HttpPost(Name = "Product")] - public void Post(string title, string description, decimal price, string imagename) { + public void Post(int id, string title, string category, string description, decimal? price, uint? quantity, bool? disc, string imagename) { Product prod = new() { Title = title, + Category = category, Description = description, - Price = price, + Price = price.HasValue? (decimal)price: 0.01M, + Quantity = quantity.HasValue ? (uint)quantity : 0, + isDiscontinued = disc.HasValue? (bool)disc: false, ImageName = imagename }; try { @@ -44,19 +47,28 @@ public class ProductController : Controller { } [HttpPut(Name = "Product")] - public void Put(int id, string title, string description, decimal? price, string imagename) { + public void Put(int id, string title, string category, string description, decimal? price, uint? quantity, bool? disc, string imagename) { try { Product prod = _context.Products.Where(x => x.Id == id).First(); if (title != null || title != "") prod.Title = title; + if (category != null || category != "") + prod.Category = category; + if (description != null || description != "") prod.Description = description; if (price.HasValue || price > 0) prod.Price = (decimal)price; + if (quantity.HasValue) + prod.Quantity = (uint)quantity; + + if (disc.HasValue) + prod.isDiscontinued = (bool)disc; + if (imagename != null || imagename != "") prod.ImageName = imagename; @@ -67,7 +79,7 @@ public class ProductController : Controller { } [HttpPatch(Name = "Product")] - public void Patch(int id, string title, string description, decimal? price, string imagename) { + public void Patch(int id, string title, string category, string description, decimal? price, uint? quantity, bool? disc, string imagename) { try { Product prod = _context.Products.Where(x => x.Id == id).First(); @@ -75,6 +87,10 @@ public class ProductController : Controller { prod.Title = title; else prod.Title = ""; + if (category != null) + prod.Category = category; + else prod.Category = ""; + if (description != null) prod.Description = description; else prod.Description = ""; @@ -83,6 +99,14 @@ public class ProductController : Controller { prod.Price = (decimal)price; else prod.Price = 0.01M; + if (quantity.HasValue) + prod.Quantity = (uint)quantity; + else prod.Quantity = 0; + + if (disc.HasValue) + prod.isDiscontinued = (bool)disc; + else prod.isDiscontinued = false; + if (imagename != null) prod.ImageName = imagename; else prod.ImageName = ""; diff --git a/GrossesMitaines/GrossesMitainesAPI/Data/InventoryContext.cs b/GrossesMitaines/GrossesMitainesAPI/Data/InventoryContext.cs index 87bc80e..9916206 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Data/InventoryContext.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Data/InventoryContext.cs @@ -14,22 +14,31 @@ public class InventoryContext : DbContext { modelBuilder.Entity().HasData(new Product { Id = 1, Title = $"Ceinture flèchée", + Category = $"Linge", Description = $"Pour faire votre propre bonhomme de 1837, comme dans le bon vieux temps.", Price = 85.86M, + Quantity = 1, + isDiscontinued = false, ImageName = $"ceintureflechee" }); modelBuilder.Entity().HasData(new Product { Id = 2, Title = $"Pantoufles du Canadien en Phentex", + Category = $"Linge", Description = $"Parce que ça sent la coupe!", Price = 15.64M, + Quantity = 54, + isDiscontinued = false, ImageName = $"pantouflesCH" }); modelBuilder.Entity().HasData(new Product { Id = 3, Title = $"Jean-Luc Mongrain", + Category = $"Homme", Description = $"On ne lui ferait pas mal, en tout cas!!", Price = 1453.12M, + Quantity = 1, + isDiscontinued = true, ImageName = $"jeanlucmongrain" }); } diff --git a/GrossesMitaines/GrossesMitainesAPI/Migrations/20221009165926_Initial-Db.Designer.cs b/GrossesMitaines/GrossesMitainesAPI/Migrations/20221009190720_Initial-Db.Designer.cs similarity index 75% rename from GrossesMitaines/GrossesMitainesAPI/Migrations/20221009165926_Initial-Db.Designer.cs rename to GrossesMitaines/GrossesMitainesAPI/Migrations/20221009190720_Initial-Db.Designer.cs index a7f3eba..f9bffa1 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Migrations/20221009165926_Initial-Db.Designer.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Migrations/20221009190720_Initial-Db.Designer.cs @@ -11,7 +11,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace GrossesMitainesAPI.Migrations { [DbContext(typeof(InventoryContext))] - [Migration("20221009165926_Initial-Db")] + [Migration("20221009190720_Initial-Db")] partial class InitialDb { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -31,6 +31,10 @@ namespace GrossesMitainesAPI.Migrations SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + b.Property("Category") + .IsRequired() + .HasColumnType("nvarchar(max)"); + b.Property("Description") .IsRequired() .HasColumnType("nvarchar(max)"); @@ -41,11 +45,17 @@ namespace GrossesMitainesAPI.Migrations b.Property("Price") .HasColumnType("decimal(18,2)"); + b.Property("Quantity") + .HasColumnType("bigint"); + b.Property("Title") .IsRequired() .HasMaxLength(255) .HasColumnType("nvarchar(255)"); + b.Property("isDiscontinued") + .HasColumnType("bit"); + b.HasKey("Id"); b.ToTable("Products"); @@ -54,26 +64,35 @@ namespace GrossesMitainesAPI.Migrations new { Id = 1, + Category = "Linge", Description = "Pour faire votre propre bonhomme de 1837, comme dans le bon vieux temps.", ImageName = "ceintureflechee", Price = 85.86m, - Title = "Ceinture flèchée" + Quantity = 1L, + Title = "Ceinture flèchée", + isDiscontinued = false }, new { Id = 2, + Category = "Linge", Description = "Parce que ça sent la coupe!", ImageName = "pantouflesCH", Price = 15.64m, - Title = "Pantoufles du Canadien en Phentex" + Quantity = 54L, + Title = "Pantoufles du Canadien en Phentex", + isDiscontinued = false }, new { Id = 3, + Category = "Homme", Description = "On ne lui ferait pas mal, en tout cas!!", ImageName = "jeanlucmongrain", Price = 1453.12m, - Title = "Jean-Luc Mongrain" + Quantity = 1L, + Title = "Jean-Luc Mongrain", + isDiscontinued = true }); }); #pragma warning restore 612, 618 diff --git a/GrossesMitaines/GrossesMitainesAPI/Migrations/20221009165926_Initial-Db.cs b/GrossesMitaines/GrossesMitainesAPI/Migrations/20221009190720_Initial-Db.cs similarity index 56% rename from GrossesMitaines/GrossesMitainesAPI/Migrations/20221009165926_Initial-Db.cs rename to GrossesMitaines/GrossesMitainesAPI/Migrations/20221009190720_Initial-Db.cs index 2ec1ee3..3f92185 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Migrations/20221009165926_Initial-Db.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Migrations/20221009190720_Initial-Db.cs @@ -15,8 +15,11 @@ namespace GrossesMitainesAPI.Migrations Id = table.Column(type: "int", nullable: false) .Annotation("SqlServer:Identity", "1, 1"), Title = table.Column(type: "nvarchar(255)", maxLength: 255, nullable: false), + Category = table.Column(type: "nvarchar(max)", nullable: false), 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), ImageName = table.Column(type: "nvarchar(max)", nullable: true) }, constraints: table => @@ -26,18 +29,18 @@ namespace GrossesMitainesAPI.Migrations migrationBuilder.InsertData( table: "Products", - columns: new[] { "Id", "Description", "ImageName", "Price", "Title" }, - values: new object[] { 1, "Pour faire votre propre bonhomme de 1837, comme dans le bon vieux temps.", "ceintureflechee", 85.86m, "Ceinture flèchée" }); + 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 }); migrationBuilder.InsertData( table: "Products", - columns: new[] { "Id", "Description", "ImageName", "Price", "Title" }, - values: new object[] { 2, "Parce que ça sent la coupe!", "pantouflesCH", 15.64m, "Pantoufles du Canadien en Phentex" }); + 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 }); migrationBuilder.InsertData( table: "Products", - columns: new[] { "Id", "Description", "ImageName", "Price", "Title" }, - values: new object[] { 3, "On ne lui ferait pas mal, en tout cas!!", "jeanlucmongrain", 1453.12m, "Jean-Luc Mongrain" }); + 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 }); } protected override void Down(MigrationBuilder migrationBuilder) diff --git a/GrossesMitaines/GrossesMitainesAPI/Migrations/InventoryContextModelSnapshot.cs b/GrossesMitaines/GrossesMitainesAPI/Migrations/InventoryContextModelSnapshot.cs index baa7a96..a33e0c5 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Migrations/InventoryContextModelSnapshot.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Migrations/InventoryContextModelSnapshot.cs @@ -29,6 +29,10 @@ namespace GrossesMitainesAPI.Migrations SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + b.Property("Category") + .IsRequired() + .HasColumnType("nvarchar(max)"); + b.Property("Description") .IsRequired() .HasColumnType("nvarchar(max)"); @@ -39,11 +43,17 @@ namespace GrossesMitainesAPI.Migrations b.Property("Price") .HasColumnType("decimal(18,2)"); + b.Property("Quantity") + .HasColumnType("bigint"); + b.Property("Title") .IsRequired() .HasMaxLength(255) .HasColumnType("nvarchar(255)"); + b.Property("isDiscontinued") + .HasColumnType("bit"); + b.HasKey("Id"); b.ToTable("Products"); @@ -52,26 +62,35 @@ namespace GrossesMitainesAPI.Migrations new { Id = 1, + Category = "Linge", Description = "Pour faire votre propre bonhomme de 1837, comme dans le bon vieux temps.", ImageName = "ceintureflechee", Price = 85.86m, - Title = "Ceinture flèchée" + Quantity = 1L, + Title = "Ceinture flèchée", + isDiscontinued = false }, new { Id = 2, + Category = "Linge", Description = "Parce que ça sent la coupe!", ImageName = "pantouflesCH", Price = 15.64m, - Title = "Pantoufles du Canadien en Phentex" + Quantity = 54L, + Title = "Pantoufles du Canadien en Phentex", + isDiscontinued = false }, new { Id = 3, + Category = "Homme", Description = "On ne lui ferait pas mal, en tout cas!!", ImageName = "jeanlucmongrain", Price = 1453.12m, - Title = "Jean-Luc Mongrain" + Quantity = 1L, + Title = "Jean-Luc Mongrain", + isDiscontinued = true }); }); #pragma warning restore 612, 618 diff --git a/GrossesMitaines/GrossesMitainesAPI/Models/Product.cs b/GrossesMitaines/GrossesMitainesAPI/Models/Product.cs index ecd439a..2635bf2 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Models/Product.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Models/Product.cs @@ -17,5 +17,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 string? ImageName { get; set; } // Base pour sortir les images ({ImageName}.jpg , {ImageName}_thumbnail.jpg, etc...) } \ No newline at end of file