Add search for games and orders
All checks were successful
Game Ideas build for PR / build_blazor_app (pull_request) Successful in 48s

This commit is contained in:
Maxime Adler
2025-04-18 15:20:13 +02:00
parent 5889216dfa
commit b66bb911f1
9 changed files with 132 additions and 70 deletions

View File

@@ -0,0 +1,100 @@
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;
using System.Linq.Expressions;
namespace GameIdeas.WebAPI.Services.Games;
public class GameReadService(GameIdeasContext context, IMapper mapper) : IGameReadService
{
public async Task<IEnumerable<GameDto>> GetGames(GameFilterDto filter)
{
var query = context.Games
.Include(g => g.GamePlatforms).ThenInclude(gp => gp.Platform)
.Include(g => g.GameProperties)
.Include(g => g.GameTags).ThenInclude(gt => gt.Tag)
.Include(g => g.GamePublishers)
.Include(g => g.GameDevelopers)
.AsQueryable();
ApplyFilter(ref query, filter);
ApplyOrder(ref query, filter);
var games = await query.Skip((filter.CurrentPage - 1) * GlobalConstants.NUMBER_PER_PAGE)
.Take(GlobalConstants.NUMBER_PER_PAGE)
.ToListAsync();
return mapper.Map<IEnumerable<GameDto>>(games);
}
public async Task<GameDetailDto> 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).FullName}] with ID {gameId} has not been found in context")
: mapper.Map<GameDetailDto>(game);
}
private static void ApplyOrder(ref IQueryable<Game> query, GameFilterDto filter)
{
if (filter.Title != null)
{
return;
}
if (filter.SortType != null && filter.SortPropertyName != null)
{
var param = Expression.Parameter(typeof(Game), "x");
Expression propertyAccess = Expression.PropertyOrField(param, filter.SortPropertyName);
var converted = Expression.Convert(propertyAccess, typeof(object));
var lambda = Expression.Lambda<Func<Game, object>>(converted, param);
if (filter.SortType == Shared.Enum.SortType.Ascending)
{
query = query.OrderBy(lambda.Compile()).AsQueryable();
}
else
{
query = query.OrderByDescending(lambda.Compile()).AsQueryable();
}
}
}
private static void ApplyFilter(ref IQueryable<Game> query, GameFilterDto filter)
{
if (filter.Title != null)
{
var keywords = filter.Title
.Split([' '], StringSplitOptions.RemoveEmptyEntries)
.Select(k => k.Trim())
.ToArray();
query = query
.Where(game => keywords.All(
kw => game.Title.IndexOf(kw, StringComparison.OrdinalIgnoreCase) >= 0
))
.OrderBy(game =>
keywords.Min(kw =>
game.Title.IndexOf(kw, StringComparison.OrdinalIgnoreCase)
)
)
.ThenBy(game => game.Title.Length);
return;
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -0,0 +1,9 @@
using GameIdeas.Shared.Dto;
namespace GameIdeas.WebAPI.Services.Games;
public interface IGameReadService
{
Task<IEnumerable<GameDto>> GetGames(GameFilterDto filter);
Task<GameDetailDto> GetGameById(int gameId);
}

View File

@@ -0,0 +1,10 @@
using GameIdeas.Shared.Dto;
namespace GameIdeas.WebAPI.Services.Games;
public interface IGameWriteService
{
Task<GameDetailDto> CreateGame(GameDetailDto gameDto);
Task<GameDetailDto> UpdateGame(GameDetailDto gameDto);
Task<bool> DeleteGame(int gameId);
}