From bcdd04f42659be335cbad7cb2dd960aac7a5a7ee Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Tue, 18 Oct 2022 10:17:28 -0700 Subject: [PATCH 1/4] Update SearchController.cs --- .../Controllers/SearchController.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs index 1acf0a8..2d63a8f 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs @@ -20,17 +20,24 @@ public class SearchController : Controller { [HttpPost(Name = "Search")] public IEnumerable Post(string query, bool? preview) { const int PREVIEW = 3; - HashSet products = new(); + List products = new(); query = query.Trim(); try { // Pour faire une liste priorisée. if (preview.HasValue && preview == true) - products = _context.Products.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToHashSet(); + products = _context.Products.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList(); 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()); + products = _context.Products.Where(x => x.Title.Contains(query)).ToList(); + + foreach (Product prod in _context.Products.Where(x => x.Category.Contains(query)).ToList()) { + if (!products.Contains(prod)) + products.Add(prod); + } + foreach (Product prod in _context.Products.Where(x => x.Description.Contains(query)).ToList()) { + if (!products.Contains(prod)) + products.Add(prod); + } } } catch (Exception e) { _logger.LogError(8, e.Message); From 1af5b8f702ff9fea5a0cd4109306c9666524bd08 Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Tue, 18 Oct 2022 10:25:08 -0700 Subject: [PATCH 2/4] A long vacation --- .../GrossesMitainesAPI/Controllers/InventoryController.cs | 4 +++- .../GrossesMitainesAPI/Controllers/SearchController.cs | 8 ++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs index dc3be9a..6aafcb5 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs @@ -20,7 +20,7 @@ public class InventoryController : Controller { [EnableCors("_myAllowSpecificOrigins")] [HttpGet(Name = "Inventory")] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant) - public IEnumerable Get(int? lastId, string? order, string? filterPrice, string? filterState) { + public IEnumerable Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) { const int AMOUNT = 5; var ret = _context.Products.AsQueryable(); @@ -95,6 +95,8 @@ public class InventoryController : Controller { if (prod.Id == lastId) yup = true; } + else if (all.HasValue && all == true) + lst = ret.ToList(); else lst = ret.Take(AMOUNT).ToList(); return lst; diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs index 7baeb70..485c05d 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs @@ -31,8 +31,12 @@ public class SearchController : Controller { if (preview.HasValue && preview == true) products = _context.Products.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList(); else { - products = _context.Products.Where(x => x.Title.Contains(query)).ToList(); - + products = _context.Products.Where(x => x.Title.Replace(".", " ") + .Replace(",", " ") + .ToUpper() + .Contains(" " + query.ToUpper() + " ")) + .ToList(); + foreach (Product prod in _context.Products.Where(x => x.Category.Contains(query)).ToList()) { if (!products.Contains(prod)) products.Add(prod); From ffe5919354fb919bfa2758e7b9059f0cc1e92161 Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Tue, 18 Oct 2022 11:25:23 -0700 Subject: [PATCH 3/4] Update SearchController.cs --- .../Controllers/SearchController.cs | 60 ++++++++++++++----- 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs index 485c05d..d6629d8 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs @@ -13,15 +13,21 @@ namespace GrossesMitainesAPI.Controllers; public class SearchController : Controller { private readonly ILogger _logger; private readonly InventoryContext _context; + private static List _searchCache = new(); + private static DateTime? _cacheDate; public SearchController(ILogger logger, InventoryContext context) { _logger = logger; _context = context; + if (_searchCache.Count == 0 || _cacheDate.HasValue && _cacheDate.Value.AddMinutes(5) < DateTime.Now) { + _searchCache = _context.Products.ToList(); + _cacheDate = DateTime.Now; + } } [EnableCors("_myAllowSpecificOrigins")] [HttpPost(Name = "Search")] - public IEnumerable Post(string query, bool? preview) { + public IEnumerable Post(string query, bool? preview, bool? deep) { const int PREVIEW = 3; List products = new(); @@ -29,21 +35,45 @@ public class SearchController : Controller { try { // Pour faire une liste priorisée. if (preview.HasValue && preview == true) - products = _context.Products.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList(); - else { - products = _context.Products.Where(x => x.Title.Replace(".", " ") - .Replace(",", " ") - .ToUpper() - .Contains(" " + query.ToUpper() + " ")) - .ToList(); + //products = _context.Products.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList(); + products = _searchCache.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList(); - foreach (Product prod in _context.Products.Where(x => x.Category.Contains(query)).ToList()) { - if (!products.Contains(prod)) - products.Add(prod); - } - foreach (Product prod in _context.Products.Where(x => x.Description.Contains(query)).ToList()) { - if (!products.Contains(prod)) - products.Add(prod); + else { + if (deep.HasValue && deep == true) { + List title = new(), desc = new(), cat = new(); + query = query.ToLower(); + + // foreach (Product prod in _context.Products.ToList()) { + foreach (Product prod in _searchCache) { + string sTitle = prod.Title.Replace(".", " ").Replace(",", " ").ToLower(), + sCat = prod.Category.Replace(".", " ").Replace(",", " ").ToLower(), + sDesc = prod.Description.Replace(".", " ").Replace(",", " ").ToLower(); + + if (sTitle.StartsWith(query)) + products.Add(prod); + else if (sTitle.Contains(" " + query + " ")) + title.Add(prod); + else if (sDesc.Contains(" " + query + " ")) + desc.Add(prod); + else if (sCat.Contains(" " + query + " ")) + cat.Add(prod); + } + products.AddRange(title); + products.AddRange(desc); + products.AddRange(cat); + + } else { + // products = _context.Products.Where(x => x.Title.Contains(query)).ToList(); + products = _searchCache.Where(x => x.Title.Contains(query)).ToList(); + + // foreach (Product prod in _context.Products.Where(x => x.Description.Contains(query)).ToList()) + foreach (Product prod in _searchCache.Where(x => x.Description.Contains(query)).ToList()) + if (!products.Contains(prod)) + products.Add(prod); + // foreach (Product prod in _context.Products.Where(x => x.Category.Contains(query)).ToList()) + foreach (Product prod in _searchCache.Where(x => x.Category.Contains(query)).ToList()) + if (!products.Contains(prod)) + products.Add(prod); } } } catch (Exception e) { From 144ff9478f662e4c2796665e6c5ec38548c91ebc Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Tue, 18 Oct 2022 11:28:22 -0700 Subject: [PATCH 4/4] Cleanup on aisle Search --- .../GrossesMitainesAPI/Controllers/SearchController.cs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs index d6629d8..3292fcb 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs @@ -35,15 +35,12 @@ public class SearchController : Controller { try { // Pour faire une liste priorisée. if (preview.HasValue && preview == true) - //products = _context.Products.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList(); products = _searchCache.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList(); - else { if (deep.HasValue && deep == true) { List title = new(), desc = new(), cat = new(); query = query.ToLower(); - // foreach (Product prod in _context.Products.ToList()) { foreach (Product prod in _searchCache) { string sTitle = prod.Title.Replace(".", " ").Replace(",", " ").ToLower(), sCat = prod.Category.Replace(".", " ").Replace(",", " ").ToLower(), @@ -61,16 +58,11 @@ public class SearchController : Controller { products.AddRange(title); products.AddRange(desc); products.AddRange(cat); - } else { - // products = _context.Products.Where(x => x.Title.Contains(query)).ToList(); products = _searchCache.Where(x => x.Title.Contains(query)).ToList(); - - // foreach (Product prod in _context.Products.Where(x => x.Description.Contains(query)).ToList()) foreach (Product prod in _searchCache.Where(x => x.Description.Contains(query)).ToList()) if (!products.Contains(prod)) products.Add(prod); - // foreach (Product prod in _context.Products.Where(x => x.Category.Contains(query)).ToList()) foreach (Product prod in _searchCache.Where(x => x.Category.Contains(query)).ToList()) if (!products.Contains(prod)) products.Add(prod);