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 CreateUser(UserDto user) { try { return await httpClient.PostAsync(Endpoints.User.Create, user) ?? throw new InvalidOperationException(ResourcesKey.ErrorCreateUser); } catch (Exception) { throw new UserCreationException(ResourcesKey.ErrorCreateUser); } } public async Task DeleteUser(string userId) { try { return await httpClient.DeleteAsync(Endpoints.User.Delete(userId)) ?? throw new InvalidOperationException(ResourcesKey.ErrorDeleteUser); } catch (Exception) { throw new UserCreationException(ResourcesKey.ErrorDeleteUser); } } public async Task> GetRoles() { try { return await httpClient.FetchDataAsync>(Endpoints.User.Roles) ?? throw new InvalidOperationException(ResourcesKey.ErrorFetchRoles); } catch (Exception) { throw new RoleNotFoundException(ResourcesKey.ErrorFetchRoles); } } public async Task 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(url) ?? throw new InvalidOperationException(ResourcesKey.ErrorFetchUsers); } catch (Exception) { throw new UserNotFoundException(ResourcesKey.ErrorFetchUsers); } } public async Task UpdateUser(UserDto user) { try { return await httpClient.PutAsync(Endpoints.User.Update(user.Id ?? string.Empty), user) ?? throw new InvalidOperationException(ResourcesKey.ErrorUpdateUser); } catch (Exception) { throw new UserCreationException(ResourcesKey.ErrorUpdateUser); } } }