1
0
mirror of synced 2024-11-28 00:20:53 +01:00

Add search to users api

This commit is contained in:
shiibe 2024-06-05 20:38:52 -04:00
parent 8fcd7a1057
commit 682346521e
5 changed files with 34 additions and 12 deletions

View File

@ -37,7 +37,7 @@ public class UsersController(IUserDatumService userDatumService, IAuthService au
[HttpGet]
[ServiceFilter(typeof(AuthorizeIfRequiredAttribute))]
public async Task<ActionResult<UsersResponse>> GetUsers([FromQuery] int page = 1, [FromQuery] int limit = 10)
public async Task<ActionResult<UsersResponse>> GetUsers([FromQuery] int page = 1, [FromQuery] int limit = 10, [FromQuery] string? searchTerm = null)
{
if (page < 1)
{
@ -63,7 +63,7 @@ public class UsersController(IUserDatumService userDatumService, IAuthService au
}
}
return await authService.GetUsersFromCards(page, limit);
return await authService.GetUsersFromCards(page, limit, searchTerm);
}
[HttpDelete("{baid}")]

View File

@ -30,13 +30,25 @@ public class AuthService(TaikoDbContext context) : IAuthService
};
}
public async Task<UsersResponse> GetUsersFromCards(int page = 1, int limit = 12)
public async Task<UsersResponse> GetUsersFromCards(int page = 1, int limit = 12, string? searchTerm = null)
{
// Get the total count of users
var totalUsers = await context.UserData.CountAsync();
var users = new List<User>();
var cardEntries = await context.Cards.ToListAsync();
var userEntriesQuery = context.UserData.AsQueryable();
// If a search term is provided, filter the users based on the search term
if (!string.IsNullOrEmpty(searchTerm))
{
var lowerCaseSearchTerm = searchTerm.ToLower();
userEntriesQuery = userEntriesQuery.Where(user => user.Baid.ToString() == lowerCaseSearchTerm
|| user.MyDonName.ToLower().Contains(lowerCaseSearchTerm)
|| context.Cards.Any(card => card.Baid == user.Baid && card.AccessCode == lowerCaseSearchTerm));
}
// Get the total count of users after applying the search term
var totalUsers = await userEntriesQuery.CountAsync();
// Calculate the total pages
var totalPages = totalUsers / limit;
@ -46,8 +58,7 @@ public class AuthService(TaikoDbContext context) : IAuthService
totalPages++;
}
var cardEntries = await context.Cards.ToListAsync();
var userEntries = await context.UserData
var userEntries = await userEntriesQuery
.OrderBy(user => user.Baid)
.Skip((page - 1) * limit)
.Take(limit)

View File

@ -9,7 +9,7 @@ public interface IAuthService
public Task<Card?> GetCardByAccessCode(string accessCode);
public Task<UsersResponse> GetUsersFromCards(int page = 0, int limit = 12);
public Task<UsersResponse> GetUsersFromCards(int page = 0, int limit = 12, string? searchTerm = null);
public Task AddCard(Card card);

View File

@ -9,7 +9,10 @@
<MudText Typo="Typo.h4">@Localizer["Users"]</MudText>
<MudGrid Class="my-8">
@if (!AuthService.LoginRequired || (AuthService.LoginRequired && AuthService.IsAdmin)) {
if (isLoading == true || response == null)
<MudItem xs="12">
<MudInput Style="width: 100%" @bind-Value="searchTerm" Clearable="true" FullWidth="true" TextChanged="(term => OnSearch(term))" Margin="Margin.Dense" Placeholder="@Localizer["User ID, Name, or Access Code"]" Label="@Localizer["Search"]" Variant="Variant.Outlined" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.Search" />
</MudItem>
@if (isLoading || response == null)
{
// Loading...
for (uint i = 0; i < pageSize; i++) {

View File

@ -7,12 +7,13 @@ public partial class Users
private int TotalPages { get; set; } = 0;
private bool isLoading = true;
private int currentPage = 1;
private int pageSize = 12;
private readonly int pageSize = 12;
private string? searchTerm = null;
private async Task GetUsersData()
{
isLoading = true;
response = await Client.GetFromJsonAsync<UsersResponse>($"api/Users?page={currentPage}&limit={pageSize}");
response = await Client.GetFromJsonAsync<UsersResponse>($"api/Users?page={currentPage}&limit={pageSize}&searchTerm={searchTerm}");
response.ThrowIfNull();
TotalPages = response.TotalPages;
@ -38,4 +39,11 @@ public partial class Users
currentPage = page;
await GetUsersData();
}
private async Task OnSearch(string search)
{
searchTerm = search;
currentPage = 1;
await GetUsersData();
}
}