Working update game

This commit is contained in:
Maxime Adler
2025-05-13 13:26:38 +02:00
parent e78f29ffac
commit 78e67796e5
2 changed files with 63 additions and 8 deletions

View File

@@ -27,9 +27,9 @@
@if (Params.Headers != null) @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))
{ {
<SelectRow IsSelected=@(HeaderValues?.Any(val => Params.GetHeaderLabel(val) == Params.GetHeaderLabel(header))) <SelectRow IsSelected=@(HeaderValues?.Contains(header))
Label="@Params.GetHeaderLabel(header)" Theme=Theme Label="@Params.GetHeaderLabel(header)" Theme=Theme
OnClick="_ => HandleHeaderClicked(header)" /> OnClick="_ => HandleHeaderClicked(header)" />
} }
@@ -42,9 +42,9 @@
@if (Params.Items != null) @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))
{ {
<SelectRow IsSelected=@(Values?.Any(val => Params.GetItemLabel(val) == Params.GetItemLabel(item))) <SelectRow IsSelected=@(Values?.Contains(item))
Label="@Params.GetItemLabel(item)" Theme=Theme Label="@Params.GetItemLabel(item)" Theme=Theme
OnClick="_ => HandleValueClicked(item)" /> OnClick="_ => HandleValueClicked(item)" />
} }

View File

@@ -4,6 +4,7 @@ using GameIdeas.Shared.Exceptions;
using GameIdeas.Shared.Model; using GameIdeas.Shared.Model;
using GameIdeas.WebAPI.Context; using GameIdeas.WebAPI.Context;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;
namespace GameIdeas.WebAPI.Services.Games; namespace GameIdeas.WebAPI.Services.Games;
@@ -13,7 +14,7 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW
{ {
var gameToCreate = mapper.Map<Game>(gameDto); var gameToCreate = mapper.Map<Game>(gameDto);
HandleDeveloperPublisherCreation(gameToCreate); await HandleDeveloperPublisherCreation(gameToCreate);
await context.Games.AddAsync(gameToCreate); await context.Games.AddAsync(gameToCreate);
await context.SaveChangesAsync(); await context.SaveChangesAsync();
@@ -35,7 +36,7 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW
var gameToUpdate = mapper.Map<Game>(gameDto); var gameToUpdate = mapper.Map<Game>(gameDto);
HandleDeveloperPublisherCreation(gameToUpdate); await HandleDeveloperPublisherCreation(gameToUpdate);
await HandlePlatformsCreation(gameDto.Platforms, gameToUpdate.Id); await HandlePlatformsCreation(gameDto.Platforms, gameToUpdate.Id);
await HandlePropertiesCreation(gameDto.Properties, gameToUpdate.Id); await HandlePropertiesCreation(gameDto.Properties, gameToUpdate.Id);
await HandleTagsCreation(gameDto.Tags, gameToUpdate.Id); await HandleTagsCreation(gameDto.Tags, gameToUpdate.Id);
@@ -48,12 +49,27 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW
public async Task<bool> DeleteGame(int gameId) public async Task<bool> DeleteGame(int gameId)
{ {
await HandlePlatformsCreation([], gameId);
await HandlePropertiesCreation([], gameId);
await HandleTagsCreation([], gameId);
var gameToRemove = await context.Games var gameToRemove = await context.Games
.FirstOrDefaultAsync(g => g.Id == gameId) .FirstOrDefaultAsync(g => g.Id == gameId)
?? throw new NotFoundException($"[{typeof(Game).FullName}] with ID {gameId} has not been found in context"); ?? throw new NotFoundException($"[{typeof(Game).FullName}] with ID {gameId} has not been found in context");
context.Games.Remove(gameToRemove); 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<PlatformDto>? categoriesToCreate, int gameId) private async Task HandlePlatformsCreation(IEnumerable<PlatformDto>? categoriesToCreate, int gameId)
@@ -62,6 +78,9 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW
{ {
var gps = mapper.Map<ICollection<GamePlatform>>(categoriesToCreate); var gps = mapper.Map<ICollection<GamePlatform>>(categoriesToCreate);
context.GamePlatforms.RemoveRange(
context.GamePlatforms.Where(gp => gp.GameId == gameId));
foreach (var gp in gps) foreach (var gp in gps)
{ {
gp.GameId = gameId; gp.GameId = gameId;
@@ -69,6 +88,12 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW
context.Platforms.AttachRange(gps.Select(gp => gp.Platform)); context.Platforms.AttachRange(gps.Select(gp => gp.Platform));
await context.GamePlatforms.AddRangeAsync(gps); 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<ICollection<GameProperty>>(categoriesToCreate); var gps = mapper.Map<ICollection<GameProperty>>(categoriesToCreate);
context.GameProperties.RemoveRange(
context.GameProperties.Where(gp => gp.GameId == gameId));
foreach (var gp in gps) foreach (var gp in gps)
{ {
gp.GameId = gameId; gp.GameId = gameId;
@@ -85,6 +113,12 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW
context.Properties.AttachRange(gps.Select(gp => gp.Property)); context.Properties.AttachRange(gps.Select(gp => gp.Property));
await context.GameProperties.AddRangeAsync(gps); 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<ICollection<GameTag>>(categoriesToCreate); var gts = mapper.Map<ICollection<GameTag>>(categoriesToCreate);
context.GameTags.RemoveRange(
context.GameTags.Where(gt => gt.GameId == gameId));
foreach (var gt in gts) foreach (var gt in gts)
{ {
gt.GameId = gameId; gt.GameId = gameId;
@@ -101,11 +138,29 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW
context.Tags.AttachRange(gts.Select(gt => gt.Tag)); context.Tags.AttachRange(gts.Select(gt => gt.Tag));
await context.GameTags.AddRangeAsync(gts); 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) if (game?.Publisher != null)
{ {
context.Publishers.Attach(game.Publisher); context.Publishers.Attach(game.Publisher);