Skip to content

Commit

Permalink
New sorting implementation in the users classes
Browse files Browse the repository at this point in the history
  • Loading branch information
KaloyankerR committed Dec 28, 2023
1 parent c1d87f6 commit 82e884d
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 96 deletions.
58 changes: 29 additions & 29 deletions FitFusion/Controllers/Sort/ProductSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Services.Sort
//{
//}

public class ProductSorter // : ISort<Product>
public class ProductSorter : ISort<Product>
{
public List<Product> Sort(List<Product> products, string param)
{
Expand All @@ -33,36 +33,36 @@ public List<Product> Sort(List<Product> products, string param)
}
}

public class SortProductByTitleAscending: ISort<Product>
{
public List<Product> Sort(List<Product> products)
{
return products.OrderBy(o => o.Title).ToList();
}
}
//public class SortProductByTitleAscending: ISort<Product>
//{
// public List<Product> Sort(List<Product> products)
// {
// return products.OrderBy(o => o.Title).ToList();
// }
//}

public class SortProductByTitleDescending : ISort<Product>
{
public List<Product> Sort(List<Product> products)
{
return products.OrderByDescending(o => o.Title).ToList();
}
}
//public class SortProductByTitleDescending : ISort<Product>
//{
// public List<Product> Sort(List<Product> products)
// {
// return products.OrderByDescending(o => o.Title).ToList();
// }
//}

public class SortProductByPriceDescending : ISort<Product>
{
public List<Product> Sort(List<Product> products)
{
return products.OrderByDescending(o => o.Price).ToList();
}
}
//public class SortProductByPriceDescending : ISort<Product>
//{
// public List<Product> Sort(List<Product> products)
// {
// return products.OrderByDescending(o => o.Price).ToList();
// }
//}

public class SortProductByPriceAscending : ISort<Product>
{
public List<Product> Sort(List<Product> products)
{
return products.OrderBy(o => o.Price).ToList();
}
}
//public class SortProductByPriceAscending : ISort<Product>
//{
// public List<Product> Sort(List<Product> products)
// {
// return products.OrderBy(o => o.Price).ToList();
// }
//}

}
84 changes: 53 additions & 31 deletions FitFusion/Controllers/Sort/UserSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,65 @@ namespace Services.Sort
//{
//}

public class SortUserByFirstNameAscending : ISort<User>
public class UserSorter : ISort<User>
{
public List<User> Sort(List<User> users)
public List<User> Sort(List<User> users, string param)
{
return users.OrderBy(x => x.FirstName).ToList();
switch (param)
{
case "FirstNameAscending":
return users.OrderBy(x => x.FirstName).ToList();
case "FirstNameDescending":
return users.OrderByDescending(x => x.FirstName).ToList();
case "LastNameAscending":
return users.OrderBy(x => x.LastName).ToList();
case "LastNameDescending":
return users.OrderByDescending(x => x.LastName).ToList();
case "Role":
return users.OrderBy(x => x.GetUserRole()).ToList();
default:
return users;
}
}
}

public class SortUserByFirstNameDescending : ISort<User>
{
public List<User> Sort(List<User> users)
{
return users.OrderByDescending(x => x.FirstName).ToList();
}
}
//public class SortUserByFirstNameAscending : ISort<User>
//{
// public List<User> Sort(List<User> users)
// {
// return users.OrderBy(x => x.FirstName).ToList();
// }
//}

public class SortUserByLastNameAscending : ISort<User>
{
public List<User> Sort(List<User> users)
{
return users.OrderBy(x => x.LastName).ToList();
}
}
//public class SortUserByFirstNameDescending : ISort<User>
//{
// public List<User> Sort(List<User> users)
// {
// return users.OrderByDescending(x => x.FirstName).ToList();
// }
//}

public class SortUserByLastNameDescending : ISort<User>
{
public List<User> Sort(List<User> users)
{
return users.OrderByDescending(x => x.LastName).ToList();
}
}
//public class SortUserByLastNameAscending : ISort<User>
//{
// public List<User> Sort(List<User> users)
// {
// return users.OrderBy(x => x.LastName).ToList();
// }
//}

public class SortUserByRole : ISort<User>
{
public List<User> Sort(List<User> users)
{
return users.OrderBy(x => x.GetUserRole()).ToList();
}
}
//public class SortUserByLastNameDescending : ISort<User>
//{
// public List<User> Sort(List<User> users)
// {
// return users.OrderByDescending(x => x.LastName).ToList();
// }
//}

//public class SortUserByRole : ISort<User>
//{
// public List<User> Sort(List<User> users)
// {
// return users.OrderBy(x => x.GetUserRole()).ToList();
// }
//}
}
53 changes: 29 additions & 24 deletions FitFusion/Controllers/UserManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ namespace Services
public class UserManager : IUser
{
private readonly IUser dao;
private ISort<User> _sort;
// private ISort<User> _sort;
private UserSorter _sorter;

public UserManager(IUser userDao, ISort<User> sort)
public UserManager(IUser userDao, UserSorter sorter)
{
dao = userDao;
_sort = sort;
// _sort = new SortUserByFirstNameAscending();
_sorter = sorter;
}

public bool CreateUser(UserModel user)
Expand Down Expand Up @@ -132,28 +132,33 @@ public List<UserModel> Search(List<UserModel> users, string param)

public List<UserModel> Sort(List<UserModel> users, SortParameter param)
{
switch (param)
{
case SortParameter.FirstNameAscending:
_sort = new SortUserByFirstNameAscending();
break;
case SortParameter.LastNameAscending:
_sort = new SortUserByLastNameAscending();
break;
case SortParameter.FirstNameDescending:
_sort = new SortUserByFirstNameDescending();
break;
case SortParameter.LastNameDescending:
_sort = new SortUserByLastNameDescending();
break;
default:
_sort = new SortUserByFirstNameAscending();
break;
}

return _sort.Sort(users);
return _sorter.Sort(users, param.ToString());
}

//public List<UserModel> Sort(List<UserModel> users, SortParameter param)
//{
// switch (param)
// {
// case SortParameter.FirstNameAscending:
// _sort = new SortUserByFirstNameAscending();
// break;
// case SortParameter.LastNameAscending:
// _sort = new SortUserByLastNameAscending();
// break;
// case SortParameter.FirstNameDescending:
// _sort = new SortUserByFirstNameDescending();
// break;
// case SortParameter.LastNameDescending:
// _sort = new SortUserByLastNameDescending();
// break;
// default:
// _sort = new SortUserByFirstNameAscending();
// break;
// }

// return _sort.Sort(users);
//}


public List<string> EncryptPassword(string password)
{
Expand Down
2 changes: 1 addition & 1 deletion FitFusion/FitFusionWeb/Pages/Account.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class AccountModel : PageModel
{
[BindProperty]
public User? CurrentUser { get; set; }
private UserManager _userManager = new(new DataAcess.UserDAO(), new SortUserByFirstNameAscending());
private UserManager _userManager = new(new DataAcess.UserDAO(), new UserSorter());

public IActionResult OnGet()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class LoginModel : PageModel
[BindProperty]
public bool RememberMe { get; set; }

private readonly UserManager _userManager = new(new UserDAO(), new SortUserByFirstNameAscending());
private readonly UserManager _userManager = new(new UserDAO(), new UserSorter());

private readonly ILogger<ErrorModel> _logger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class RegisterModel : PageModel
{
[BindProperty]
public Customer Customer { get; set; } = new();
private readonly UserManager _userManager = new UserManager(new UserDAO(), new SortUserByFirstNameAscending());
private readonly UserManager _userManager = new UserManager(new UserDAO(), new UserSorter());
private readonly ILogger<ErrorModel> _logger;

public RegisterModel(ILogger<ErrorModel> logger)
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 @@ -21,7 +21,7 @@ namespace FitFusionWeb.Pages
public class CartModel : PageModel
{
public readonly ProductManager _productManager = new(new ProductDAO(), new FilterByCategory(), new ProductSorter());
private readonly UserManager _userManager = new(new UserDAO(), new SortUserByFirstNameAscending());
private readonly UserManager _userManager = new(new UserDAO(), new UserSorter());
private readonly OrderManager _orderManager = new(new OrderDAO());

[BindProperty]
Expand Down
2 changes: 1 addition & 1 deletion FitFusion/FitFusionWeb/Pages/Users/All.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class AllModel : PageModel
public SortParameter Sort { get; set; }

public List<User> Users { get; set; } = new List<User>();
private readonly UserManager _userManager = new(new UserDAO(), new SortUserByFirstNameAscending());
private readonly UserManager _userManager = new(new UserDAO(), new UserSorter());

public IActionResult OnGet()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CustomerModel : PageModel
{
[BindProperty]
public Customer Customer { get; set; } = new();
private readonly UserManager _usermanager = new(new UserDAO(), new SortUserByFirstNameAscending());
private readonly UserManager _usermanager = new(new UserDAO(), new UserSorter());

public void OnGet()
{
Expand Down
2 changes: 1 addition & 1 deletion FitFusion/FitFusionWeb/Pages/Users/Create/Owner.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class OwnerModel : PageModel
{
[BindProperty]
public Owner Owner { get; set; } = new();
private readonly UserManager _usermanager = new(new UserDAO(), new SortUserByFirstNameAscending());
private readonly UserManager _usermanager = new(new UserDAO(), new UserSorter());

public void OnGet()
{
Expand Down
2 changes: 1 addition & 1 deletion FitFusion/FitFusionWeb/Pages/Users/Create/Staff.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class StaffModel : PageModel
{
[BindProperty]
public Staff Staff { get; set; } = new();
private readonly UserManager _usermanager = new(new UserDAO(), new SortUserByFirstNameAscending());
private readonly UserManager _usermanager = new(new UserDAO(), new UserSorter());

public void OnGet()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class CustomerModel : PageModel
public int Id { get; set; }
[BindProperty]
public Customer Customer { get; set; } = new();
private readonly UserManager _usermanager = new(new UserDAO(), new SortUserByFirstNameAscending());
private readonly UserManager _usermanager = new(new UserDAO(), new UserSorter());

public void OnGet()
{
Expand Down
2 changes: 1 addition & 1 deletion FitFusion/FitFusionWeb/Pages/Users/Update/Owner.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class OwnerModel : PageModel
public int Id { get; set; }
[BindProperty]
public Owner Owner { get; set; } = new();
private readonly UserManager _usermanager = new(new UserDAO(), new SortUserByFirstNameAscending());
private readonly UserManager _usermanager = new(new UserDAO(), new UserSorter());

public void OnGet()
{
Expand Down
2 changes: 1 addition & 1 deletion FitFusion/FitFusionWeb/Pages/Users/Update/Staff.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class StaffModel : PageModel
public int Id { get; set; }
[BindProperty]
public Staff Staff { get; set; } = new();
private readonly UserManager _usermanager = new(new UserDAO(), new SortUserByFirstNameAscending());
private readonly UserManager _usermanager = new(new UserDAO(), new UserSorter());

public void OnGet()
{
Expand Down
2 changes: 1 addition & 1 deletion FitFusion/Interfaces/Strategy/ISort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ namespace Interfaces.Strategy
{
public interface ISort<T>
{
List<T> Sort(List<T> elements);
List<T> Sort(List<T> elements, string param);
}
}

0 comments on commit 82e884d

Please sign in to comment.