From b53bcc17ca41374db6ba2e17380460956aed4932 Mon Sep 17 00:00:00 2001 From: Samuel Montambault Date: Fri, 5 Apr 2024 18:54:11 -0400 Subject: [PATCH] add filter flag (#39) --- core/Controllers/OrganizersController.cs | 5 +++-- core/Data/Enums/OrganizerAccountActiveFilter.cs | 13 +++++++++++++ core/Services/Abstractions/IUserService.cs | 3 ++- core/Services/UserService.cs | 12 ++++++++---- 4 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 core/Data/Enums/OrganizerAccountActiveFilter.cs diff --git a/core/Controllers/OrganizersController.cs b/core/Controllers/OrganizersController.cs index d5cca55..10f8ce5 100644 --- a/core/Controllers/OrganizersController.cs +++ b/core/Controllers/OrganizersController.cs @@ -1,5 +1,6 @@ using api.core.data.entities; using api.core.Data; +using api.core.Data.Enums; using api.core.Data.Exceptions; using api.core.Data.requests; using api.core.Data.Requests; @@ -61,10 +62,10 @@ await emailService.SendEmailAsync( } [HttpGet] - public IActionResult GetUsers(string? search, [FromQuery] PaginationRequest pagination) + public IActionResult GetUsers(string? search, OrganizerAccountActiveFilter filter, [FromQuery] PaginationRequest pagination) { var validFilter = new PaginationRequest(pagination.PageNumber, pagination.PageSize); - var users = userService.GetUsers(search, out int totalRecords); + var users = userService.GetUsers(search, filter, out int totalRecords); var paginatedRes = users .Skip((pagination.PageNumber - 1) * pagination.PageSize) diff --git a/core/Data/Enums/OrganizerAccountActiveFilter.cs b/core/Data/Enums/OrganizerAccountActiveFilter.cs new file mode 100644 index 0000000..a3a2658 --- /dev/null +++ b/core/Data/Enums/OrganizerAccountActiveFilter.cs @@ -0,0 +1,13 @@ +namespace api.core.Data.Enums; + +/// +/// Filter the organizer accounts by their active status +/// +[Flags] +public enum OrganizerAccountActiveFilter +{ + None = 0, + Active = 1, + Inactive = 2, + All = Active | Inactive +} diff --git a/core/Services/Abstractions/IUserService.cs b/core/Services/Abstractions/IUserService.cs index fb43b76..be4098c 100644 --- a/core/Services/Abstractions/IUserService.cs +++ b/core/Services/Abstractions/IUserService.cs @@ -1,5 +1,6 @@ using System.Diagnostics.Tracing; +using api.core.Data.Enums; using api.core.Data.requests; using api.core.Data.Responses; @@ -13,7 +14,7 @@ public interface IUserService public string GetUserAvatarUrl(Guid id); - public IEnumerable GetUsers(string? search, out int count); + public IEnumerable GetUsers(string? search, OrganizerAccountActiveFilter activeFilter, out int count); public bool UpdateUser(Guid id, UserUpdateDTO dto); diff --git a/core/Services/UserService.cs b/core/Services/UserService.cs index 70fc03f..36aa1ca 100644 --- a/core/Services/UserService.cs +++ b/core/Services/UserService.cs @@ -1,5 +1,6 @@ using api.core.data.entities; using api.core.Data.Exceptions; +using api.core.Data.Enums; using api.core.Data.requests; using api.core.Data.Responses; using api.core.repositories; @@ -81,12 +82,15 @@ public string GetUserAvatarUrl(Guid id) return avatarUri.ToString(); } - public IEnumerable GetUsers(string? search, out int count) + public IEnumerable GetUsers(string? search, OrganizerAccountActiveFilter activeFilter, out int count) { var organizers = organizerRepository.GetAll() - .Where(x => search.IsNullOrEmpty() || - x.Organization.ToLower().Contains(search.ToLower() ?? "") || - x.Email.ToLower().Contains(search.ToLower() ?? "") + .Where(x => (search.IsNullOrEmpty() || + x.Organization.ToLower().Contains(search!.ToLower() ?? "") || + x.Email.ToLower().Contains(search!.ToLower() ?? "")) && + ((activeFilter.HasFlag(OrganizerAccountActiveFilter.Active) && x.IsActive) || + (activeFilter.HasFlag(OrganizerAccountActiveFilter.Inactive) && !x.IsActive) || + activeFilter.HasFlag(OrganizerAccountActiveFilter.All)) ); count = organizers.Count();