From fc92da8ec02655f1c20864e38adbd415efb0d25e Mon Sep 17 00:00:00 2001 From: Kaloyan Date: Fri, 29 Dec 2023 21:44:12 +0100 Subject: [PATCH] Product filter created --- FitFusion/Controllers/Filter/ProductFilter.cs | 81 +++++++++++++++++++ FitFusion/Controllers/ProductManager.cs | 5 +- FitFusion/FitFusionWeb/Pages/Cart.cshtml.cs | 2 +- FitFusion/FitFusionWeb/Pages/Index.cshtml.cs | 2 +- .../FitFusionWeb/Pages/Products/All.cshtml.cs | 7 +- .../Pages/Products/Create.cshtml.cs | 2 +- .../Pages/Products/Product.cshtml.cs | 2 +- .../Pages/Products/Update.cshtml.cs | 2 +- .../Pages/Stats/Products.cshtml.cs | 2 +- 9 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 FitFusion/Controllers/Filter/ProductFilter.cs diff --git a/FitFusion/Controllers/Filter/ProductFilter.cs b/FitFusion/Controllers/Filter/ProductFilter.cs new file mode 100644 index 0000000..91b1b1a --- /dev/null +++ b/FitFusion/Controllers/Filter/ProductFilter.cs @@ -0,0 +1,81 @@ +using Interfaces.Strategy; +using Models.Product; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Services.Filter +{ + public class CategoryFilterStrategy : IFilter + { + public List Filter(List products, Dictionary filters) + { + string filterKey = filters.Keys.FirstOrDefault()!; + + if (filterKey == "category" && filters.TryGetValue(filterKey, out var filterValue)) + { + string categoryFilter = filterValue.ToString()!; + + if (!string.IsNullOrEmpty(categoryFilter) && categoryFilter != "All") + { + return products.Where(p => p.Category.ToString().Equals(categoryFilter, StringComparison.OrdinalIgnoreCase)).ToList(); + } + } + + return products; + } + } + + public class PriceFilterStrategy : IFilter + { + public List Filter(List products, Dictionary filters) + { + string filterKey = filters.Keys.FirstOrDefault()!; + + if (filterKey == "price" && filters.TryGetValue(filterKey, out var priceRangeObj) && priceRangeObj is List priceRange) + { + double min = priceRange[0]; + double max = priceRange[1]; + + if (min >= 0 && max >= 0 && min <= max) + { + return products.Where(p => p.Price >= min && p.Price <= max).ToList(); + } + } + + return products; + } + } + + public class ProductFilter + { + public List Filter(List products, Dictionary filters) + { + IFilter filter; + + if (filters == null || filters.Count == 0) + { + return products; + } + + string filterKey = filters.Keys.FirstOrDefault()!; + + switch (filterKey) + { + case "category": + filter = new CategoryFilterStrategy(); + break; + case "price": + filter = new PriceFilterStrategy(); + break; + default: + return products; + } + + return filter.Filter(products, filters); + } + } + +} diff --git a/FitFusion/Controllers/ProductManager.cs b/FitFusion/Controllers/ProductManager.cs index 6fd78b6..8f95030 100644 --- a/FitFusion/Controllers/ProductManager.cs +++ b/FitFusion/Controllers/ProductManager.cs @@ -16,11 +16,12 @@ namespace Services public class ProductManager : IProduct { private readonly IProduct _dao; - private IFilter _filter; + // private IFilter _filter; // private ISort _sort; private ProductSorter _sorter; + private ProductFilter _filter; - public ProductManager(IProduct dao, IFilter filter, ProductSorter sorter) + public ProductManager(IProduct dao, ProductFilter filter, ProductSorter sorter) { _dao = dao; _filter = filter; diff --git a/FitFusion/FitFusionWeb/Pages/Cart.cshtml.cs b/FitFusion/FitFusionWeb/Pages/Cart.cshtml.cs index 7bf43c0..1d316f7 100644 --- a/FitFusion/FitFusionWeb/Pages/Cart.cshtml.cs +++ b/FitFusion/FitFusionWeb/Pages/Cart.cshtml.cs @@ -20,7 +20,7 @@ namespace FitFusionWeb.Pages [Authorize(Roles = "Customer")] public class CartModel : PageModel { - public readonly ProductManager _productManager = new(new ProductDAO(), new FilterByCategory(), new ProductSorter()); + public readonly ProductManager _productManager = new(new ProductDAO(), new ProductFilter(), new ProductSorter()); private readonly UserManager _userManager = new(new UserDAO(), new UserSorter()); private readonly OrderManager _orderManager = new(new OrderDAO()); diff --git a/FitFusion/FitFusionWeb/Pages/Index.cshtml.cs b/FitFusion/FitFusionWeb/Pages/Index.cshtml.cs index 2d4a964..4766a2d 100644 --- a/FitFusion/FitFusionWeb/Pages/Index.cshtml.cs +++ b/FitFusion/FitFusionWeb/Pages/Index.cshtml.cs @@ -15,7 +15,7 @@ public class IndexModel : PageModel { [BindProperty] public List Products { get; set; } = new(); - private readonly ProductManager _productManager = new(new ProductDAO(), new FilterByCategory(), new ProductSorter()); + private readonly ProductManager _productManager = new(new ProductDAO(), new ProductFilter(), new ProductSorter()); private readonly ILogger _logger; public IndexModel(ILogger logger) diff --git a/FitFusion/FitFusionWeb/Pages/Products/All.cshtml.cs b/FitFusion/FitFusionWeb/Pages/Products/All.cshtml.cs index ac66b81..b828af1 100644 --- a/FitFusion/FitFusionWeb/Pages/Products/All.cshtml.cs +++ b/FitFusion/FitFusionWeb/Pages/Products/All.cshtml.cs @@ -21,7 +21,7 @@ public class AllModel : PageModel public string FilterByCategory { get; set; } = "All"; public List Products { get; set; } = new(); - private ProductManager productManager = new(new ProductDAO(), new FilterByCategory(), new ProductSorter()); + private ProductManager productManager = new(new ProductDAO(), new ProductFilter(), new ProductSorter()); public IActionResult OnGet() { @@ -41,9 +41,12 @@ public IActionResult OnPost() { try { + Dictionary filter = new Dictionary(); + filter.Add("category", FilterByCategory); + Products = productManager.GetProducts(); Products = productManager.Search(Products, SearchQuery); - // Products = productManager.Filter(Products, FilterByCategory); + Products = productManager.Filter(Products, filter); Products = productManager.Sort(Products, Sort); } catch (DataAccessException) diff --git a/FitFusion/FitFusionWeb/Pages/Products/Create.cshtml.cs b/FitFusion/FitFusionWeb/Pages/Products/Create.cshtml.cs index a257958..854f341 100644 --- a/FitFusion/FitFusionWeb/Pages/Products/Create.cshtml.cs +++ b/FitFusion/FitFusionWeb/Pages/Products/Create.cshtml.cs @@ -15,7 +15,7 @@ public class CreateModel : PageModel { [BindProperty] public Product Product { get; set; } = new(); - private readonly ProductManager _productManager = new(new DataAcess.ProductDAO(), new FilterByCategory(), new ProductSorter()); + private readonly ProductManager _productManager = new(new DataAcess.ProductDAO(), new ProductFilter(), new ProductSorter()); public void OnGet() { } diff --git a/FitFusion/FitFusionWeb/Pages/Products/Product.cshtml.cs b/FitFusion/FitFusionWeb/Pages/Products/Product.cshtml.cs index 814f7db..e3db6ff 100644 --- a/FitFusion/FitFusionWeb/Pages/Products/Product.cshtml.cs +++ b/FitFusion/FitFusionWeb/Pages/Products/Product.cshtml.cs @@ -14,7 +14,7 @@ public class ProductModel : PageModel public int Id { get; set; } [BindProperty] public Product Product { get; set; } = new(); - private readonly ProductManager _productManager = new ProductManager(new DataAcess.ProductDAO(), new FilterByCategory(), new ProductSorter()); + private readonly ProductManager _productManager = new ProductManager(new DataAcess.ProductDAO(), new ProductFilter(), new ProductSorter()); public IActionResult OnGet() { diff --git a/FitFusion/FitFusionWeb/Pages/Products/Update.cshtml.cs b/FitFusion/FitFusionWeb/Pages/Products/Update.cshtml.cs index e5f5d91..6e62a7e 100644 --- a/FitFusion/FitFusionWeb/Pages/Products/Update.cshtml.cs +++ b/FitFusion/FitFusionWeb/Pages/Products/Update.cshtml.cs @@ -14,7 +14,7 @@ public class UpdateModel : PageModel public int Id { get; set; } [BindProperty] public Product Product { get; set; } = new(); - private readonly ProductManager _productManager = new (new DataAcess.ProductDAO(), new FilterByCategory(), new ProductSorter()); + private readonly ProductManager _productManager = new (new DataAcess.ProductDAO(), new ProductFilter(), new ProductSorter()); public IActionResult OnGet() { diff --git a/FitFusion/FitFusionWeb/Pages/Stats/Products.cshtml.cs b/FitFusion/FitFusionWeb/Pages/Stats/Products.cshtml.cs index 8aa8001..e962e45 100644 --- a/FitFusion/FitFusionWeb/Pages/Stats/Products.cshtml.cs +++ b/FitFusion/FitFusionWeb/Pages/Stats/Products.cshtml.cs @@ -21,7 +21,7 @@ public class ProductsModel : PageModel public ProductsModel() { - _productManager = new(new ProductDAO(), new FilterByCategory(), new ProductSorter()); + _productManager = new(new ProductDAO(), new ProductFilter(), new ProductSorter()); } public void OnGet()