Apply sort and game filter
All checks were successful
Game Ideas build for PR / build_blazor_app (pull_request) Successful in 1m2s
All checks were successful
Game Ideas build for PR / build_blazor_app (pull_request) Successful in 1m2s
This commit is contained in:
@@ -29,6 +29,11 @@ public class GameReadService(GameIdeasContext context, IMapper mapper) : IGameRe
|
||||
.Take(GlobalConstants.NUMBER_PER_PAGE)
|
||||
.ToListAsync();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(filter.Title))
|
||||
{
|
||||
games = ApplySearchGameFilter(games, filter).ToList();
|
||||
}
|
||||
|
||||
return mapper.Map<IEnumerable<GameDto>>(games);
|
||||
}
|
||||
|
||||
@@ -51,11 +56,6 @@ public class GameReadService(GameIdeasContext context, IMapper mapper) : IGameRe
|
||||
|
||||
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");
|
||||
@@ -65,34 +65,40 @@ public class GameReadService(GameIdeasContext context, IMapper mapper) : IGameRe
|
||||
|
||||
if (filter.SortType == Shared.Enum.SortType.Ascending)
|
||||
{
|
||||
query = query.OrderBy(lambda.Compile()).AsQueryable();
|
||||
query = Queryable.OrderBy(query, lambda);
|
||||
}
|
||||
else
|
||||
{
|
||||
query = query.OrderByDescending(lambda.Compile()).AsQueryable();
|
||||
query = Queryable.OrderByDescending(query, lambda);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void ApplyFilter(ref IQueryable<Game> query, GameFilterDto filter)
|
||||
{
|
||||
if (filter.Title != null)
|
||||
if (filter.PlatformIds != null)
|
||||
{
|
||||
var keywords = filter.Title
|
||||
.Split([' '], StringSplitOptions.RemoveEmptyEntries)
|
||||
.Select(k => k.Trim())
|
||||
.ToArray();
|
||||
|
||||
query = query
|
||||
.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);
|
||||
|
||||
return;
|
||||
query = query.Where(game => filter.PlatformIds.All(plat =>
|
||||
game.GamePlatforms.Any(gp => gp.PlatformId == plat)));
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<Game> ApplySearchGameFilter(IEnumerable<Game> query, GameFilterDto filter)
|
||||
{
|
||||
var keywords = filter.Title?
|
||||
.Split([' '], StringSplitOptions.RemoveEmptyEntries)
|
||||
.Select(k => k.Trim())
|
||||
.ToArray() ?? [];
|
||||
|
||||
query = query
|
||||
.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);
|
||||
|
||||
return query;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user