Update game
All checks were successful
Game Ideas build for PR / build_test (pull_request) Successful in 50s

This commit is contained in:
Maxime Adler
2025-05-12 16:25:28 +02:00
parent 14dc1928bc
commit 5d30c2353e
14 changed files with 135 additions and 23 deletions

View File

@@ -1,6 +1,9 @@
using GameIdeas.BlazorApp.Shared.Components;
using GameIdeas.BlazorApp.Shared.Exceptions;
using GameIdeas.Resources;
using GameIdeas.Shared.Dto;
using Microsoft.AspNetCore.Components;
using System.Linq.Expressions;
namespace GameIdeas.BlazorApp.Pages.Detail;
@@ -13,7 +16,7 @@ public partial class GameDetail : GameBaseComponent
protected override async Task OnInitializedAsync()
{
Game = await GameGateway.GetGameById(GameId);
await FetchGameDetail();
await base.OnInitializedAsync();
}
@@ -21,4 +24,22 @@ public partial class GameDetail : GameBaseComponent
{
NavigationManager.NavigateTo("/");
}
private async Task FetchGameDetail()
{
try
{
IsLoading = true;
Game = await GameGateway.GetGameById(GameId);
}
catch (Exception)
{
throw new FetchGameDetailException(ResourcesKey.ErrorFetchDetail);
}
finally
{
IsLoading = false;
}
}
}

View File

@@ -11,7 +11,7 @@
<div class="container">
<div class="input-game">
<div id="first-label" class="label">@ResourcesKey.Title :</div>
<InputText class="title" @bind-Value=GameDto.Title/>
<InputText class="title" @bind-Value=GameDto.Title/>
</div>
<div class="input-game">
<div class="label">@ResourcesKey.ReleaseDate :</div>
@@ -44,21 +44,21 @@
<div class="input-game">
<div class="label">@ResourcesKey.Properties :</div>
<SelectSearch TItem="PropertyDto" Theme="Theme" GetLabel="@(i => i.Label)" QuickAdd=true
Items="Categories?.Properties" @bind-Values=GameDto.Properties
AddItem="@(str => new PropertyDto() { Label = str })" />
Items="Categories?.Properties" @bind-Values=GameDto.Properties
AddItem="@(str => new PropertyDto() { Label = str })" />
</div>
<div class="input-game">
<div class="label">@ResourcesKey.Tags :</div>
<SelectSearch TItem="TagDto" Theme="Theme" GetLabel="@(i => i.Label)" QuickAdd=true
Items="Categories?.Tags" @bind-Values=GameDto.Tags
AddItem="@(str => new TagDto() { Label = str })" />
Items="Categories?.Tags" @bind-Values=GameDto.Tags
AddItem="@(str => new TagDto() { Label = str })" />
</div>
<div class="input-game">
<div class="label">@ResourcesKey.Platforms :</div>
<SelectSearch TItem="PlatformDto" Theme="Theme" GetLabel="@(i => i.Label)" QuickAdd=true
Items="Categories?.Platforms" @bind-Values=GameDto.Platforms
AddItem="@(str => new PlatformDto() { Label = str })" />
Items="Categories?.Platforms" @bind-Values=GameDto.Platforms
AddItem="@(str => new PlatformDto() { Label = str })" />
</div>
@foreach (var platform in GameDto.Platforms ?? [])

View File

@@ -3,6 +3,8 @@ using GameIdeas.BlazorApp.Pages.Games.Gateways;
using GameIdeas.BlazorApp.Shared.Components.Popup;
using GameIdeas.BlazorApp.Shared.Components.Select.Models;
using GameIdeas.BlazorApp.Shared.Components.Slider;
using GameIdeas.BlazorApp.Shared.Exceptions;
using GameIdeas.Resources;
using GameIdeas.Shared.Dto;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
@@ -19,22 +21,25 @@ public partial class GameCreationForm
[CascadingParameter] private Popup? Popup { get; set; }
[Parameter] public CategoriesDto? Categories { get; set; }
[Parameter] public EventCallback OnSubmit { get; set; }
private readonly GameDetailDto GameDto = new();
[Parameter] public int? GameIdToUpdate { get; set; }
private GameDetailDto GameDto = new();
private EditContext? EditContext;
private readonly SelectTheme Theme = SelectTheme.Creation;
private readonly SliderParams SliderParams = new() { Gap = 1, Min = 1, Max = 5 };
private bool IsLoading = false;
protected override async Task OnInitializedAsync()
protected override void OnInitialized()
{
EditContext = new(GameDto);
await base.OnInitializedAsync();
IsLoading = GameIdToUpdate != null;
base.OnInitialized();
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await Js.InvokeVoidAsync("resizeGameForm");
await SetGameToUpdate();
}
private void HandleOnCancel()
@@ -53,15 +58,22 @@ public partial class GameCreationForm
{
IsLoading = true;
int gameId;
var authState = await AuthenticationState.GetAuthenticationStateAsync();
GameHelper.WriteTrackingDto(GameDto, authState);
var gameId = await GameGateway.CreateGame(GameDto);
if (gameId != 0)
if (GameDto.Id != null)
{
Popup?.Close();
await OnSubmit.InvokeAsync();
gameId = await GameGateway.UpdateGame(GameDto);
}
else
{
gameId = await GameGateway.CreateGame(GameDto);
}
if (gameId == 0)
{
throw new GameCreationException(ResourcesKey.ErrorCreateGame);
}
}
catch (Exception)
@@ -73,13 +85,45 @@ public partial class GameCreationForm
IsLoading = false;
StateHasChanged();
}
Popup?.Close();
await OnSubmit.InvokeAsync();
}
private void HandlePublisherChanged(List<PublisherDto> pubs)
{
GameDto.Publisher = pubs.FirstOrDefault();
}
private void HandleDeveloperChanged(List<DeveloperDto> devs)
{
GameDto.Developer = devs.FirstOrDefault();
}
private async Task SetGameToUpdate()
{
if (GameIdToUpdate == null)
{
return;
}
try
{
IsLoading = true;
GameDto = await GameGateway.GetGameById(GameIdToUpdate ?? 0);
}
catch (Exception)
{
throw new FetchGameDetailException(ResourcesKey.ErrorFetchDetail);
}
finally
{
IsLoading = false;
StateHasChanged();
}
EditContext = new(GameDto);
}
}

View File

@@ -42,7 +42,7 @@
</div>
<Popup @ref=ManualAddPopup BackdropFilterClicked="HandleBackdropManualAddClicked" Closable=false>
<GameCreationForm Categories="Categories" OnSubmit="() => HandleFetchDatas()" />
<GameCreationForm GameIdToUpdate="@GameIdToUpdate" Categories="Categories" OnSubmit="() => HandleFetchDatas()" />
</Popup>
<Popup @ref=DeletePopup Closable=false>

View File

@@ -1,3 +1,4 @@
using GameIdeas.BlazorApp.Pages.Games.Components;
using GameIdeas.BlazorApp.Pages.Games.Filter;
using GameIdeas.BlazorApp.Shared.Components;
using GameIdeas.BlazorApp.Shared.Components.Popup;
@@ -15,6 +16,7 @@ public partial class Games : GameBaseComponent
private int CurrentPage;
private Popup? DeletePopup;
private GameDto? GameToDelete;
private int? GameIdToUpdate;
protected override async Task OnInitializedAsync()
{
@@ -64,9 +66,16 @@ public partial class Games : GameBaseComponent
GameToDelete = null;
}
private Task HandleEditGame(GameDto args)
private void HandleEditGame(GameDto args)
{
throw new NotImplementedException();
if (args.Id == null)
{
return;
}
GameIdToUpdate = args.Id;
ManualAddPopup?.Open();
}
private async Task HandleRemoveGame()
@@ -87,7 +96,6 @@ public partial class Games : GameBaseComponent
}
catch (Exception)
{
throw;
}

View File

@@ -88,4 +88,16 @@ public class GameGateway(IHttpClientService httpClientService) : IGameGateway
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);
}
}
}

View File

@@ -10,4 +10,5 @@ public interface IGameGateway
Task<IEnumerable<GameDto>> FetchGames(GameFilterParams filter, int currentPage);
Task<GameDetailDto> GetGameById(int gameId);
Task<bool> DeleteGame(int gameIdToDelete);
Task<int> UpdateGame(GameDetailDto gameDto);
}