Add filter and gateway logics
All checks were successful
Game Ideas build for PR / build_blazor_app (pull_request) Successful in 39s
All checks were successful
Game Ideas build for PR / build_blazor_app (pull_request) Successful in 39s
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
using GameIdeas.Shared.Dto;
|
||||
|
||||
namespace GameIdeas.BlazorApp.Pages.Users.Filters;
|
||||
|
||||
public class UserFilterParams
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public List<RoleDto>? Roles { get; set; }
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
using GameIdeas.Shared.Dto;
|
||||
using GameIdeas.BlazorApp.Pages.Users.Filters;
|
||||
using GameIdeas.Shared.Dto;
|
||||
|
||||
namespace GameIdeas.BlazorApp.Pages.Users.Gateways;
|
||||
|
||||
public interface IUserGateway
|
||||
{
|
||||
Task<UserListDto> GetUsers(UserFilterParams filterParams);
|
||||
Task<UserListDto> GetUsers(UserFilterParams filterParams, int currentPage);
|
||||
Task<IEnumerable<RoleDto>> GetRoles();
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using GameIdeas.BlazorApp.Services;
|
||||
using GameIdeas.BlazorApp.Pages.Users.Filters;
|
||||
using GameIdeas.BlazorApp.Services;
|
||||
using GameIdeas.BlazorApp.Shared.Constants;
|
||||
using GameIdeas.BlazorApp.Shared.Exceptions;
|
||||
using GameIdeas.Resources;
|
||||
@@ -8,13 +9,28 @@ namespace GameIdeas.BlazorApp.Pages.Users.Gateways;
|
||||
|
||||
public class UserGateway(IHttpClientService httpClient) : IUserGateway
|
||||
{
|
||||
public async Task<UserListDto> GetUsers(UserFilterParams filterParams)
|
||||
public async Task<IEnumerable<RoleDto>> GetRoles()
|
||||
{
|
||||
try
|
||||
{
|
||||
return await httpClient.FetchDataAsync<IEnumerable<RoleDto>>(Endpoints.User.Roles)
|
||||
?? throw new InvalidOperationException(ResourcesKey.ErrorFetchRoles);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw new RoleNotFoundException(ResourcesKey.ErrorFetchRoles);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<UserListDto> GetUsers(UserFilterParams filterParams, int currentPage)
|
||||
{
|
||||
try
|
||||
{
|
||||
UserFilterDto filter = new()
|
||||
{
|
||||
|
||||
CurrentPage = currentPage,
|
||||
Name = filterParams.Name,
|
||||
RoleIds = filterParams.Roles?.Select(r => r.Id)
|
||||
};
|
||||
|
||||
var url = Endpoints.User.Fetch(filter);
|
||||
|
||||
@@ -12,8 +12,9 @@
|
||||
|
||||
<GameHeader DisplayAdd="false">
|
||||
<div class="header-content">
|
||||
<SearchInput Placeholder="@ResourcesKey.EnterUsername" />
|
||||
<SelectSearch TItem="RoleDto" Placeholder="@ResourcesKey.Roles" />
|
||||
<SearchInput Placeholder="@ResourcesKey.EnterUsername" @bind-Text="FilterParams.Name" />
|
||||
<SelectSearch TItem="RoleDto" Placeholder="@ResourcesKey.Roles" @bind-Values="FilterParams.Roles"
|
||||
Items="Roles.ToList()" GetLabel="@(role => role.Name)"/>
|
||||
</div>
|
||||
</GameHeader>
|
||||
|
||||
@@ -21,7 +22,7 @@
|
||||
<div class="content">
|
||||
@if (!IsLoading)
|
||||
{
|
||||
@foreach (var user in UsersDto)
|
||||
@foreach (var user in UserList.Users ?? [])
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -1,16 +1,46 @@
|
||||
using GameIdeas.BlazorApp.Pages.Users.Filters;
|
||||
using GameIdeas.BlazorApp.Pages.Users.Gateways;
|
||||
using GameIdeas.BlazorApp.Shared.Components.Popup;
|
||||
using GameIdeas.Shared.Dto;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
|
||||
namespace GameIdeas.BlazorApp.Pages.Users;
|
||||
|
||||
public partial class Users
|
||||
{
|
||||
[Inject] private IUserGateway UserGateway { get; set; } = default!;
|
||||
|
||||
private Popup? Popup;
|
||||
private bool IsLoading = false;
|
||||
private IEnumerable<UserDto> UsersDto = [];
|
||||
private UserFilterParams FilterParams = new();
|
||||
private UserListDto UserList = new();
|
||||
private IEnumerable<RoleDto> Roles = [];
|
||||
private int CurrentPage = 1;
|
||||
|
||||
private void HandleBackdropClicked()
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
Popup?.Close();
|
||||
await FetchData();
|
||||
await base.OnInitializedAsync();
|
||||
}
|
||||
|
||||
private async Task FetchData(bool fetchRoles = true)
|
||||
{
|
||||
try
|
||||
{
|
||||
IsLoading = true;
|
||||
|
||||
if (fetchRoles)
|
||||
Roles = await UserGateway.GetRoles();
|
||||
|
||||
UserList = await UserGateway.GetUsers(FilterParams, CurrentPage);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsLoading = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,5 +24,6 @@ public static class Endpoints
|
||||
public static class User
|
||||
{
|
||||
public static string Fetch(UserFilterDto filter) => $"api/User?{UrlHelper.BuildUrlParams(filter)}";
|
||||
public const string Roles = "api/User/Roles";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace GameIdeas.BlazorApp.Shared.Exceptions;
|
||||
|
||||
public class RoleNotFoundException(string message) : Exception(message);
|
||||
@@ -55,6 +55,8 @@ public class Translations (TranslationService translationService)
|
||||
public string UserLoginFailed => translationService.Translate(nameof(UserLoginFailed));
|
||||
public string UserLogoutFailed => translationService.Translate(nameof(UserLogoutFailed));
|
||||
public string Roles => translationService.Translate(nameof(Roles));
|
||||
public string ErrorFetchUsers => translationService.Translate(nameof(ErrorFetchUsers));
|
||||
public string ErrorFetchRoles => translationService.Translate(nameof(ErrorFetchRoles));
|
||||
}
|
||||
|
||||
public static class ResourcesKey
|
||||
@@ -118,4 +120,6 @@ public static class ResourcesKey
|
||||
public static string UserLoginFailed => _instance?.UserLoginFailed ?? throw new InvalidOperationException("ResourcesKey.UserLoginFailed is not initialized.");
|
||||
public static string UserLogoutFailed => _instance?.UserLogoutFailed ?? throw new InvalidOperationException("ResourcesKey.UserLogoutFailed is not initialized.");
|
||||
public static string Roles => _instance?.Roles ?? throw new InvalidOperationException("ResourcesKey.Roles is not initialized.");
|
||||
public static string ErrorFetchUsers => _instance?.ErrorFetchUsers ?? throw new InvalidOperationException("ResourcesKey.ErrorFetchUsers is not initialized.");
|
||||
public static string ErrorFetchRoles => _instance?.ErrorFetchRoles ?? throw new InvalidOperationException("ResourcesKey.ErrorFetchRoles is not initialized.");
|
||||
}
|
||||
@@ -50,5 +50,7 @@
|
||||
"UserUnauthorized": "Utilisateur non authorisé",
|
||||
"UserLoginFailed": "Authentification de l'utilisateur échoué",
|
||||
"UserLogoutFailed": "Déconnection de l'utilisateur échoué",
|
||||
"Roles": "Rôles"
|
||||
"Roles": "Rôles",
|
||||
"ErrorFetchUsers": "Erreur lors de la récupération des utilisateurs",
|
||||
"ErrorFetchRoles": "Erreur lors de la récupération des rôles"
|
||||
}
|
||||
Reference in New Issue
Block a user