Skip to content

Commit

Permalink
Product filter created
Browse files Browse the repository at this point in the history
  • Loading branch information
KaloyankerR committed Dec 29, 2023
1 parent 82e884d commit fc92da8
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 10 deletions.
81 changes: 81 additions & 0 deletions FitFusion/Controllers/Filter/ProductFilter.cs
Original file line number Diff line number Diff line change
@@ -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<Product>
{
public List<Product> Filter(List<Product> products, Dictionary<string, object> 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<Product>
{
public List<Product> Filter(List<Product> products, Dictionary<string, object> filters)
{
string filterKey = filters.Keys.FirstOrDefault()!;

if (filterKey == "price" && filters.TryGetValue(filterKey, out var priceRangeObj) && priceRangeObj is List<double> 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<Product> Filter(List<Product> products, Dictionary<string, object> filters)
{
IFilter<Product> 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);
}
}

}
5 changes: 3 additions & 2 deletions FitFusion/Controllers/ProductManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ namespace Services
public class ProductManager : IProduct
{
private readonly IProduct _dao;
private IFilter<Product> _filter;
// private IFilter<Product> _filter;
// private ISort<Product> _sort;
private ProductSorter _sorter;
private ProductFilter _filter;

public ProductManager(IProduct dao, IFilter<Product> filter, ProductSorter sorter)
public ProductManager(IProduct dao, ProductFilter filter, ProductSorter sorter)
{
_dao = dao;
_filter = filter;
Expand Down
2 changes: 1 addition & 1 deletion FitFusion/FitFusionWeb/Pages/Cart.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
2 changes: 1 addition & 1 deletion FitFusion/FitFusionWeb/Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class IndexModel : PageModel
{
[BindProperty]
public List<Product> 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<IndexModel> _logger;

public IndexModel(ILogger<IndexModel> logger)
Expand Down
7 changes: 5 additions & 2 deletions FitFusion/FitFusionWeb/Pages/Products/All.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class AllModel : PageModel
public string FilterByCategory { get; set; } = "All";

public List<Product> 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()
{
Expand All @@ -41,9 +41,12 @@ public IActionResult OnPost()
{
try
{
Dictionary<string, object> filter = new Dictionary<string, object>();
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)
Expand Down
2 changes: 1 addition & 1 deletion FitFusion/FitFusionWeb/Pages/Products/Create.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{ }
Expand Down
2 changes: 1 addition & 1 deletion FitFusion/FitFusionWeb/Pages/Products/Product.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
2 changes: 1 addition & 1 deletion FitFusion/FitFusionWeb/Pages/Products/Update.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
2 changes: 1 addition & 1 deletion FitFusion/FitFusionWeb/Pages/Stats/Products.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit fc92da8

Please sign in to comment.