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:
@@ -1,13 +1,23 @@
|
||||
using AutoMapper;
|
||||
using GameIdeas.Shared.Dto;
|
||||
using GameIdeas.WebAPI.Context;
|
||||
using GameIdeas.WebAPI.Services.Interfaces;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GameIdeas.WebAPI.Services;
|
||||
namespace GameIdeas.WebAPI.Services.Categories;
|
||||
|
||||
public class CategoryService(GameIdeasContext context, IMapper mapper) : ICategoryService
|
||||
{
|
||||
public List<StorageSpaceDto> GetStorageSpaces() => [
|
||||
new() { Id = 1, MaxSize = 100 },
|
||||
new() { Id = 2, MinSize = 100, MaxSize = 1000 },
|
||||
new() { Id = 3, MinSize = 1000, MaxSize = 5000 },
|
||||
new() { Id = 4, MinSize = 5000, MaxSize = 10000 },
|
||||
new() { Id = 5, MinSize = 10000, MaxSize = 20000 },
|
||||
new() { Id = 6, MinSize = 20000, MaxSize = 40000 },
|
||||
new() { Id = 7, MinSize = 40000, MaxSize = 100000 },
|
||||
new() { Id = 8, MinSize = 100000 },
|
||||
];
|
||||
|
||||
public async Task<CategoriesDto> GetCategories()
|
||||
{
|
||||
var platforms = await context.Platforms.ToListAsync();
|
||||
@@ -15,6 +25,9 @@ public class CategoryService(GameIdeasContext context, IMapper mapper) : ICatego
|
||||
var tags = await context.Tags.ToListAsync();
|
||||
var developers = await context.Developers.ToListAsync();
|
||||
var publishers = await context.Publishers.ToListAsync();
|
||||
var releaseYears = await context.Games
|
||||
.Where(game => game.ReleaseDate != null)
|
||||
.Select(game => game.ReleaseDate!.Value.Year).Distinct().ToListAsync();
|
||||
|
||||
return new()
|
||||
{
|
||||
@@ -22,7 +35,9 @@ public class CategoryService(GameIdeasContext context, IMapper mapper) : ICatego
|
||||
Properties = mapper.Map<List<PropertyDto>>(properties),
|
||||
Tags = mapper.Map<List<TagDto>>(tags),
|
||||
Developers = mapper.Map<List<DeveloperDto>>(developers),
|
||||
Publishers = mapper.Map<List<PublisherDto>>(publishers)
|
||||
Publishers = mapper.Map<List<PublisherDto>>(publishers),
|
||||
ReleaseYears = mapper.Map<List<int>>(releaseYears),
|
||||
StorageSpaces = GetStorageSpaces()
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
using GameIdeas.Shared.Dto;
|
||||
|
||||
namespace GameIdeas.WebAPI.Services.Interfaces;
|
||||
namespace GameIdeas.WebAPI.Services.Categories;
|
||||
|
||||
public interface ICategoryService
|
||||
{
|
||||
List<StorageSpaceDto> GetStorageSpaces();
|
||||
Task<CategoriesDto> GetCategories();
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
using AutoMapper;
|
||||
using GameIdeas.Shared.Constants;
|
||||
using GameIdeas.Shared.Dto;
|
||||
using GameIdeas.Shared.Exceptions;
|
||||
using GameIdeas.Shared.Model;
|
||||
using GameIdeas.WebAPI.Context;
|
||||
using GameIdeas.WebAPI.Services.Categories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace GameIdeas.WebAPI.Services.Games;
|
||||
|
||||
public class GameReadService(GameIdeasContext context, IMapper mapper, ICategoryService categoryService) : 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();
|
||||
|
||||
ApplyStaticFilter(ref games, filter);
|
||||
|
||||
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.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 = Queryable.OrderBy(query, lambda);
|
||||
}
|
||||
else
|
||||
{
|
||||
query = Queryable.OrderByDescending(query, lambda);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyFilter(ref IQueryable<Game> query, GameFilterDto filter)
|
||||
{
|
||||
if (filter.PlatformIds != null)
|
||||
{
|
||||
query = query.Where(game => filter.PlatformIds.All(plat =>
|
||||
game.GamePlatforms.Any(gp => gp.PlatformId == plat)));
|
||||
}
|
||||
|
||||
if (filter.PropertyIds != null)
|
||||
{
|
||||
query = query.Where(game => filter.PropertyIds.All(prop =>
|
||||
game.GameProperties.Any(gp => gp.PropertyId == prop)));
|
||||
}
|
||||
|
||||
if (filter.TagIds != null)
|
||||
{
|
||||
query = query.Where(game => filter.TagIds.All(tag =>
|
||||
game.GameTags.Any(gt => gt.TagId == tag)));
|
||||
}
|
||||
|
||||
if (filter.PublisherIds != null)
|
||||
{
|
||||
query = query.Where(game => filter.PublisherIds.All(pub =>
|
||||
game.GamePublishers.Any(gp => gp.PublisherId == pub)));
|
||||
}
|
||||
|
||||
if (filter.DeveloperIds != null)
|
||||
{
|
||||
query = query.Where(game => filter.DeveloperIds.All(dev =>
|
||||
game.GameDevelopers.Any(gd => gd.DeveloperId == dev)));
|
||||
}
|
||||
|
||||
if (filter.MinInterest != null)
|
||||
{
|
||||
query = query.Where(game => game.Interest >= filter.MinInterest);
|
||||
}
|
||||
|
||||
if (filter.MaxInterest != null)
|
||||
{
|
||||
query = query.Where(game => game.Interest <= filter.MaxInterest);
|
||||
}
|
||||
|
||||
if (filter.ReleaseYears != null)
|
||||
{
|
||||
query = query.Where(game => game.ReleaseDate != null &&
|
||||
filter.ReleaseYears.Contains(game.ReleaseDate.Value.Year));
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyStaticFilter(ref List<Game> games, GameFilterDto filter)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(filter.Title))
|
||||
{
|
||||
var keywords = filter.Title?
|
||||
.Split([' '], StringSplitOptions.RemoveEmptyEntries)
|
||||
.Select(k => k.Trim())
|
||||
.ToArray() ?? [];
|
||||
|
||||
games = games
|
||||
.Where(game => keywords.All(
|
||||
kw => game.Title.Contains(kw, StringComparison.OrdinalIgnoreCase)
|
||||
))
|
||||
.OrderBy(game => keywords.Min(kw =>
|
||||
game.Title.IndexOf(kw, StringComparison.OrdinalIgnoreCase)
|
||||
))
|
||||
.ThenBy(game => game.Title.Length)
|
||||
.ToList();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (filter.StorageSpaces != null)
|
||||
{
|
||||
var storageSpaces = categoryService.GetStorageSpaces().Where(stor => filter.StorageSpaces.Contains(stor.Id));
|
||||
|
||||
games = games
|
||||
.Where(game => storageSpaces.Any(stor =>
|
||||
(stor.MinSize ?? int.MinValue) <= game.StorageSpace && (stor.MaxSize ?? int.MaxValue) > game.StorageSpace))
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,37 +1,16 @@
|
||||
using AutoMapper;
|
||||
using GameIdeas.Shared.Constants;
|
||||
using GameIdeas.Shared.Dto;
|
||||
using GameIdeas.Shared.Exceptions;
|
||||
using GameIdeas.Shared.Model;
|
||||
using GameIdeas.WebAPI.Context;
|
||||
using GameIdeas.WebAPI.Services.Interfaces;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GameIdeas.WebAPI.Services;
|
||||
namespace GameIdeas.WebAPI.Services.Games;
|
||||
|
||||
public class GameService(GameIdeasContext context, IMapper mapper) : IGameService
|
||||
public class GameWriteService(GameIdeasContext context, IMapper mapper) : IGameWriteService
|
||||
{
|
||||
public async Task<IEnumerable<GameDto>> GetGames(PaggingDto pagging)
|
||||
{
|
||||
var games = await SelectGames()
|
||||
.OrderBy(g => g.Title)
|
||||
.Skip((pagging.CurrentPage - 1) * pagging.NumberPerPage)
|
||||
.Take(pagging.NumberPerPage)
|
||||
.ToListAsync();
|
||||
|
||||
return mapper.Map<IEnumerable<GameDto>>(games);
|
||||
}
|
||||
|
||||
public async Task<GameDto> GetGameById(int gameId)
|
||||
{
|
||||
var game = await SelectGames()
|
||||
.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<GameDto>(game);
|
||||
}
|
||||
|
||||
public async Task<GameDto> CreateGame(GameDto gameDto)
|
||||
public async Task<GameDetailDto> CreateGame(GameDetailDto gameDto)
|
||||
{
|
||||
var gameToCreate = mapper.Map<Game>(gameDto);
|
||||
|
||||
@@ -46,10 +25,10 @@ public class GameService(GameIdeasContext context, IMapper mapper) : IGameServic
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
return mapper.Map<GameDto>(gameToCreate);
|
||||
return mapper.Map<GameDetailDto>(gameToCreate);
|
||||
}
|
||||
|
||||
public async Task<GameDto> UpdateGame(GameDto gameDto)
|
||||
public async Task<GameDetailDto> UpdateGame(GameDetailDto gameDto)
|
||||
{
|
||||
if (await context.Games.CountAsync(g => g.Id == gameDto.Id) == 0)
|
||||
{
|
||||
@@ -67,7 +46,7 @@ public class GameService(GameIdeasContext context, IMapper mapper) : IGameServic
|
||||
context.Games.Update(gameToUpdate);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
return mapper.Map<GameDto>(gameToUpdate);
|
||||
return mapper.Map<GameDetailDto>(gameToUpdate);
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteGame(int gameId)
|
||||
@@ -80,19 +59,6 @@ public class GameService(GameIdeasContext context, IMapper mapper) : IGameServic
|
||||
return await context.SaveChangesAsync() != 0;
|
||||
}
|
||||
|
||||
private IQueryable<Game> SelectGames()
|
||||
{
|
||||
return 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();
|
||||
}
|
||||
|
||||
private async Task HandlePlatformsCreation(IEnumerable<PlatformDto>? categoriesToCreate, int gameId)
|
||||
{
|
||||
if (categoriesToCreate != null)
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
using GameIdeas.Shared.Dto;
|
||||
|
||||
namespace GameIdeas.WebAPI.Services.Interfaces;
|
||||
|
||||
public interface IGameService
|
||||
{
|
||||
Task<IEnumerable<GameDto>> GetGames(PaggingDto pagging);
|
||||
Task<GameDto> GetGameById(int gameId);
|
||||
Task<GameDto> CreateGame(GameDto gameDto);
|
||||
Task<GameDto> UpdateGame(GameDto gameDto);
|
||||
Task<bool> DeleteGame(int gameId);
|
||||
}
|
||||
Reference in New Issue
Block a user