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