From 272c2325306688df9062e954169dfe89773fd12c Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Sun, 16 Oct 2022 17:33:58 -0700 Subject: [PATCH] update search and products --- .../Controllers/ProductController.cs | 26 ++++++++++++++----- .../Controllers/SearchController.cs | 12 ++++++--- ... => 20221017003248_Initial-Db.Designer.cs} | 8 +++++- ...ial-Db.cs => 20221017003248_Initial-Db.cs} | 13 +++++----- .../InventoryContextModelSnapshot.cs | 6 +++++ .../GrossesMitainesAPI/Models/Product.cs | 2 ++ .../GrossesMitainesAPI/appsettings.json | 2 +- 7 files changed, 51 insertions(+), 18 deletions(-) rename GrossesMitaines/GrossesMitainesAPI/Migrations/{20221016150710_Initial-Db.Designer.cs => 20221017003248_Initial-Db.Designer.cs} (92%) rename GrossesMitaines/GrossesMitainesAPI/Migrations/{20221016150710_Initial-Db.cs => 20221017003248_Initial-Db.cs} (79%) diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs index 4fd403a..5ceaefa 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs @@ -30,15 +30,20 @@ public class ProductController : Controller { } [HttpPost(Name = "Product")] - public void Post(int id, string title, string category, string description, decimal? price, uint? quantity, bool? disc, string imagename) { + public void Post(string title, string category, string description, decimal? price, decimal? promoprice, uint? quantity, bool? disc, string imagename) { Product prod = new() { Title = title, Category = category, Description = description, Price = price.HasValue? (decimal)price: 0.01M, + PromoPrice = promoprice.HasValue ? (decimal)promoprice : 0.01M, Quantity = quantity.HasValue ? (uint)quantity : 0, ImageName = imagename }; + + if (prod.Price <= prod.PromoPrice) + prod.PromoPrice = prod.Price - 0.01M; + try { _context.Products.Add(prod); _context.SaveChanges(); @@ -48,7 +53,7 @@ public class ProductController : Controller { } [HttpPut(Name = "Product")] - public void Put(int id, string title, string category, string description, decimal? price, uint? quantity, string? status, string imagename) { + public void Put(int id, string title, string category, string description, decimal? price, decimal? promoprice, uint? quantity, string? status, string imagename) { try { Product prod = _context.Products.Where(x => x.Id == id).First(); @@ -64,6 +69,9 @@ public class ProductController : Controller { if (price.HasValue || price > 0) prod.Price = (decimal)price; + if (promoprice.HasValue || promoprice > prod.Price) + prod.PromoPrice = (decimal)promoprice; + if (quantity.HasValue) prod.Quantity = (uint)quantity; @@ -80,6 +88,9 @@ public class ProductController : Controller { case "isClearance": prod.Status = Product.States.Clearance; break; + case "isPromotion": + prod.Status = Product.States.Promotion; + break; case "isDiscontinued": prod.Status = Product.States.Discontinued; break; @@ -97,7 +108,7 @@ public class ProductController : Controller { } [HttpPatch(Name = "Product")] - public void Patch(int id, string title, string category, string description, decimal? price, uint? quantity, string? status, string imagename) { + public void Patch(int id, string title, string category, string description, decimal? price, decimal? promoprice, uint? quantity, string? status, string imagename) { try { Product prod = _context.Products.Where(x => x.Id == id).First(); @@ -113,9 +124,9 @@ public class ProductController : Controller { prod.Description = description; else prod.Description = ""; - if (price.HasValue || price > 0) - prod.Price = (decimal)price; - else prod.Price = 0.01M; + if (promoprice.HasValue || promoprice < prod.Price) + prod.PromoPrice = (decimal)promoprice; + else prod.PromoPrice = prod.Price - 0.01M; if (quantity.HasValue) prod.Quantity = (uint)quantity; @@ -134,6 +145,9 @@ public class ProductController : Controller { case "isClearance": prod.Status = Product.States.Clearance; break; + case "isPromotion": + prod.Status = Product.States.Promotion; + break; case "isDiscontinued": prod.Status = Product.States.Discontinued; break; diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs index 65abd4e..8f14534 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs @@ -18,15 +18,19 @@ public class SearchController : Controller { } [HttpPost(Name = "Search")] - public IEnumerable Post(string query) { + public IEnumerable Post(string query, bool? preview) { HashSet products = new(); query = query.Trim(); try { // Pour faire une liste priorisée. - products.Concat(_context.Products.Where(x => x.Title.Contains(query)).ToHashSet()); - products.Concat(_context.Products.Where(x => x.Category.Contains(query)).ToHashSet()); - products.Concat(_context.Products.Where(x => x.Description.Contains(query)).ToHashSet()); + if (preview.HasValue && preview == true) + products = _context.Products.Where(x => x.Title.Contains(query)).Take(3).ToHashSet(); + else { + products.Concat(_context.Products.Where(x => x.Title.Contains(query)).ToHashSet()); + products.Concat(_context.Products.Where(x => x.Category.Contains(query)).ToHashSet()); + products.Concat(_context.Products.Where(x => x.Description.Contains(query)).ToHashSet()); + } } catch (Exception e) { _logger.LogError(8, e.Message); } diff --git a/GrossesMitaines/GrossesMitainesAPI/Migrations/20221016150710_Initial-Db.Designer.cs b/GrossesMitaines/GrossesMitainesAPI/Migrations/20221017003248_Initial-Db.Designer.cs similarity index 92% rename from GrossesMitaines/GrossesMitainesAPI/Migrations/20221016150710_Initial-Db.Designer.cs rename to GrossesMitaines/GrossesMitainesAPI/Migrations/20221017003248_Initial-Db.Designer.cs index 228c8dd..387e0f0 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Migrations/20221016150710_Initial-Db.Designer.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Migrations/20221017003248_Initial-Db.Designer.cs @@ -11,7 +11,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace GrossesMitainesAPI.Migrations { [DbContext(typeof(InventoryContext))] - [Migration("20221016150710_Initial-Db")] + [Migration("20221017003248_Initial-Db")] partial class InitialDb { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -45,6 +45,9 @@ namespace GrossesMitainesAPI.Migrations b.Property("Price") .HasColumnType("decimal(18,2)"); + b.Property("PromoPrice") + .HasColumnType("decimal(18,2)"); + b.Property("Quantity") .HasColumnType("bigint"); @@ -68,6 +71,7 @@ namespace GrossesMitainesAPI.Migrations Description = "Pour faire votre propre bonhomme de 1837, comme dans le bon vieux temps.", ImageName = "ceintureflechee", Price = 85.86m, + PromoPrice = 0m, Quantity = 1L, Status = 4, Title = "Ceinture flèchée" @@ -79,6 +83,7 @@ namespace GrossesMitainesAPI.Migrations Description = "Parce que ça sent la coupe!", ImageName = "pantouflesCH", Price = 15.64m, + PromoPrice = 0m, Quantity = 54L, Status = 0, Title = "Pantoufles du Canadien en Phentex" @@ -90,6 +95,7 @@ namespace GrossesMitainesAPI.Migrations Description = "On ne lui ferait pas mal, en tout cas!!", ImageName = "jeanlucmongrain", Price = 1453.12m, + PromoPrice = 0m, Quantity = 1L, Status = 3, Title = "Jean-Luc Mongrain" diff --git a/GrossesMitaines/GrossesMitainesAPI/Migrations/20221016150710_Initial-Db.cs b/GrossesMitaines/GrossesMitainesAPI/Migrations/20221017003248_Initial-Db.cs similarity index 79% rename from GrossesMitaines/GrossesMitainesAPI/Migrations/20221016150710_Initial-Db.cs rename to GrossesMitaines/GrossesMitainesAPI/Migrations/20221017003248_Initial-Db.cs index 1293d3f..682ab01 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Migrations/20221016150710_Initial-Db.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Migrations/20221017003248_Initial-Db.cs @@ -18,6 +18,7 @@ namespace GrossesMitainesAPI.Migrations 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), + PromoPrice = table.Column(type: "decimal(18,2)", nullable: false), Quantity = table.Column(type: "bigint", nullable: false), Status = table.Column(type: "int", nullable: false), ImageName = table.Column(type: "nvarchar(max)", nullable: true) @@ -29,18 +30,18 @@ namespace GrossesMitainesAPI.Migrations migrationBuilder.InsertData( table: "Products", - 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" }); + columns: new[] { "Id", "Category", "Description", "ImageName", "Price", "PromoPrice", "Quantity", "Status", "Title" }, + values: new object[] { 1, "Linge", "Pour faire votre propre bonhomme de 1837, comme dans le bon vieux temps.", "ceintureflechee", 85.86m, 0m, 1L, 4, "Ceinture flèchée" }); migrationBuilder.InsertData( table: "Products", - 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" }); + columns: new[] { "Id", "Category", "Description", "ImageName", "Price", "PromoPrice", "Quantity", "Status", "Title" }, + values: new object[] { 2, "Linge", "Parce que ça sent la coupe!", "pantouflesCH", 15.64m, 0m, 54L, 0, "Pantoufles du Canadien en Phentex" }); migrationBuilder.InsertData( table: "Products", - 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" }); + columns: new[] { "Id", "Category", "Description", "ImageName", "Price", "PromoPrice", "Quantity", "Status", "Title" }, + values: new object[] { 3, "Homme", "On ne lui ferait pas mal, en tout cas!!", "jeanlucmongrain", 1453.12m, 0m, 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 3fad637..1d1e47d 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Migrations/InventoryContextModelSnapshot.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Migrations/InventoryContextModelSnapshot.cs @@ -43,6 +43,9 @@ namespace GrossesMitainesAPI.Migrations b.Property("Price") .HasColumnType("decimal(18,2)"); + b.Property("PromoPrice") + .HasColumnType("decimal(18,2)"); + b.Property("Quantity") .HasColumnType("bigint"); @@ -66,6 +69,7 @@ namespace GrossesMitainesAPI.Migrations Description = "Pour faire votre propre bonhomme de 1837, comme dans le bon vieux temps.", ImageName = "ceintureflechee", Price = 85.86m, + PromoPrice = 0m, Quantity = 1L, Status = 4, Title = "Ceinture flèchée" @@ -77,6 +81,7 @@ namespace GrossesMitainesAPI.Migrations Description = "Parce que ça sent la coupe!", ImageName = "pantouflesCH", Price = 15.64m, + PromoPrice = 0m, Quantity = 54L, Status = 0, Title = "Pantoufles du Canadien en Phentex" @@ -88,6 +93,7 @@ namespace GrossesMitainesAPI.Migrations Description = "On ne lui ferait pas mal, en tout cas!!", ImageName = "jeanlucmongrain", Price = 1453.12m, + PromoPrice = 0m, Quantity = 1L, Status = 3, Title = "Jean-Luc Mongrain" diff --git a/GrossesMitaines/GrossesMitainesAPI/Models/Product.cs b/GrossesMitaines/GrossesMitainesAPI/Models/Product.cs index 363d64d..6eac294 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Models/Product.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Models/Product.cs @@ -24,6 +24,8 @@ public class Product { public string Description { get; set; } = "Lorem Ipsum."; [Required, Range(0.01, (double)decimal.MaxValue)] // Range qui prend pas les decimals! public decimal Price { get; set; } = 0; + [Required, Range(0.00, (double)decimal.MaxValue)] + public decimal PromoPrice { get; set; } = 0; public uint Quantity { get; set; } = 0; public States Status { get; set; } = States.Available; public string? ImageName { get; set; } // Base pour sortir les images ({ImageName}.jpg , {ImageName}_thumbnail.jpg, etc...) diff --git a/GrossesMitaines/GrossesMitainesAPI/appsettings.json b/GrossesMitaines/GrossesMitainesAPI/appsettings.json index c4178a3..1d25e03 100644 --- a/GrossesMitaines/GrossesMitainesAPI/appsettings.json +++ b/GrossesMitaines/GrossesMitainesAPI/appsettings.json @@ -7,6 +7,6 @@ }, "AllowedHosts": "*", "ConnectionStrings": { - "DefaultConnection": "Server=(localdb)\\mssqllocaldb; Database=GrossesMitainesDB0; Trusted_Connection=True; MultipleActiveResultSets=true" + "DefaultConnection": "Server=(localdb)\\mssqllocaldb; Database=GrossesMitainesDB1; Trusted_Connection=True; MultipleActiveResultSets=true" } }