Update and delete games (#48)
All checks were successful
Game Ideas deploy / build-test-deploy (push) Successful in 1m27s
All checks were successful
Game Ideas deploy / build-test-deploy (push) Successful in 1m27s
Co-authored-by: Maxime Adler <madler@sqli.com> Reviewed-on: #48
This commit was merged in pull request #48.
This commit is contained in:
@@ -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<Game>(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<Game>(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<bool> 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<PlatformDto>? categoriesToCreate, int gameId)
|
||||
@@ -62,6 +78,9 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW
|
||||
{
|
||||
var gps = mapper.Map<ICollection<GamePlatform>>(categoriesToCreate);
|
||||
|
||||
context.GamePlatforms.RemoveRange(
|
||||
context.GamePlatforms.Where(gp => gp.GameId == gameId));
|
||||
|
||||
foreach (var gp in gps)
|
||||
{
|
||||
gp.GameId = gameId;
|
||||
@@ -69,6 +88,14 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW
|
||||
|
||||
context.Platforms.AttachRange(gps.Select(gp => gp.Platform));
|
||||
await context.GamePlatforms.AddRangeAsync(gps);
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
context.Platforms.RemoveRange(
|
||||
context.Platforms.Include(p => p.GamePlatforms)
|
||||
.Where(p => p.GamePlatforms.Count == 0));
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,6 +105,9 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW
|
||||
{
|
||||
var gps = mapper.Map<ICollection<GameProperty>>(categoriesToCreate);
|
||||
|
||||
context.GameProperties.RemoveRange(
|
||||
context.GameProperties.Where(gp => gp.GameId == gameId));
|
||||
|
||||
foreach (var gp in gps)
|
||||
{
|
||||
gp.GameId = gameId;
|
||||
@@ -85,6 +115,14 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW
|
||||
|
||||
context.Properties.AttachRange(gps.Select(gp => gp.Property));
|
||||
await context.GameProperties.AddRangeAsync(gps);
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
context.Properties.RemoveRange(
|
||||
context.Properties.Include(p => p.GameProperties)
|
||||
.Where(p => p.GameProperties.Count == 0));
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,6 +132,9 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW
|
||||
{
|
||||
var gts = mapper.Map<ICollection<GameTag>>(categoriesToCreate);
|
||||
|
||||
context.GameTags.RemoveRange(
|
||||
context.GameTags.Where(gt => gt.GameId == gameId));
|
||||
|
||||
foreach (var gt in gts)
|
||||
{
|
||||
gt.GameId = gameId;
|
||||
@@ -101,11 +142,30 @@ public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameW
|
||||
|
||||
context.Tags.AttachRange(gts.Select(gt => gt.Tag));
|
||||
await context.GameTags.AddRangeAsync(gts);
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user