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> 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>(games); } public async Task 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(game); } public async Task CreateGame(GameDto gameDto) { var game = mapper.Map(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(game); } public async Task UpdateGame(GameDto gameDto) { var gameToUpdate = mapper.Map(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(gameToUpdate); } }