feature/apply-filter #18
@@ -4,7 +4,6 @@ using GameIdeas.BlazorApp.Shared.Models;
|
||||
using GameIdeas.Shared.Dto;
|
||||
using GameIdeas.Shared.Enum;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.AspNetCore.Components.Forms;
|
||||
|
||||
namespace GameIdeas.BlazorApp.Pages.Games.Filter;
|
||||
|
||||
@@ -23,10 +22,10 @@ public partial class GameFilter
|
||||
];
|
||||
|
||||
private readonly List<SortPropertyDto> GameProperties = [
|
||||
new() { PropertyName = nameof(GameDetailDto.Title), Label = "Titre" },
|
||||
new() { PropertyName = nameof(GameDetailDto.ReleaseDate), Label = "Date de parution" },
|
||||
new() { PropertyName = nameof(GameDetailDto.StorageSpace), Label = "Espace de stockage" },
|
||||
new() { PropertyName = nameof(GameDetailDto.Interest), Label = "Inter<65>t" }
|
||||
new() { PropertyName = nameof(GameIdeas.Shared.Model.Game.Title), Label = "Titre" },
|
||||
new() { PropertyName = nameof(GameIdeas.Shared.Model.Game.ReleaseDate), Label = "Date de parution" },
|
||||
new() { PropertyName = nameof(GameIdeas.Shared.Model.Game.StorageSpace), Label = "Espace de stockage" },
|
||||
new() { PropertyName = nameof(GameIdeas.Shared.Model.Game.Interest), Label = "Inter<65>t" }
|
||||
];
|
||||
|
||||
private SelectParams<SortPropertyDto, SortTypeDto> SelectParams = new();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using GameIdeas.Shared.Dto;
|
||||
using GameIdeas.WebAPI.Services.Interfaces;
|
||||
using GameIdeas.WebAPI.Services.Games;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace GameIdeas.WebAPI.Controllers;
|
||||
@@ -7,7 +7,10 @@ namespace GameIdeas.WebAPI.Controllers;
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
|
||||
public class GameController(IGameService gameService, ILoggerFactory loggerFactory) : Controller
|
||||
public class GameController(
|
||||
IGameReadService gameReadService,
|
||||
IGameWriteService gameWriteService,
|
||||
ILoggerFactory loggerFactory) : Controller
|
||||
{
|
||||
private readonly ILogger<GameController> logger = loggerFactory.CreateLogger<GameController>();
|
||||
|
||||
@@ -16,7 +19,7 @@ public class GameController(IGameService gameService, ILoggerFactory loggerFacto
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(await gameService.GetGames(filter));
|
||||
return Ok(await gameReadService.GetGames(filter));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -30,7 +33,7 @@ public class GameController(IGameService gameService, ILoggerFactory loggerFacto
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(await gameService.GetGameById(id));
|
||||
return Ok(await gameReadService.GetGameById(id));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -44,7 +47,7 @@ public class GameController(IGameService gameService, ILoggerFactory loggerFacto
|
||||
{
|
||||
try
|
||||
{
|
||||
var gameResult = await gameService.CreateGame(game);
|
||||
var gameResult = await gameWriteService.CreateGame(game);
|
||||
return Created("/Create", gameResult.Id);
|
||||
}
|
||||
catch (Exception e)
|
||||
@@ -59,7 +62,7 @@ public class GameController(IGameService gameService, ILoggerFactory loggerFacto
|
||||
{
|
||||
try
|
||||
{
|
||||
var gameResult = await gameService.UpdateGame(game);
|
||||
var gameResult = await gameWriteService.UpdateGame(game);
|
||||
return Created($"/Update", gameResult.Id);
|
||||
}
|
||||
catch (Exception e)
|
||||
@@ -74,7 +77,7 @@ public class GameController(IGameService gameService, ILoggerFactory loggerFacto
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(await gameService.DeleteGame(id));
|
||||
return Ok(await gameWriteService.DeleteGame(id));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
using GameIdeas.Resources;
|
||||
using GameIdeas.WebAPI.Context;
|
||||
using GameIdeas.WebAPI.Profiles;
|
||||
using GameIdeas.WebAPI.Services;
|
||||
using GameIdeas.WebAPI.Services.Categories;
|
||||
using GameIdeas.WebAPI.Services.Games;
|
||||
using GameIdeas.WebAPI.Services.Interfaces;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Text.Json.Serialization;
|
||||
@@ -32,7 +33,8 @@ services.AddDbContext<GameIdeasContext>(dbContextOptions);
|
||||
services.AddSingleton<TranslationService>();
|
||||
services.AddSingleton<Translations>();
|
||||
|
||||
services.AddScoped<IGameService, GameService>();
|
||||
services.AddScoped<IGameReadService, GameReadService>();
|
||||
services.AddScoped<IGameWriteService, GameWriteService>();
|
||||
services.AddScoped<ICategoryService, CategoryService>();
|
||||
|
||||
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
|
||||
|
||||
@@ -4,7 +4,7 @@ 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
|
||||
{
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,51 +4,12 @@ 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(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);
|
||||
}
|
||||
|
||||
public async Task<GameDetailDto> CreateGame(GameDetailDto gameDto)
|
||||
{
|
||||
var gameToCreate = mapper.Map<Game>(gameDto);
|
||||
@@ -98,16 +59,6 @@ public class GameService(GameIdeasContext context, IMapper mapper) : IGameServic
|
||||
return await context.SaveChangesAsync() != 0;
|
||||
}
|
||||
|
||||
private void ApplyOrder(ref IQueryable<Game> query, GameFilterDto filter)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void ApplyFilter(ref IQueryable<Game> query, GameFilterDto filter)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -1,11 +1,9 @@
|
||||
using GameIdeas.Shared.Dto;
|
||||
|
||||
namespace GameIdeas.WebAPI.Services.Interfaces;
|
||||
namespace GameIdeas.WebAPI.Services.Games;
|
||||
|
||||
public interface IGameService
|
||||
public interface IGameWriteService
|
||||
{
|
||||
Task<IEnumerable<GameDto>> GetGames(GameFilterDto filter);
|
||||
Task<GameDetailDto> GetGameById(int gameId);
|
||||
Task<GameDetailDto> CreateGame(GameDetailDto gameDto);
|
||||
Task<GameDetailDto> UpdateGame(GameDetailDto gameDto);
|
||||
Task<bool> DeleteGame(int gameId);
|
||||
Reference in New Issue
Block a user