Skip to content

Commit

Permalink
add filter flag (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
MysticFragilist authored Apr 5, 2024
1 parent 36fb571 commit b53bcc1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
5 changes: 3 additions & 2 deletions core/Controllers/OrganizersController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions core/Data/Enums/OrganizerAccountActiveFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace api.core.Data.Enums;

/// <summary>
/// Filter the organizer accounts by their active status
/// </summary>
[Flags]
public enum OrganizerAccountActiveFilter
{
None = 0,
Active = 1,
Inactive = 2,
All = Active | Inactive
}
3 changes: 2 additions & 1 deletion core/Services/Abstractions/IUserService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics.Tracing;

using api.core.Data.Enums;
using api.core.Data.requests;
using api.core.Data.Responses;

Expand All @@ -13,7 +14,7 @@ public interface IUserService

public string GetUserAvatarUrl(Guid id);

public IEnumerable<UserResponseDTO> GetUsers(string? search, out int count);
public IEnumerable<UserResponseDTO> GetUsers(string? search, OrganizerAccountActiveFilter activeFilter, out int count);

public bool UpdateUser(Guid id, UserUpdateDTO dto);

Expand Down
12 changes: 8 additions & 4 deletions core/Services/UserService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -81,12 +82,15 @@ public string GetUserAvatarUrl(Guid id)
return avatarUri.ToString();
}

public IEnumerable<UserResponseDTO> GetUsers(string? search, out int count)
public IEnumerable<UserResponseDTO> 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();

Expand Down

0 comments on commit b53bcc1

Please sign in to comment.