From 0b5bb8629155894f44131a78c1600656e27f0d75 Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Sun, 16 Oct 2022 07:33:16 -0700 Subject: [PATCH] API --- .../Controllers/InventoryController.cs | 65 ++++++++++++++++++- .../Controllers/SearchController.cs | 3 +- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs index 9c34b3c..6a90399 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs @@ -16,13 +16,72 @@ public class InventoryController : Controller { _logger = logger; } - [HttpGet(Name ="Inventory")] // Pour faire des calls async par paquet de 5 (pour du loading en scrollant) - public IEnumerable Get(int? lastId) { + [HttpGet(Name = "Inventory")] // Pour faire des calls async par paquet de 5 (pour du loading en scrollant) + public IEnumerable Get(int? lastId, string order, string filter) { if (!lastId.HasValue) lastId = 1; - return _context.Products.Where(x => x.Id >= lastId && x.Id < lastId + 5).ToList(); + var ret = _context.Products.AsQueryable(); + + switch (filter) { + case "PriceUnder20": + ret = ret.Where(x => x.Price < 20); + break; + case "Price20to49": + ret = ret.Where(x => x.Price >= 20 && x.Price < 50); + break; + case "Price50to99": + ret = ret.Where(x => x.Price >= 50 && x.Price < 100); + break; + case "PriceOver100": + ret = ret.Where(x => x.Price >= 100); + break; + case "isAvailable": + ret = ret.Where(x => x.Quantity > 0); + break; + case "isBackOrder": + ret = ret.Where(x => x.Quantity == 0 || !x.isDiscontinued); + break; ; + case "isClearance": + ret = ret.Where(x => x.Quantity > 0 && x.isDiscontinued); + break; + case "isDiscontinued": + ret = ret.Where(x => x.Quantity == 0 && x.isDiscontinued); + break; + } + + switch (order) { + case "Price": + ret = ret.OrderBy(x => x.Price) + .Where(x => x.Id > lastId && x.Id < lastId + 5); + break; + case "PriceDesc": + ret = ret.OrderByDescending(x => x.Price) + .Where(x => x.Id < lastId && x.Id > lastId - 5); + break; + case "Title": + ret = ret.OrderBy(x => x.Title) + .Where(x => x.Id > lastId && x.Id < lastId + 5); + break; + case "TitleDesc": + ret = ret.OrderByDescending(x => x.Title) + .Where(x => x.Id < lastId && x.Id > lastId - 5); + break; + case "Category": + ret = ret.OrderBy(x => x.Category) + .Where(x => x.Id > lastId && x.Id < lastId + 5); + break; + case "CategoryDesc": + ret = ret.OrderByDescending(x => x.Category) + .Where(x => x.Id < lastId && x.Id > lastId - 5); + break; + default: + ret = ret.Where(x => x.Id > lastId && x.Id < lastId + 5); + break; + } + return ret.ToList(); } + // Inventory/Delete => Décrémenter un produit. [HttpDelete(Name = "Inventory")] public void Delete(int? id) { if (!id.HasValue) { diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs index 457e363..8c0cfb1 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs @@ -17,7 +17,7 @@ public class SearchController : Controller { } [HttpPost(Name = "Search")] - public IEnumerable Post(string query, bool preview) { + public IEnumerable Post(string query) { HashSet products = new(); query = query.Trim(); @@ -29,7 +29,6 @@ public class SearchController : Controller { } catch (Exception e) { _logger.LogError(8, e.Message); } - return products; }