update search and products

This commit is contained in:
MarcEricMartel
2022-10-16 17:33:58 -07:00
parent dbc1c78f07
commit 272c232530
7 changed files with 51 additions and 18 deletions

View File

@@ -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;

View File

@@ -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);
}