feature/apply-filter (#18)
Co-authored-by: Maxime Adler <madler@sqli.com> Reviewed-on: #18
This commit was merged in pull request #18.
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
using AutoMapper;
|
||||
using GameIdeas.Shared.Constants;
|
||||
using GameIdeas.Shared.Dto;
|
||||
using GameIdeas.Shared.Exceptions;
|
||||
using GameIdeas.Shared.Model;
|
||||
using GameIdeas.WebAPI.Context;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GameIdeas.WebAPI.Services.Games;
|
||||
|
||||
public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameWriteService
|
||||
{
|
||||
public async Task<GameDetailDto> CreateGame(GameDetailDto gameDto)
|
||||
{
|
||||
var gameToCreate = mapper.Map<Game>(gameDto);
|
||||
|
||||
await context.Games.AddAsync(gameToCreate);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
await HandlePlatformsCreation(gameDto.Platforms, gameToCreate.Id);
|
||||
await HandlePropertiesCreation(gameDto.Properties, gameToCreate.Id);
|
||||
await HandleTagsCreation(gameDto.Tags, gameToCreate.Id);
|
||||
await HandlePublishersCreation(gameDto.Publishers, gameToCreate.Id);
|
||||
await HandleDevelopersCreation(gameDto.Developers, gameToCreate.Id);
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
return mapper.Map<GameDetailDto>(gameToCreate);
|
||||
}
|
||||
|
||||
public async Task<GameDetailDto> UpdateGame(GameDetailDto gameDto)
|
||||
{
|
||||
if (await context.Games.CountAsync(g => g.Id == gameDto.Id) == 0)
|
||||
{
|
||||
throw new NotFoundException($"[{typeof(Game).FullName}] with ID {gameDto.Id} has not been found in context");
|
||||
}
|
||||
|
||||
var gameToUpdate = mapper.Map<Game>(gameDto);
|
||||
|
||||
await HandlePlatformsCreation(gameDto.Platforms, gameToUpdate.Id);
|
||||
await HandlePropertiesCreation(gameDto.Properties, gameToUpdate.Id);
|
||||
await HandleTagsCreation(gameDto.Tags, gameToUpdate.Id);
|
||||
await HandlePublishersCreation(gameDto.Publishers, gameToUpdate.Id);
|
||||
await HandleDevelopersCreation(gameDto.Developers, gameToUpdate.Id);
|
||||
|
||||
context.Games.Update(gameToUpdate);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
return mapper.Map<GameDetailDto>(gameToUpdate);
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteGame(int 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;
|
||||
}
|
||||
|
||||
private async Task HandlePlatformsCreation(IEnumerable<PlatformDto>? categoriesToCreate, int gameId)
|
||||
{
|
||||
if (categoriesToCreate != null)
|
||||
{
|
||||
var gps = mapper.Map<ICollection<GamePlatform>>(categoriesToCreate);
|
||||
|
||||
foreach (var gp in gps)
|
||||
{
|
||||
gp.GameId = gameId;
|
||||
}
|
||||
|
||||
context.Platforms.AttachRange(gps.Select(gp => gp.Platform));
|
||||
await context.GamePlatforms.AddRangeAsync(gps);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandlePropertiesCreation(IEnumerable<PropertyDto>? categoriesToCreate, int gameId)
|
||||
{
|
||||
if (categoriesToCreate != null)
|
||||
{
|
||||
var gps = mapper.Map<ICollection<GameProperty>>(categoriesToCreate);
|
||||
|
||||
foreach (var gp in gps)
|
||||
{
|
||||
gp.GameId = gameId;
|
||||
}
|
||||
|
||||
context.Properties.AttachRange(gps.Select(gp => gp.Property));
|
||||
await context.GameProperties.AddRangeAsync(gps);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleTagsCreation(IEnumerable<TagDto>? categoriesToCreate, int gameId)
|
||||
{
|
||||
if (categoriesToCreate != null)
|
||||
{
|
||||
var gts = mapper.Map<ICollection<GameTag>>(categoriesToCreate);
|
||||
|
||||
foreach (var gt in gts)
|
||||
{
|
||||
gt.GameId = gameId;
|
||||
}
|
||||
|
||||
context.Tags.AttachRange(gts.Select(gt => gt.Tag));
|
||||
await context.GameTags.AddRangeAsync(gts);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandlePublishersCreation(IEnumerable<PublisherDto>? categoriesToCreate, int gameId)
|
||||
{
|
||||
if (categoriesToCreate != null)
|
||||
{
|
||||
var gps = mapper.Map<ICollection<GamePublisher>>(categoriesToCreate);
|
||||
|
||||
foreach (var gp in gps)
|
||||
{
|
||||
gp.GameId = gameId;
|
||||
}
|
||||
|
||||
context.Publishers.AttachRange(gps.Select(gp => gp.Publisher));
|
||||
await context.GamePublishers.AddRangeAsync(gps);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleDevelopersCreation(IEnumerable<DeveloperDto>? categoriesToCreate, int gameId)
|
||||
{
|
||||
if (categoriesToCreate != null)
|
||||
{
|
||||
var gds = mapper.Map<ICollection<GameDeveloper>>(categoriesToCreate);
|
||||
|
||||
foreach (var gd in gds)
|
||||
{
|
||||
gd.GameId = gameId;
|
||||
}
|
||||
|
||||
context.Developers.AttachRange(gds.Select(gd => gd.Developer));
|
||||
await context.GameDevelopers.AddRangeAsync(gds);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user