update search and products
This commit is contained in:
		| @@ -30,15 +30,20 @@ public class ProductController : Controller { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     [HttpPost(Name = "Product")] |     [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() {  |         Product prod = new() {  | ||||||
|             Title =  title, |             Title =  title, | ||||||
|             Category = category, |             Category = category, | ||||||
|             Description = description, |             Description = description, | ||||||
|             Price = price.HasValue? (decimal)price: 0.01M, |             Price = price.HasValue? (decimal)price: 0.01M, | ||||||
|  |             PromoPrice = promoprice.HasValue ? (decimal)promoprice : 0.01M, | ||||||
|             Quantity = quantity.HasValue ? (uint)quantity : 0, |             Quantity = quantity.HasValue ? (uint)quantity : 0, | ||||||
|             ImageName = imagename |             ImageName = imagename | ||||||
|         }; |         }; | ||||||
|  |  | ||||||
|  |         if (prod.Price <= prod.PromoPrice) | ||||||
|  |             prod.PromoPrice = prod.Price - 0.01M; | ||||||
|  |  | ||||||
|         try { |         try { | ||||||
|             _context.Products.Add(prod); |             _context.Products.Add(prod); | ||||||
|             _context.SaveChanges(); |             _context.SaveChanges(); | ||||||
| @@ -48,7 +53,7 @@ public class ProductController : Controller { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     [HttpPut(Name = "Product")] |     [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 { |         try { | ||||||
|             Product prod = _context.Products.Where(x => x.Id == id).First(); |             Product prod = _context.Products.Where(x => x.Id == id).First(); | ||||||
|  |  | ||||||
| @@ -64,6 +69,9 @@ public class ProductController : Controller { | |||||||
|             if (price.HasValue || price > 0) |             if (price.HasValue || price > 0) | ||||||
|                 prod.Price = (decimal)price; |                 prod.Price = (decimal)price; | ||||||
|  |  | ||||||
|  |             if (promoprice.HasValue || promoprice > prod.Price) | ||||||
|  |                 prod.PromoPrice = (decimal)promoprice; | ||||||
|  |  | ||||||
|             if (quantity.HasValue) |             if (quantity.HasValue) | ||||||
|                 prod.Quantity = (uint)quantity; |                 prod.Quantity = (uint)quantity; | ||||||
|  |  | ||||||
| @@ -80,6 +88,9 @@ public class ProductController : Controller { | |||||||
|                 case "isClearance": |                 case "isClearance": | ||||||
|                     prod.Status = Product.States.Clearance; |                     prod.Status = Product.States.Clearance; | ||||||
|                     break; |                     break; | ||||||
|  |                 case "isPromotion": | ||||||
|  |                     prod.Status = Product.States.Promotion; | ||||||
|  |                     break; | ||||||
|                 case "isDiscontinued": |                 case "isDiscontinued": | ||||||
|                     prod.Status = Product.States.Discontinued; |                     prod.Status = Product.States.Discontinued; | ||||||
|                     break; |                     break; | ||||||
| @@ -97,7 +108,7 @@ public class ProductController : Controller { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     [HttpPatch(Name = "Product")] |     [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 { |         try { | ||||||
|             Product prod = _context.Products.Where(x => x.Id == id).First(); |             Product prod = _context.Products.Where(x => x.Id == id).First(); | ||||||
|  |  | ||||||
| @@ -113,9 +124,9 @@ public class ProductController : Controller { | |||||||
|                 prod.Description = description; |                 prod.Description = description; | ||||||
|             else prod.Description = ""; |             else prod.Description = ""; | ||||||
|  |  | ||||||
|             if (price.HasValue || price > 0) |             if (promoprice.HasValue || promoprice < prod.Price) | ||||||
|                 prod.Price = (decimal)price; |                 prod.PromoPrice = (decimal)promoprice; | ||||||
|             else prod.Price = 0.01M; |             else prod.PromoPrice = prod.Price - 0.01M; | ||||||
|  |  | ||||||
|             if (quantity.HasValue) |             if (quantity.HasValue) | ||||||
|                 prod.Quantity = (uint)quantity; |                 prod.Quantity = (uint)quantity; | ||||||
| @@ -134,6 +145,9 @@ public class ProductController : Controller { | |||||||
|                 case "isClearance": |                 case "isClearance": | ||||||
|                     prod.Status = Product.States.Clearance; |                     prod.Status = Product.States.Clearance; | ||||||
|                     break; |                     break; | ||||||
|  |                 case "isPromotion": | ||||||
|  |                     prod.Status = Product.States.Promotion; | ||||||
|  |                     break; | ||||||
|                 case "isDiscontinued": |                 case "isDiscontinued": | ||||||
|                     prod.Status = Product.States.Discontinued; |                     prod.Status = Product.States.Discontinued; | ||||||
|                     break; |                     break; | ||||||
|   | |||||||
| @@ -18,15 +18,19 @@ public class SearchController : Controller { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     [HttpPost(Name = "Search")] |     [HttpPost(Name = "Search")] | ||||||
|     public IEnumerable<Product> Post(string query) { |     public IEnumerable<Product> Post(string query, bool? preview) { | ||||||
|         HashSet<Product> products = new(); |         HashSet<Product> products = new(); | ||||||
|  |  | ||||||
|         query = query.Trim(); |         query = query.Trim(); | ||||||
|  |  | ||||||
|         try { // Pour faire une liste priorisée. |         try { // Pour faire une liste priorisée. | ||||||
|             products.Concat(_context.Products.Where(x => x.Title.Contains(query)).ToHashSet()); |             if (preview.HasValue && preview == true) | ||||||
|             products.Concat(_context.Products.Where(x => x.Category.Contains(query)).ToHashSet()); |                 products = _context.Products.Where(x => x.Title.Contains(query)).Take(3).ToHashSet(); | ||||||
|             products.Concat(_context.Products.Where(x => x.Description.Contains(query)).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) { |         } catch (Exception e) { | ||||||
|             _logger.LogError(8, e.Message); |             _logger.LogError(8, e.Message); | ||||||
|         } |         } | ||||||
|   | |||||||
| @@ -11,7 +11,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | |||||||
| namespace GrossesMitainesAPI.Migrations | namespace GrossesMitainesAPI.Migrations | ||||||
| { | { | ||||||
|     [DbContext(typeof(InventoryContext))] |     [DbContext(typeof(InventoryContext))] | ||||||
|     [Migration("20221016150710_Initial-Db")] |     [Migration("20221017003248_Initial-Db")] | ||||||
|     partial class InitialDb |     partial class InitialDb | ||||||
|     { |     { | ||||||
|         protected override void BuildTargetModel(ModelBuilder modelBuilder) |         protected override void BuildTargetModel(ModelBuilder modelBuilder) | ||||||
| @@ -45,6 +45,9 @@ namespace GrossesMitainesAPI.Migrations | |||||||
|                     b.Property<decimal>("Price") |                     b.Property<decimal>("Price") | ||||||
|                         .HasColumnType("decimal(18,2)"); |                         .HasColumnType("decimal(18,2)"); | ||||||
| 
 | 
 | ||||||
|  |                     b.Property<decimal>("PromoPrice") | ||||||
|  |                         .HasColumnType("decimal(18,2)"); | ||||||
|  | 
 | ||||||
|                     b.Property<long>("Quantity") |                     b.Property<long>("Quantity") | ||||||
|                         .HasColumnType("bigint"); |                         .HasColumnType("bigint"); | ||||||
| 
 | 
 | ||||||
| @@ -68,6 +71,7 @@ namespace GrossesMitainesAPI.Migrations | |||||||
|                             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.", | ||||||
|                             ImageName = "ceintureflechee", |                             ImageName = "ceintureflechee", | ||||||
|                             Price = 85.86m, |                             Price = 85.86m, | ||||||
|  |                             PromoPrice = 0m, | ||||||
|                             Quantity = 1L, |                             Quantity = 1L, | ||||||
|                             Status = 4, |                             Status = 4, | ||||||
|                             Title = "Ceinture flèchée" |                             Title = "Ceinture flèchée" | ||||||
| @@ -79,6 +83,7 @@ namespace GrossesMitainesAPI.Migrations | |||||||
|                             Description = "Parce que ça sent la coupe!", |                             Description = "Parce que ça sent la coupe!", | ||||||
|                             ImageName = "pantouflesCH", |                             ImageName = "pantouflesCH", | ||||||
|                             Price = 15.64m, |                             Price = 15.64m, | ||||||
|  |                             PromoPrice = 0m, | ||||||
|                             Quantity = 54L, |                             Quantity = 54L, | ||||||
|                             Status = 0, |                             Status = 0, | ||||||
|                             Title = "Pantoufles du Canadien en Phentex" |                             Title = "Pantoufles du Canadien en Phentex" | ||||||
| @@ -90,6 +95,7 @@ namespace GrossesMitainesAPI.Migrations | |||||||
|                             Description = "On ne lui ferait pas mal, en tout cas!!", |                             Description = "On ne lui ferait pas mal, en tout cas!!", | ||||||
|                             ImageName = "jeanlucmongrain", |                             ImageName = "jeanlucmongrain", | ||||||
|                             Price = 1453.12m, |                             Price = 1453.12m, | ||||||
|  |                             PromoPrice = 0m, | ||||||
|                             Quantity = 1L, |                             Quantity = 1L, | ||||||
|                             Status = 3, |                             Status = 3, | ||||||
|                             Title = "Jean-Luc Mongrain" |                             Title = "Jean-Luc Mongrain" | ||||||
| @@ -18,6 +18,7 @@ namespace GrossesMitainesAPI.Migrations | |||||||
|                     Category = table.Column<string>(type: "nvarchar(max)", nullable: false), |                     Category = table.Column<string>(type: "nvarchar(max)", nullable: false), | ||||||
|                     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), | ||||||
|  |                     PromoPrice = 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), | ||||||
|                     Status = table.Column<int>(type: "int", 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) | ||||||
| @@ -29,18 +30,18 @@ namespace GrossesMitainesAPI.Migrations | |||||||
| 
 | 
 | ||||||
|             migrationBuilder.InsertData( |             migrationBuilder.InsertData( | ||||||
|                 table: "Products", |                 table: "Products", | ||||||
|                 columns: new[] { "Id", "Category", "Description", "ImageName", "Price", "Quantity", "Status", "Title" }, |                 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, 1L, 4, "Ceinture flèchée" }); |                 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( |             migrationBuilder.InsertData( | ||||||
|                 table: "Products", |                 table: "Products", | ||||||
|                 columns: new[] { "Id", "Category", "Description", "ImageName", "Price", "Quantity", "Status", "Title" }, |                 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, 54L, 0, "Pantoufles du Canadien en Phentex" }); |                 values: new object[] { 2, "Linge", "Parce que ça sent la coupe!", "pantouflesCH", 15.64m, 0m, 54L, 0, "Pantoufles du Canadien en Phentex" }); | ||||||
| 
 | 
 | ||||||
|             migrationBuilder.InsertData( |             migrationBuilder.InsertData( | ||||||
|                 table: "Products", |                 table: "Products", | ||||||
|                 columns: new[] { "Id", "Category", "Description", "ImageName", "Price", "Quantity", "Status", "Title" }, |                 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, 1L, 3, "Jean-Luc Mongrain" }); |                 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) |         protected override void Down(MigrationBuilder migrationBuilder) | ||||||
| @@ -43,6 +43,9 @@ namespace GrossesMitainesAPI.Migrations | |||||||
|                     b.Property<decimal>("Price") |                     b.Property<decimal>("Price") | ||||||
|                         .HasColumnType("decimal(18,2)"); |                         .HasColumnType("decimal(18,2)"); | ||||||
|  |  | ||||||
|  |                     b.Property<decimal>("PromoPrice") | ||||||
|  |                         .HasColumnType("decimal(18,2)"); | ||||||
|  |  | ||||||
|                     b.Property<long>("Quantity") |                     b.Property<long>("Quantity") | ||||||
|                         .HasColumnType("bigint"); |                         .HasColumnType("bigint"); | ||||||
|  |  | ||||||
| @@ -66,6 +69,7 @@ namespace GrossesMitainesAPI.Migrations | |||||||
|                             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.", | ||||||
|                             ImageName = "ceintureflechee", |                             ImageName = "ceintureflechee", | ||||||
|                             Price = 85.86m, |                             Price = 85.86m, | ||||||
|  |                             PromoPrice = 0m, | ||||||
|                             Quantity = 1L, |                             Quantity = 1L, | ||||||
|                             Status = 4, |                             Status = 4, | ||||||
|                             Title = "Ceinture flèchée" |                             Title = "Ceinture flèchée" | ||||||
| @@ -77,6 +81,7 @@ namespace GrossesMitainesAPI.Migrations | |||||||
|                             Description = "Parce que ça sent la coupe!", |                             Description = "Parce que ça sent la coupe!", | ||||||
|                             ImageName = "pantouflesCH", |                             ImageName = "pantouflesCH", | ||||||
|                             Price = 15.64m, |                             Price = 15.64m, | ||||||
|  |                             PromoPrice = 0m, | ||||||
|                             Quantity = 54L, |                             Quantity = 54L, | ||||||
|                             Status = 0, |                             Status = 0, | ||||||
|                             Title = "Pantoufles du Canadien en Phentex" |                             Title = "Pantoufles du Canadien en Phentex" | ||||||
| @@ -88,6 +93,7 @@ namespace GrossesMitainesAPI.Migrations | |||||||
|                             Description = "On ne lui ferait pas mal, en tout cas!!", |                             Description = "On ne lui ferait pas mal, en tout cas!!", | ||||||
|                             ImageName = "jeanlucmongrain", |                             ImageName = "jeanlucmongrain", | ||||||
|                             Price = 1453.12m, |                             Price = 1453.12m, | ||||||
|  |                             PromoPrice = 0m, | ||||||
|                             Quantity = 1L, |                             Quantity = 1L, | ||||||
|                             Status = 3, |                             Status = 3, | ||||||
|                             Title = "Jean-Luc Mongrain" |                             Title = "Jean-Luc Mongrain" | ||||||
|   | |||||||
| @@ -24,6 +24,8 @@ public class Product { | |||||||
|     public string Description { get; set; } = "Lorem Ipsum."; |     public string Description { get; set; } = "Lorem Ipsum."; | ||||||
|     [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; | ||||||
|  |     [Required, Range(0.00, (double)decimal.MaxValue)] | ||||||
|  |     public decimal PromoPrice { get; set; } = 0; | ||||||
|     public uint Quantity { get; set; } = 0; |     public uint Quantity { get; set; } = 0; | ||||||
|     public States Status { get; set; } = States.Available; |     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...) | ||||||
|   | |||||||
| @@ -7,6 +7,6 @@ | |||||||
|   }, |   }, | ||||||
|   "AllowedHosts": "*", |   "AllowedHosts": "*", | ||||||
|   "ConnectionStrings": { |   "ConnectionStrings": { | ||||||
|     "DefaultConnection": "Server=(localdb)\\mssqllocaldb; Database=GrossesMitainesDB0; Trusted_Connection=True; MultipleActiveResultSets=true" |     "DefaultConnection": "Server=(localdb)\\mssqllocaldb; Database=GrossesMitainesDB1; Trusted_Connection=True; MultipleActiveResultSets=true" | ||||||
|   } |   } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user