using GameIdeas.BlazorApp.Pages.Games.Filter; 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.Games.Gateways; public class GameGateway(IHttpClientService httpClientService) : IGameGateway { public async Task CreateGame(GameDetailDto game) { try { return await httpClientService.PostAsync(Endpoints.Game.Create, game); } catch (Exception) { throw new GameCreationException(ResourcesKey.ErrorCreateGame); } } public async Task FetchCategories() { try { var result = await httpClientService.FetchDataAsync(Endpoints.Category.AllCategories); return result ?? throw new InvalidOperationException(ResourcesKey.ErrorFetchCategories); } catch (Exception) { throw new CategoryNotFoundException(ResourcesKey.ErrorFetchCategories); } } public async Task> FetchGames(GameFilterParams filterParams, int currentPage) { try { GameFilterDto filter = new() { CurrentPage = currentPage, Title = filterParams.Title, MaxInterest = filterParams.Interest.Max, MinInterest = filterParams.Interest.Min, StorageSpaces = filterParams.StorageSpaceIds, DeveloperIds = filterParams.Developers?.Select(d => d.Id ?? 0).ToList(), PublisherIds = filterParams.Publishers?.Select(d => d.Id ?? 0).ToList(), PlatformIds = filterParams.Platforms?.Select(d => d.Id ?? 0).ToList(), PropertyIds = filterParams.Properties?.Select(d => d.Id ?? 0).ToList(), ReleaseYears = filterParams.ReleaseYears, TagIds = filterParams.Tags?.Select(d => d.Id ?? 0).ToList(), }; var result = await httpClientService.FetchDataAsync>(Endpoints.Game.Fetch(filter)); return result ?? throw new InvalidOperationException(ResourcesKey.ErrorFetchGames); } catch (Exception) { throw new GameNotFoundException(ResourcesKey.ErrorFetchGames); } } public async Task GetGameById(int gameId) { try { var result = await httpClientService.FetchDataAsync(Endpoints.Game.FetchById(gameId)); return result ?? throw new InvalidOperationException(ResourcesKey.ErrorFetchGames); } catch (Exception) { throw new CategoryNotFoundException(ResourcesKey.ErrorFetchGames); } } public async Task DeleteGame(int gameIdToDelete) { try { return await httpClientService.DeleteAsync(Endpoints.Game.Delete(gameIdToDelete)); } catch (Exception) { throw new GameDeletionException(ResourcesKey.ErrorDeleteGame); } } public async Task UpdateGame(GameDetailDto gameDto) { try { return await httpClientService.PutAsync(Endpoints.Game.Update, gameDto); } catch (Exception) { throw new GameUpdateException(ResourcesKey.ErrorUpdateGame); } } }