Files
game-ideas/src/GameIdeas/Server/GameIdeas.WebAPI/Services/GameService.cs
Maxime Adler b545da19b7
All checks were successful
Game Ideas build for PR / build_blazor_app (pull_request) Successful in 34s
Fix message
2025-04-10 15:21:13 +02:00

93 lines
3.9 KiB
C#

using AutoMapper;
using GameIdeas.Shared.Dto;
using GameIdeas.Shared.Exceptions;
using GameIdeas.Shared.Model;
using GameIdeas.WebAPI.Context;
using Microsoft.EntityFrameworkCore;
namespace GameIdeas.WebAPI.Services;
public class GameService(GameIdeasContext context, IMapper mapper)
{
public async Task<IEnumerable<GameDto>> GetGames(PaggingDto pagging)
{
var query = context.Games
.Include(g => g.CreationUser)
.Include(g => g.ModificationUser)
.Include(g => g.GamePlatforms).ThenInclude(p => p.Platform)
.Include(g => g.GameProperties).ThenInclude(p => p.Property)
.Include(g => g.GameTags).ThenInclude(p => p.Tag)
.Include(g => g.GamePublishers).ThenInclude(p => p.Publisher)
.Include(g => g.GameDevelopers).ThenInclude(p => p.Developer)
.AsQueryable();
var games = await query
.OrderBy(g => g.Title)
.Skip(pagging.CurrentPage * pagging.NumberPerPage)
.Take(pagging.NumberPerPage)
.ToListAsync();
return mapper.Map<IEnumerable<GameDto>>(games);
}
public async Task<GameDto> GetGameById(int gameId)
{
var game = await context.Games
.Include(g => g.CreationUser)
.Include(g => g.ModificationUser)
.Include(g => g.GamePlatforms).ThenInclude(p => p.Platform)
.Include(g => g.GameProperties).ThenInclude(p => p.Property)
.Include(g => g.GameTags).ThenInclude(p => p.Tag)
.Include(g => g.GamePublishers).ThenInclude(p => p.Publisher)
.Include(g => g.GameDevelopers).ThenInclude(p => p.Developer)
.FirstOrDefaultAsync(g => g.Id == gameId);
return game == null
? throw new NotFoundException($"[{typeof(Game).Name}] with ID {gameId} has not been found in context")
: mapper.Map<GameDto>(game);
}
public async Task<GameDto> CreateGame(GameDto gameDto)
{
var game = mapper.Map<Game>(gameDto);
context.GameDevelopers.AttachRange(game.GameDevelopers);
context.Developers.AttachRange(game.GameDevelopers.Select(gd => gd.Developer));
context.GamePlatforms.AttachRange(game.GamePlatforms);
context.Platforms.AttachRange(game.GamePlatforms.Select(gp => gp.Platform));
context.GameProperties.AttachRange(game.GameProperties);
context.Properties.AttachRange(game.GameProperties.Select(gp => gp.Property));
context.GamePublishers.AttachRange(game.GamePublishers);
context.Publishers.AttachRange(game.GamePublishers.Select(gp => gp.Publisher));
context.GameTags.AttachRange(game.GameTags);
context.Tags.AttachRange(game.GameTags.Select(gt => gt.Tag));
await context.Games.AddAsync(game);
await context.SaveChangesAsync();
return mapper.Map<GameDto>(game);
}
public async Task<GameDto> UpdateGame(GameDto gameDto)
{
var gameToUpdate = mapper.Map<Game>(gameDto);
context.GameDevelopers.AttachRange(gameToUpdate.GameDevelopers);
context.Developers.AttachRange(gameToUpdate.GameDevelopers.Select(gd => gd.Developer));
context.GamePlatforms.AttachRange(gameToUpdate.GamePlatforms);
context.Platforms.AttachRange(gameToUpdate.GamePlatforms.Select(gp => gp.Platform));
context.GameProperties.AttachRange(gameToUpdate.GameProperties);
context.Properties.AttachRange(gameToUpdate.GameProperties.Select(gp => gp.Property));
context.GamePublishers.AttachRange(gameToUpdate.GamePublishers);
context.Publishers.AttachRange(gameToUpdate.GamePublishers.Select(gp => gp.Publisher));
context.GameTags.AttachRange(gameToUpdate.GameTags);
context.Tags.AttachRange(gameToUpdate.GameTags.Select(gt => gt.Tag));
context.Games.Update(gameToUpdate);
await context.SaveChangesAsync();
return mapper.Map<GameDto>(gameToUpdate);
}
}