From 78e67796e55be05506ca9e10a568fada4bf54c13 Mon Sep 17 00:00:00 2001 From: Maxime Adler Date: Tue, 13 May 2025 13:26:38 +0200 Subject: [PATCH] Working update game --- .../Shared/Components/Select/Select.razor | 8 +-- .../Services/Games/GameWriteService.cs | 63 +++++++++++++++++-- 2 files changed, 63 insertions(+), 8 deletions(-) diff --git a/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Select/Select.razor b/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Select/Select.razor index c1cca38..7999f56 100644 --- a/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Select/Select.razor +++ b/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Select/Select.razor @@ -27,9 +27,9 @@ @if (Params.Headers != null) { - @foreach (var header in Params.Headers.UnionBy(HeaderValues ?? [], Params.GetHeaderLabel).OrderBy(Params.GetHeaderLabel)) + @foreach (var header in (HeaderValues ?? []).UnionBy(Params.Headers, Params.GetHeaderLabel).OrderBy(Params.GetHeaderLabel)) { - Params.GetHeaderLabel(val) == Params.GetHeaderLabel(header))) + } @@ -42,9 +42,9 @@ @if (Params.Items != null) { - @foreach (var item in Params.Items.UnionBy(Values ?? [], Params.GetItemLabel).OrderBy(Params.GetItemLabel)) + @foreach (var item in (Values ?? []).UnionBy(Params.Items, Params.GetItemLabel).OrderBy(Params.GetItemLabel)) { - Params.GetItemLabel(val) == Params.GetItemLabel(item))) + } diff --git a/src/GameIdeas/Server/GameIdeas.WebAPI/Services/Games/GameWriteService.cs b/src/GameIdeas/Server/GameIdeas.WebAPI/Services/Games/GameWriteService.cs index 4681b75..4e87bc5 100644 --- a/src/GameIdeas/Server/GameIdeas.WebAPI/Services/Games/GameWriteService.cs +++ b/src/GameIdeas/Server/GameIdeas.WebAPI/Services/Games/GameWriteService.cs @@ -4,6 +4,7 @@ using GameIdeas.Shared.Exceptions; using GameIdeas.Shared.Model; using GameIdeas.WebAPI.Context; using Microsoft.EntityFrameworkCore; +using System.Threading.Tasks; namespace GameIdeas.WebAPI.Services.Games; @@ -13,7 +14,7 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW { var gameToCreate = mapper.Map(gameDto); - HandleDeveloperPublisherCreation(gameToCreate); + await HandleDeveloperPublisherCreation(gameToCreate); await context.Games.AddAsync(gameToCreate); await context.SaveChangesAsync(); @@ -35,7 +36,7 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW var gameToUpdate = mapper.Map(gameDto); - HandleDeveloperPublisherCreation(gameToUpdate); + await HandleDeveloperPublisherCreation(gameToUpdate); await HandlePlatformsCreation(gameDto.Platforms, gameToUpdate.Id); await HandlePropertiesCreation(gameDto.Properties, gameToUpdate.Id); await HandleTagsCreation(gameDto.Tags, gameToUpdate.Id); @@ -48,12 +49,27 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW public async Task DeleteGame(int gameId) { + await HandlePlatformsCreation([], gameId); + await HandlePropertiesCreation([], gameId); + await HandleTagsCreation([], gameId); + var gameToRemove = await context.Games .FirstOrDefaultAsync(g => g.Id == gameId) ?? throw new NotFoundException($"[{typeof(Game).FullName}] with ID {gameId} has not been found in context"); context.Games.Remove(gameToRemove); - return await context.SaveChangesAsync() != 0; + await context.SaveChangesAsync(); + + context.Publishers.RemoveRange( + context.Publishers.Include(p => p.Games) + .Where(p => p.Games.Count == 0)); + + context.Developers.RemoveRange( + context.Developers.Include(d => d.Games) + .Where(d => d.Games.Count == 0)); + + await context.SaveChangesAsync(); + return true; } private async Task HandlePlatformsCreation(IEnumerable? categoriesToCreate, int gameId) @@ -62,6 +78,9 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW { var gps = mapper.Map>(categoriesToCreate); + context.GamePlatforms.RemoveRange( + context.GamePlatforms.Where(gp => gp.GameId == gameId)); + foreach (var gp in gps) { gp.GameId = gameId; @@ -69,6 +88,12 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW context.Platforms.AttachRange(gps.Select(gp => gp.Platform)); await context.GamePlatforms.AddRangeAsync(gps); + + context.Platforms.RemoveRange( + context.Platforms.Include(p => p.GamePlatforms) + .Where(p => p.GamePlatforms.Count == 0)); + + await context.SaveChangesAsync(); } } @@ -78,6 +103,9 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW { var gps = mapper.Map>(categoriesToCreate); + context.GameProperties.RemoveRange( + context.GameProperties.Where(gp => gp.GameId == gameId)); + foreach (var gp in gps) { gp.GameId = gameId; @@ -85,6 +113,12 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW context.Properties.AttachRange(gps.Select(gp => gp.Property)); await context.GameProperties.AddRangeAsync(gps); + + context.Properties.RemoveRange( + context.Properties.Include(p => p.GameProperties) + .Where(p => p.GameProperties.Count == 0)); + + await context.SaveChangesAsync(); } } @@ -94,6 +128,9 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW { var gts = mapper.Map>(categoriesToCreate); + context.GameTags.RemoveRange( + context.GameTags.Where(gt => gt.GameId == gameId)); + foreach (var gt in gts) { gt.GameId = gameId; @@ -101,11 +138,29 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW context.Tags.AttachRange(gts.Select(gt => gt.Tag)); await context.GameTags.AddRangeAsync(gts); + + + context.Tags.RemoveRange( + context.Tags.Include(t => t.GameTags) + .Where(t => t.GameTags.Count == 0)); + + await context.SaveChangesAsync(); + } } - private void HandleDeveloperPublisherCreation(Game? game) + private async Task HandleDeveloperPublisherCreation(Game? game) { + context.Publishers.RemoveRange( + context.Publishers.Include(p => p.Games) + .Where(p => p.Games.Count == 0)); + + context.Developers.RemoveRange( + context.Developers.Include(d => d.Games) + .Where(d => d.Games.Count == 0)); + + await context.SaveChangesAsync(); + if (game?.Publisher != null) { context.Publishers.Attach(game.Publisher);