From ad05e210c297cb5ea023ed7eb0d73378185e8e42 Mon Sep 17 00:00:00 2001 From: Maxime Adler Date: Tue, 15 Apr 2025 11:57:28 +0200 Subject: [PATCH] Create game --- .../GameIdeas.BlazorApp/Helpers/GameHelper.cs | 12 ++++++++++ .../Games/Components/GameCreationForm.razor | 2 +- .../Components/GameCreationForm.razor.cs | 24 +++++++++++++++---- .../Pages/Games/Gateways/GameGateway.cs | 6 ++--- .../Pages/Games/Gateways/IGameGateway.cs | 2 +- .../Select/Components/SelectRow.razor.css | 2 +- .../Shared/Components/Select/Select.razor.css | 3 ++- .../Controllers/GameController.cs | 10 ++++---- 8 files changed, 45 insertions(+), 16 deletions(-) create mode 100644 src/GameIdeas/Client/GameIdeas.BlazorApp/Helpers/GameHelper.cs diff --git a/src/GameIdeas/Client/GameIdeas.BlazorApp/Helpers/GameHelper.cs b/src/GameIdeas/Client/GameIdeas.BlazorApp/Helpers/GameHelper.cs new file mode 100644 index 0000000..d806f8e --- /dev/null +++ b/src/GameIdeas/Client/GameIdeas.BlazorApp/Helpers/GameHelper.cs @@ -0,0 +1,12 @@ +using GameIdeas.Shared.Dto; + +namespace GameIdeas.BlazorApp.Helpers; + +public static class GameHelper +{ + public static void WriteTrackingDto(GameDto game) + { + game.CreationUserId = 100000; + game.CreationDate = DateTime.Now; + } +} diff --git a/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Components/GameCreationForm.razor b/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Components/GameCreationForm.razor index 49c858c..103208d 100644 --- a/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Components/GameCreationForm.razor +++ b/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Components/GameCreationForm.razor @@ -10,7 +10,7 @@
@ResourcesKey.Title :
- +
@ResourcesKey.ReleaseDate :
diff --git a/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Components/GameCreationForm.razor.cs b/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Components/GameCreationForm.razor.cs index 9911a85..1e773d9 100644 --- a/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Components/GameCreationForm.razor.cs +++ b/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Components/GameCreationForm.razor.cs @@ -1,3 +1,4 @@ +using GameIdeas.BlazorApp.Helpers; using GameIdeas.BlazorApp.Pages.Games.Gateways; using GameIdeas.BlazorApp.Shared.Components.Popup; using GameIdeas.BlazorApp.Shared.Components.Select.Models; @@ -20,7 +21,7 @@ public partial class GameCreationForm private EditContext? EditContext; private readonly SelectTheme Theme = SelectTheme.Creation; private readonly SliderParams SliderParams = new() { Gap = 1, Min = 1, Max = 5 }; - private bool IsLoading = true; + private bool IsLoading = false; protected override async Task OnInitializedAsync() { @@ -46,10 +47,25 @@ public partial class GameCreationForm return; } - IsLoading = true; + try + { + IsLoading = true; - await GameGateway.CreateGame(GameDto); + GameHelper.WriteTrackingDto(GameDto); + var gameId = await GameGateway.CreateGame(GameDto); - IsLoading = false; + if (gameId != 0) + { + Popup?.Close(); + } + } + catch (Exception) + { + throw; + } + finally + { + IsLoading = false; + } } } \ No newline at end of file diff --git a/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Gateways/GameGateway.cs b/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Gateways/GameGateway.cs index 1876b5c..34873dc 100644 --- a/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Gateways/GameGateway.cs +++ b/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Gateways/GameGateway.cs @@ -8,13 +8,11 @@ namespace GameIdeas.BlazorApp.Pages.Games.Gateways; public class GameGateway(IHttpClientService httpClientService) : IGameGateway { - public async Task CreateGame(GameDto game) + public async Task CreateGame(GameDto game) { try { - var result = await httpClientService.PostAsync(Endpoints.Game.Create, game); - - return result ?? throw new InvalidOperationException(ResourcesKey.ErrorCreateGame); + return await httpClientService.PostAsync(Endpoints.Game.Create, game); } catch (Exception) { diff --git a/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Gateways/IGameGateway.cs b/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Gateways/IGameGateway.cs index 6b941af..0269bad 100644 --- a/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Gateways/IGameGateway.cs +++ b/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Gateways/IGameGateway.cs @@ -5,5 +5,5 @@ namespace GameIdeas.BlazorApp.Pages.Games.Gateways; public interface IGameGateway { Task FetchCategories(); - Task CreateGame(GameDto game); + Task CreateGame(GameDto game); } diff --git a/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Select/Components/SelectRow.razor.css b/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Select/Components/SelectRow.razor.css index a12a8b5..133a7f1 100644 --- a/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Select/Components/SelectRow.razor.css +++ b/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Select/Components/SelectRow.razor.css @@ -6,10 +6,10 @@ height: 20px; align-items: center; padding: 2px 6px; + cursor: pointer; } .select-element:hover { - cursor: pointer; background: var(--input-selected); } diff --git a/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Select/Select.razor.css b/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Select/Select.razor.css index 2f564c1..90c1264 100644 --- a/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Select/Select.razor.css +++ b/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Select/Select.razor.css @@ -3,7 +3,8 @@ } .button { - z-index: var(--index-component) + z-index: var(--index-component); + cursor: pointer; } .dropdown { diff --git a/src/GameIdeas/Server/GameIdeas.WebAPI/Controllers/GameController.cs b/src/GameIdeas/Server/GameIdeas.WebAPI/Controllers/GameController.cs index 7edf1c8..3fdcb3b 100644 --- a/src/GameIdeas/Server/GameIdeas.WebAPI/Controllers/GameController.cs +++ b/src/GameIdeas/Server/GameIdeas.WebAPI/Controllers/GameController.cs @@ -40,11 +40,12 @@ public class GameController(IGameService gameService, ILoggerFactory loggerFacto } [HttpPost("Create")] - public async Task> CreateGame([FromBody] GameDto game) + public async Task> CreateGame([FromBody] GameDto game) { try { - return Created("/Create", await gameService.CreateGame(game)); + var gameResult = await gameService.CreateGame(game); + return Created("/Create", gameResult.Id); } catch (Exception e) { @@ -54,11 +55,12 @@ public class GameController(IGameService gameService, ILoggerFactory loggerFacto } [HttpPut("Update")] - public async Task> UpdateGame([FromBody] GameDto game) + public async Task> UpdateGame([FromBody] GameDto game) { try { - return Created($"/Update", await gameService.UpdateGame(game)); + var gameResult = await gameService.UpdateGame(game); + return Created($"/Update", gameResult.Id); } catch (Exception e) {