Files
game-ideas/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Users/Gateways/UserGateway.cs
Egamorf 757e9db08d
All checks were successful
Game Ideas build for PR / build_blazor_app (pull_request) Successful in 52s
Crud on users
2025-04-27 19:56:17 +02:00

85 lines
2.4 KiB
C#

using GameIdeas.BlazorApp.Pages.Users.Filters;
using GameIdeas.BlazorApp.Services;
using GameIdeas.BlazorApp.Shared.Constants;
using GameIdeas.BlazorApp.Shared.Exceptions;
using GameIdeas.Resources;
using GameIdeas.Shared.Dto;
namespace GameIdeas.BlazorApp.Pages.Users.Gateways;
public class UserGateway(IHttpClientService httpClient) : IUserGateway
{
public async Task<IdDto> CreateUser(UserDto user)
{
try
{
return await httpClient.PostAsync<IdDto>(Endpoints.User.Create, user)
?? throw new InvalidOperationException(ResourcesKey.ErrorCreateUser);
}
catch (Exception)
{
throw new UserCreationException(ResourcesKey.ErrorCreateUser);
}
}
public async Task<IdDto> DeleteUser(string userId)
{
try
{
return await httpClient.DeleteAsync<IdDto>(Endpoints.User.Delete(userId))
?? throw new InvalidOperationException(ResourcesKey.ErrorDeleteUser);
}
catch (Exception)
{
throw new UserCreationException(ResourcesKey.ErrorDeleteUser);
}
}
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);
return await httpClient.FetchDataAsync<UserListDto>(url)
?? throw new InvalidOperationException(ResourcesKey.ErrorFetchUsers);
}
catch (Exception)
{
throw new UserNotFoundException(ResourcesKey.ErrorFetchUsers);
}
}
public async Task<IdDto> UpdateUser(UserDto user)
{
try
{
return await httpClient.PutAsync<IdDto>(Endpoints.User.Update(user.Id ?? string.Empty), user)
?? throw new InvalidOperationException(ResourcesKey.ErrorUpdateUser);
}
catch (Exception)
{
throw new UserCreationException(ResourcesKey.ErrorUpdateUser);
}
}
}