feature/apply-filter #18

Merged
Egamorf merged 11 commits from feature/apply-filter into main 2025-04-20 15:43:24 +02:00
17 changed files with 137 additions and 40 deletions
Showing only changes of commit 33cdfa2841 - Show all commits

View File

@@ -0,0 +1,38 @@
using System.Collections;
namespace GameIdeas.BlazorApp.Helpers;
public static class UrlHelper
{
public static string BuildUrlParams(object? model)
{
if (model == null)
return string.Empty;
var properties = model.GetType().GetProperties();
var queryParams = properties
.Select(p =>
{
var value = p.GetValue(model);
switch (value)
{
case null:
return null;
case DateTime dateTime:
return $"{p.Name}={Uri.EscapeDataString(dateTime.ToString("yyyy-MM-dd HH:mm:ss"))}";
}
if (value is IEnumerable enumerable and not string)
{
var items = enumerable.Cast<object>()
.Select(item => $"{p.Name}={Uri.EscapeDataString(item?.ToString() ?? string.Empty)}");
return string.Join("&", items);
}
return $"{p.Name}={Uri.EscapeDataString(value.ToString() ?? string.Empty)}";
})
.ToArray();
return string.Join("&", queryParams.Where(p => p != null));
}
}

View File

@@ -6,9 +6,9 @@ namespace GameIdeas.BlazorApp.Pages.Games.Filter;
public partial class AdvancedGameFilter
{
[Parameter] public GameFilterDto GameFilter { get; set; } = new();
[Parameter] public GameFilterParams GameFilter { get; set; } = new();
[Parameter] public CategoriesDto? Categories { get; set; }
[Parameter] public EventCallback<GameFilterDto> GameFilterChanged { get; set; }
[Parameter] public EventCallback<GameFilterParams> GameFilterChanged { get; set; }
private readonly SelectTheme Theme = SelectTheme.AdvancedFilter;

View File

@@ -31,6 +31,7 @@
<div class="search-container">
<SearchInput @bind-Text="@Value.Title"
@bind-Text:after="HandleValueChanged"
Placeholder="@ResourcesKey.Research" />
</div>

View File

@@ -10,8 +10,8 @@ namespace GameIdeas.BlazorApp.Pages.Games.Filter;
public partial class GameFilter
{
[Parameter] public GameFilterDto Value { get; set; } = new();
[Parameter] public EventCallback<GameFilterDto> ValueChanged { get; set; }
[Parameter] public GameFilterParams Value { get; set; } = new();
[Parameter] public EventCallback<GameFilterParams> ValueChanged { get; set; }
[Parameter] public DisplayType DisplayType { get; set; }
[Parameter] public EventCallback<DisplayType> DisplayTypeChanged { get; set; }
[Parameter] public CategoriesDto? Categories { get; set; }
@@ -23,8 +23,10 @@ public partial class GameFilter
];
private readonly List<SortPropertyDto> GameProperties = [
new() { SortProperty = game => game.Title!, Label = "Titre" },
new() { SortProperty = game => game.ReleaseDate!, Label = "Date de parution" }
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" }
];
private SelectParams<SortPropertyDto, SortTypeDto> SelectParams = new();
@@ -39,7 +41,7 @@ public partial class GameFilter
DefaultHeaders = SortTypes.Where(h => h.SortType == SortType.Ascending).ToList(),
Items = GameProperties,
GetItemLabel = item => item.Label,
DefaultItems = GameProperties.Where(p => p.Label == "Titre").ToList()
DefaultItems = GameProperties.Where(p => p.PropertyName == nameof(GameDetailDto.Title)).ToList()
};
}

View File

@@ -0,0 +1,19 @@

namespace GameIdeas.Shared.Dto;
public class GameFilterParams
{
public SortTypeDto? SortType { get; set; }
public SortPropertyDto? SortProperty { get; set; }
public string? Title { get; set; }
public List<PlatformDto>? Platforms { get; set; }
public List<PropertyDto>? Properties { get; set; }
public List<TagDto>? Tags { get; set; }
public List<PublisherDto>? Publishers { get; set; }
public List<DeveloperDto>? Developers { get; set; }
public int MinInterest { get; set; } = 1;
public int MaxInterest { get; set; } = 5;
public List<int>? ReleaseYears { get; set; }
public int? MinStorageSize { get; set; }
public int? MaxStorageSize { get; set; }
}

View File

@@ -14,7 +14,7 @@
<GameHeader AddTypeChanged="HandleAddClicked">
<GameFilter Categories="Categories"
@bind-DisplayType=DisplayType
@bind-Value=GameFilter />
Value=GameFilter ValueChanged="HandleFilterChanged" />
</GameHeader>
<div class="container">
@@ -36,7 +36,7 @@
</div>
<AdvancedGameFilter @bind-GameFilter=GameFilter Categories="Categories" />
<AdvancedGameFilter GameFilter=GameFilter GameFilterChanged="HandleFilterChanged" Categories="Categories" />
</div>
<Popup @ref=ManualAddPopup BackdropFilterClicked="HandleBackdropManualAddClicked" Closable=false>

View File

@@ -11,14 +11,16 @@ public partial class Game
[Inject] private IGameGateway GameGateway { get; set; } = default!;
private DisplayType DisplayType = DisplayType.List;
private GameFilterDto GameFilter = new();
private GameFilterParams GameFilter = new();
private Popup? ManualAddPopup;
private bool IsLoading = false;
private CategoriesDto? Categories;
private IEnumerable<GameDto> GamesDto = [];
private int CurrentPage;
protected override async Task OnInitializedAsync()
{
CurrentPage = 1;
await HandleFetchDatas();
await base.OnInitializedAsync();
}
@@ -46,7 +48,28 @@ public partial class Game
IsLoading = true;
Categories = await GameGateway.FetchCategories();
GamesDto = await GameGateway.FetchGames(new PaggingDto() { CurrentPage = 1, NumberPerPage = 50 });
GamesDto = await GameGateway.FetchGames(GameFilter, CurrentPage);
}
catch (Exception)
{
throw;
}
finally
{
IsLoading = false;
}
}
private async Task HandleFilterChanged(GameFilterParams args)
{
GameFilter = args;
try
{
IsLoading = true;
GamesDto = await GameGateway.FetchGames(GameFilter, CurrentPage);
}
catch (Exception)
{

View File

@@ -34,11 +34,29 @@ public class GameGateway(IHttpClientService httpClientService) : IGameGateway
}
}
public async Task<IEnumerable<GameDto>> FetchGames(PaggingDto pagging)
public async Task<IEnumerable<GameDto>> FetchGames(GameFilterParams filterParams, int currentPage)
{
try
{
var result = await httpClientService.FetchDataAsync<IEnumerable<GameDto>>(Endpoints.Game.Fetch(pagging));
GameFilterDto filter = new()
{
CurrentPage = currentPage,
Title = filterParams.Title,
MaxInterest = filterParams.MaxInterest,
MinInterest = filterParams.MinInterest,
MaxStorageSize = filterParams.MaxStorageSize,
MinStorageSize = filterParams.MinStorageSize,
ReleaseYears = filterParams.ReleaseYears,
DeveloperIds = filterParams.Developers?.Select(d => d.Id ?? 0).ToList(),
PublisherIds = filterParams.Publishers?.Select(d => d.Id ?? 0).ToList(),
PlatformIds = filterParams.Platforms?.Select(d => d.Id ?? 0).ToList(),
PropertyIds = filterParams.Properties?.Select(d => d.Id ?? 0).ToList(),
TagIds = filterParams.Tags?.Select(d => d.Id ?? 0).ToList(),
SortType = filterParams.SortType?.SortType,
SortPropertyName = filterParams.SortProperty?.PropertyName
};
var result = await httpClientService.FetchDataAsync<IEnumerable<GameDto>>(Endpoints.Game.Fetch(filter));
return result ?? throw new InvalidOperationException(ResourcesKey.ErrorFetchGames);
}
catch (Exception)

View File

@@ -6,5 +6,5 @@ public interface IGameGateway
{
Task<CategoriesDto> FetchCategories();
Task<int> CreateGame(GameDetailDto game);
Task<IEnumerable<GameDto>> FetchGames(PaggingDto pagging);
Task<IEnumerable<GameDto>> FetchGames(GameFilterParams filter, int currentPage);
}

View File

@@ -1,4 +1,5 @@
using GameIdeas.Shared.Dto;
using GameIdeas.BlazorApp.Helpers;
using GameIdeas.Shared.Dto;
namespace GameIdeas.BlazorApp.Shared.Constants;
@@ -7,8 +8,7 @@ public static class Endpoints
public static class Game
{
public static readonly string Create = "api/Game/Create";
public static string Fetch(PaggingDto pagging) =>
$"api/Game?{nameof(pagging.CurrentPage)}={pagging.CurrentPage}&{nameof(pagging.NumberPerPage)}={pagging.NumberPerPage}";
public static string Fetch(GameFilterDto filter) => $"api/Game?{UrlHelper.BuildUrlParams(filter)}";
}
public static class Category

View File

@@ -2,6 +2,6 @@
public class GlobalConstants
{
public static int NUMBER_PER_PAGE = 50;
}

View File

@@ -1,16 +1,18 @@

using GameIdeas.Shared.Enum;
namespace GameIdeas.Shared.Dto;
public class GameFilterDto
{
public SortTypeDto? SortType { get; set; }
public SortPropertyDto? SortProperty { get; set; }
public int CurrentPage { get; set; } = 1;
public SortType? SortType { get; set; }
public string? SortPropertyName { get; set; }
public string? Title { get; set; }
public List<PlatformDto>? Platforms { get; set; }
public List<PropertyDto>? Properties { get; set; }
public List<TagDto>? Tags { get; set; }
public List<PublisherDto>? Publishers { get; set; }
public List<DeveloperDto>? Developers { get; set; }
public List<int>? PlatformIds { get; set; }
public List<int>? PropertyIds { get; set; }
public List<int>? TagIds { get; set; }
public List<int>? PublisherIds { get; set; }
public List<int>? DeveloperIds { get; set; }
public int MinInterest { get; set; } = 1;
public int MaxInterest { get; set; } = 5;
public List<int>? ReleaseYears { get; set; }

View File

@@ -1,7 +0,0 @@
namespace GameIdeas.Shared.Dto;
public class PaggingDto
{
public int CurrentPage { get; set; }
public int NumberPerPage { get; set; }
}

View File

@@ -2,6 +2,6 @@
public class SortPropertyDto
{
public Func<GameDetailDto, object>? SortProperty { get; set; }
public string PropertyName { get; set; } = string.Empty;
public string Label { get; set; } = string.Empty;
}

View File

@@ -12,11 +12,11 @@ public class GameController(IGameService gameService, ILoggerFactory loggerFacto
private readonly ILogger<GameController> logger = loggerFactory.CreateLogger<GameController>();
[HttpGet]
public async Task<ActionResult<IEnumerable<GameDto>>> SearchGames([FromQuery] PaggingDto pagging)
public async Task<ActionResult<IEnumerable<GameDto>>> SearchGames([FromQuery] GameFilterDto filter)
{
try
{
return Ok(await gameService.GetGames(pagging));
return Ok(await gameService.GetGames(filter));
}
catch (Exception e)
{

View File

@@ -1,4 +1,5 @@
using AutoMapper;
using GameIdeas.Shared.Constants;
using GameIdeas.Shared.Dto;
using GameIdeas.Shared.Exceptions;
using GameIdeas.Shared.Model;
@@ -10,12 +11,12 @@ namespace GameIdeas.WebAPI.Services;
public class GameService(GameIdeasContext context, IMapper mapper) : IGameService
{
public async Task<IEnumerable<GameDto>> GetGames(PaggingDto pagging)
public async Task<IEnumerable<GameDto>> GetGames(GameFilterDto filter)
{
var games = await SelectGames()
.OrderBy(g => g.Title)
.Skip((pagging.CurrentPage - 1) * pagging.NumberPerPage)
.Take(pagging.NumberPerPage)
.Skip((filter.CurrentPage - 1) * GlobalConstants.NUMBER_PER_PAGE)
.Take(GlobalConstants.NUMBER_PER_PAGE)
.ToListAsync();
return mapper.Map<IEnumerable<GameDto>>(games);

View File

@@ -4,7 +4,7 @@ namespace GameIdeas.WebAPI.Services.Interfaces;
public interface IGameService
{
Task<IEnumerable<GameDto>> GetGames(PaggingDto pagging);
Task<IEnumerable<GameDto>> GetGames(GameFilterDto filter);
Task<GameDetailDto> GetGameById(int gameId);
Task<GameDetailDto> CreateGame(GameDetailDto gameDto);
Task<GameDetailDto> UpdateGame(GameDetailDto gameDto);