update search and products
This commit is contained in:
		| @@ -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; | ||||
|   | ||||
| @@ -18,15 +18,19 @@ public class SearchController : Controller { | ||||
|     } | ||||
|  | ||||
|     [HttpPost(Name = "Search")] | ||||
|     public IEnumerable<Product> Post(string query) { | ||||
|     public IEnumerable<Product> Post(string query, bool? preview) { | ||||
|         HashSet<Product> 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); | ||||
|         } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user