Skip to content

Commit

Permalink
sort and filter refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
KaloyankerR committed Jan 8, 2024
1 parent 1c02702 commit 276f923
Show file tree
Hide file tree
Showing 45 changed files with 3,847 additions and 368 deletions.
Empty file.
Binary file modified Documentation/Archieve/UML&DB/FitFusionUML.vpp
Binary file not shown.
Binary file not shown.
Binary file added Documentation/FitFusionUML.pdf
Binary file not shown.
Binary file added Documentation/UML&DB/data.zip
Binary file not shown.
3,429 changes: 3,429 additions & 0 deletions Documentation/UML&DB/project.xml

Large diffs are not rendered by default.

91 changes: 45 additions & 46 deletions FitFusion/Controllers/Filter/ProductFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,55 +64,54 @@ public List<Product> Filter(List<Product> products, object param)
}
}

public class ProductFilter
{
public List<Product> Filter(List<Product> products, Dictionary<Enum, object> filters)
{
if (filters == null || filters.Count == 0)
{
return products;
}

List<(IFilter<Product> FilterStrategy, object FilterValue)> filterStrategies = new List<(IFilter<Product>, object)>();
//public class ProductFilter
//{
// public List<Product> Filter(List<Product> products, Dictionary<Enum, object> filters)
// {
// if (filters == null || filters.Count == 0)
// {
// return products;
// }

foreach (var filterEntry in filters)
{
if (filterEntry.Key is FilterParameter filterKey)
{
switch (filterKey)
{
case FilterParameter.Keyword:
filterStrategies.Add((new ProductKeywordFilterStrategy(), filterEntry.Value));
break;
case FilterParameter.Category:
filterStrategies.Add((new CategoryFilterStrategy(), filterEntry.Value));
break;
case FilterParameter.Price:
filterStrategies.Add((new PriceFilterStrategy(), filterEntry.Value));
break;
}
}
}
// List<(IFilter<Product> FilterStrategy, object FilterValue)> filterStrategies = new List<(IFilter<Product>, object)>();

if (filterStrategies.Count == 0)
{
return products;
}
// foreach (var filterEntry in filters)
// {
// if (filterEntry.Key is FilterParameter filterKey)
// {
// switch (filterKey)
// {
// case FilterParameter.Keyword:
// filterStrategies.Add((new ProductKeywordFilterStrategy(), filterEntry.Value));
// break;
// case FilterParameter.Category:
// filterStrategies.Add((new CategoryFilterStrategy(), filterEntry.Value));
// break;
// case FilterParameter.Price:
// filterStrategies.Add((new PriceFilterStrategy(), filterEntry.Value));
// break;
// }
// }
// }

foreach (var (filterStrategy, filterValue) in filterStrategies)
{
try
{
products = filterStrategy.Filter(products, filterValue);
}
catch
{
throw new ArgumentException("Invalid filtering parameter.");
}
}
// if (filterStrategies.Count == 0)
// {
// return products;
// }

return products;
}
// foreach (var (filterStrategy, filterValue) in filterStrategies)
// {
// try
// {
// products = filterStrategy.Filter(products, filterValue);
// }
// catch
// {
// throw new ArgumentException("Invalid filtering parameter.");
// }
// }

}
// return products;
// }
// }
}
87 changes: 43 additions & 44 deletions FitFusion/Controllers/Filter/UserFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,53 +42,52 @@ public List<User> Filter(List<User> users, object param)
}
}

public class UserFilter
{
public List<User> Filter(List<User> users, Dictionary<Enum, object> filters)
{
if (filters == null || filters.Count == 0)
{
return users;
}
//public class UserFilter
//{
// public List<User> Filter(List<User> users, Dictionary<Enum, object> filters)
// {
// if (filters == null || filters.Count == 0)
// {
// return users;
// }

List<(IFilter<User> FilterStrategy, object FilterValue)> filterStrategies = new List<(IFilter<User>, object)>();

foreach (var filterEntry in filters)
{
if (filterEntry.Key is FilterParameter filterKey)
{
switch (filterKey)
{
case FilterParameter.Keyword:
filterStrategies.Add((new KeywordFilterStrategy(), filterEntry.Value));
break;
case FilterParameter.Role:
filterStrategies.Add((new RoleFilterStrategy(), filterEntry.Value));
break;
// TODO Add more cases for additional filters
}
}
}
// List<(IFilter<User> FilterStrategy, object FilterValue)> filterStrategies = new List<(IFilter<User>, object)>();

if (filterStrategies.Count == 0)
{
return users;
}
// foreach (var filterEntry in filters)
// {
// if (filterEntry.Key is FilterParameter filterKey)
// {
// switch (filterKey)
// {
// case FilterParameter.Keyword:
// filterStrategies.Add((new KeywordFilterStrategy(), filterEntry.Value));
// break;
// case FilterParameter.Role:
// filterStrategies.Add((new RoleFilterStrategy(), filterEntry.Value));
// break;
// // TODO Add more cases for additional filters
// }
// }
// }

foreach (var (filterStrategy, filterValue) in filterStrategies)
{
try
{
users = filterStrategy.Filter(users, filterValue);
}
catch
{
throw new ArgumentException("Invalid filtering parameter.");
}
}
// if (filterStrategies.Count == 0)
// {
// return users;
// }

return users;
}
// foreach (var (filterStrategy, filterValue) in filterStrategies)
// {
// try
// {
// users = filterStrategy.Filter(users, filterValue);
// }
// catch
// {
// throw new ArgumentException("Invalid filtering parameter.");
// }
// }

}
// return users;
// }
// }
}
51 changes: 14 additions & 37 deletions FitFusion/Controllers/ProductManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ namespace Services
public class ProductManager : IProduct
{
private readonly IProduct _dao;
private readonly ProductSorter _sorter;
private readonly ProductFilter _filter;

public ProductManager(IProduct dao, ProductFilter filter, ProductSorter sorter)
public ProductManager(IProduct dao)
{
_dao = dao;
_filter = filter;
_sorter = sorter;
}

public bool CreateProduct(Product product)
Expand Down Expand Up @@ -58,18 +54,6 @@ public bool DeleteProduct(int productId)
}
}

public bool DeleteProduct(Product product)
{
try
{
return _dao.DeleteProduct(product.Id);
}
catch
{
throw;
}
}

public Product GetProductById(int id)
{
try
Expand All @@ -94,39 +78,32 @@ public List<Product> GetProducts()
}
}

//public List<Product> Search(List<Product> products, string searchQuery)
//{
// if (!string.IsNullOrEmpty(searchQuery))
// {
// searchQuery = searchQuery.ToLower();
// products = products.FindAll(p =>
// p.Title.ToLower().Contains(searchQuery, StringComparison.OrdinalIgnoreCase) ||
// (p.Description ?? "").ToLower().Contains(searchQuery, StringComparison.OrdinalIgnoreCase) ||
// p.Category.ToString().ToLower().Contains(searchQuery, StringComparison.OrdinalIgnoreCase) ||
// p.Price.ToString().ToLower().Contains(searchQuery, StringComparison.OrdinalIgnoreCase) //might be used in cases like EUR/Eur
// );
// }

// return products;
//}

public List<Product> Sort(List<Product> products, Enum param)
public List<Product> Sort(List<Product> products, ISort<Product> sortStrategy)
{
try
{
return _sorter.Sort(products, param);
return sortStrategy.Sort(products);
}
catch
{
throw;
}
}

public List<Product> Filter(List<Product> products, Dictionary<Enum, object> filters)

public List<Product> Filter(List<Product> products, Dictionary<IFilter<Product>, object> filterStrategies)
{
try
{
return _filter.Filter(products, filters);
foreach (var kvp in filterStrategies)
{
var filterStrategy = kvp.Key;
var filterValue = kvp.Value;

products = filterStrategy.Filter(products, filterValue);
}

return products;
}
catch
{
Expand Down
64 changes: 32 additions & 32 deletions FitFusion/Controllers/Sort/ProductSorter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,38 @@ public List<Product> Sort(List<Product> products)
}
}

public class ProductSorter
{
public List<Product> Sort(List<Product> products, Enum sortParam)
{
ISort<Product> sortStrategy;
//public class ProductSorter
//{
// public List<Product> Sort(List<Product> products, Enum sortParam)
// {
// ISort<Product> sortStrategy;

switch (sortParam)
{
case SortParameter.TitleAsc:
sortStrategy = new TitleAscSortStrategy();
break;
case SortParameter.TitleDesc:
sortStrategy = new TitleDescSortStrategy();
break;
case SortParameter.PriceAsc:
sortStrategy = new PriceAscSortStrategy();
break;
case SortParameter.PriceDesc:
sortStrategy = new PriceDescSortStrategy();
break;
default:
return products;
}
// switch (sortParam)
// {
// case SortParameter.TitleAsc:
// sortStrategy = new TitleAscSortStrategy();
// break;
// case SortParameter.TitleDesc:
// sortStrategy = new TitleDescSortStrategy();
// break;
// case SortParameter.PriceAsc:
// sortStrategy = new PriceAscSortStrategy();
// break;
// case SortParameter.PriceDesc:
// sortStrategy = new PriceDescSortStrategy();
// break;
// default:
// return products;
// }

try
{
return sortStrategy.Sort(products);
}
catch
{
throw new ArgumentException("Error during the sorting process.");
}
}
}
// try
// {
// return sortStrategy.Sort(products);
// }
// catch
// {
// throw new ArgumentException("Error during the sorting process.");
// }
// }
//}
}
Loading

0 comments on commit 276f923

Please sign in to comment.