Files
game-ideas/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Gateways/GameGateway.cs
Maxime Adler 5d30c2353e
All checks were successful
Game Ideas build for PR / build_test (pull_request) Successful in 50s
Update game
2025-05-12 16:25:28 +02:00

104 lines
3.4 KiB
C#

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<int> CreateGame(GameDetailDto game)
{
try
{
return await httpClientService.PostAsync<int>(Endpoints.Game.Create, game);
}
catch (Exception)
{
throw new GameCreationException(ResourcesKey.ErrorCreateGame);
}
}
public async Task<CategoriesDto> FetchCategories()
{
try
{
var result = await httpClientService.FetchDataAsync<CategoriesDto>(Endpoints.Category.AllCategories);
return result ?? throw new InvalidOperationException(ResourcesKey.ErrorFetchCategories);
}
catch (Exception)
{
throw new CategoryNotFoundException(ResourcesKey.ErrorFetchCategories);
}
}
public async Task<IEnumerable<GameDto>> 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<IEnumerable<GameDto>>(Endpoints.Game.Fetch(filter));
return result ?? throw new InvalidOperationException(ResourcesKey.ErrorFetchGames);
}
catch (Exception)
{
throw new GameNotFoundException(ResourcesKey.ErrorFetchGames);
}
}
public async Task<GameDetailDto> GetGameById(int gameId)
{
try
{
var result = await httpClientService.FetchDataAsync<GameDetailDto>(Endpoints.Game.FetchById(gameId));
return result ?? throw new InvalidOperationException(ResourcesKey.ErrorFetchGames);
}
catch (Exception)
{
throw new CategoryNotFoundException(ResourcesKey.ErrorFetchGames);
}
}
public async Task<bool> DeleteGame(int gameIdToDelete)
{
try
{
return await httpClientService.DeleteAsync<bool>(Endpoints.Game.Delete(gameIdToDelete));
}
catch (Exception)
{
throw new GameDeletionException(ResourcesKey.ErrorDeleteGame);
}
}
public async Task<int> UpdateGame(GameDetailDto gameDto)
{
try
{
return await httpClientService.PutAsync<int>(Endpoints.Game.Update, gameDto);
}
catch (Exception)
{
throw new GameUpdateException(ResourcesKey.ErrorUpdateGame);
}
}
}