feature/apply-filter #18

Merged
Egamorf merged 11 commits from feature/apply-filter into main 2025-04-20 15:43:24 +02:00
43 changed files with 505 additions and 189 deletions

View File

@@ -4,7 +4,7 @@ namespace GameIdeas.BlazorApp.Helpers;
public static class GameHelper
{
public static void WriteTrackingDto(GameDto game)
public static void WriteTrackingDto(GameDetailDto game)
{
game.CreationUserId = 100000;
game.CreationDate = DateTime.Now;
@@ -22,4 +22,19 @@ public static class GameHelper
_ => "--yellow",
};
}
public static string GetFormatedStorageSpace(double? storageValue)
{
if (storageValue == null)
{
return string.Empty;
}
return storageValue switch
{
>= 1000000 => $"{storageValue / 1000000:f1} To",
>= 1000 => $"{storageValue / 1000:f1} Go",
_ => $"{storageValue:f1} Mo"
};
}
}

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

@@ -12,19 +12,4 @@ public class GameBase : ComponentBase
{
NavigationManager.NavigateTo($"/Games/Detail/{GameDto.Id}");
}
protected string GetFormatedStorageSpace()
{
if (GameDto.StorageSpace == null)
{
return string.Empty;
}
return GameDto.StorageSpace switch
{
>= 1000000 => $"{GameDto.StorageSpace / 1000000:f1} To",
>= 1000 => $"{GameDto.StorageSpace / 1000:f1} Go",
_ => $"{GameDto.StorageSpace:f1} Mo"
};
}
}

View File

@@ -18,7 +18,7 @@ public partial class GameCreationForm
[Parameter] public CategoriesDto? Categories { get; set; }
[Parameter] public EventCallback OnSubmit { get; set; }
private GameDto GameDto = new();
private GameDetailDto GameDto = new();
private EditContext? EditContext;
private readonly SelectTheme Theme = SelectTheme.Creation;
private readonly SliderParams SliderParams = new() { Gap = 1, Min = 1, Max = 5 };

View File

@@ -28,7 +28,7 @@
}
</div>
<span class="storage">@GetFormatedStorageSpace()</span>
<span class="storage">@GameHelper.GetFormatedStorageSpace(GameDto.StorageSpace)</span>
<div class="interest">
<span class="value" style="@($"color: var({GameHelper.GetInterestColor(GameDto.Interest, 5)})")">

View File

@@ -4,7 +4,7 @@ using GameIdeas.Shared.Dto;
namespace GameIdeas.BlazorApp.Pages.Games.Components;
public class GameValidation : AbstractValidator<GameDto>
public class GameValidation : AbstractValidator<GameDetailDto>
{
public GameValidation()
{

View File

@@ -22,5 +22,11 @@
<SelectSearch TItem="PublisherDto" Placeholder="@ResourcesKey.Publishers" GetLabel="@(p => p.Name)"
@bind-Values=GameFilter.Publishers @bind-Values:after=HandleValueChanged Theme="Theme" Items="Categories?.Publishers" />
<SelectSearch TItem="int" Placeholder="@ResourcesKey.ReleaseDate" GetLabel="@(p => p.ToString())"
@bind-Values=GameFilter.ReleaseYears @bind-Values:after=HandleValueChanged Theme="Theme" Items="Categories?.ReleaseYears" />
<SelectSearch TItem="int" Placeholder="@ResourcesKey.StorageSize" GetLabel="@GetStorageSpaceLabel"
@bind-Values=GameFilter.StorageSpaceIds @bind-Values:after=HandleValueChanged Theme="Theme" Items="@(Categories?.StorageSpaces?.Select(stor => stor.Id).ToList())" />
<span class="title">@ResourcesKey.LastAdd</span>
</div>

View File

@@ -1,4 +1,7 @@
using GameIdeas.BlazorApp.Helpers;
using GameIdeas.BlazorApp.Pages.Games.Header;
using GameIdeas.BlazorApp.Shared.Components.Select.Models;
using GameIdeas.Resources;
using GameIdeas.Shared.Dto;
using Microsoft.AspNetCore.Components;
@@ -6,9 +9,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;
@@ -16,4 +19,31 @@ public partial class AdvancedGameFilter
{
await GameFilterChanged.InvokeAsync(GameFilter);
}
private string GetStorageSpaceLabel(int storageSpaceId)
{
var storageSpace = Categories?.StorageSpaces?.FirstOrDefault(c => c.Id == storageSpaceId)
?? throw new ArgumentNullException(ResourcesKey.ErrorStorageSpaceLabel);
if (storageSpace.MinSize == null && storageSpace.MaxSize != null)
{
return string.Format(ResourcesKey.MinStorageSpaceFormat,
GameHelper.GetFormatedStorageSpace(storageSpace.MaxSize));
}
if (storageSpace.MinSize != null && storageSpace.MaxSize == null)
{
return string.Format(ResourcesKey.MaxStorageSpaceFormat,
GameHelper.GetFormatedStorageSpace(storageSpace.MinSize));
}
if (storageSpace.MinSize != null && storageSpace.MaxSize != null)
{
return string.Format(ResourcesKey.MinMaxStorageSpaceFormat,
GameHelper.GetFormatedStorageSpace(storageSpace.MinSize),
GameHelper.GetFormatedStorageSpace(storageSpace.MaxSize));
}
throw new ArgumentNullException(ResourcesKey.ErrorStorageSpaceLabel);
}
}

View File

@@ -7,8 +7,8 @@
@using GameIdeas.Shared.Dto
<div class="form-filter">
<Select TItem="SortPropertyDto" ValuesChanged=HandleSortPropertyClicked
THeader="SortTypeDto" HeaderValuesChanged=HandleSortTypeClicked
<Select TItem="SortPropertyDto" Values="[Value.SortProperty]" ValuesChanged=HandleSortPropertyClicked
THeader="SortTypeDto" HeaderValues="[Value.SortType]" HeaderValuesChanged=HandleSortTypeClicked
Params=SelectParams Theme="SelectTheme.Sort" >
<div class="square-button">
<svg class="sort-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
@@ -31,6 +31,7 @@
<div class="search-container">
<SearchInput @bind-Text="@Value.Title"
@bind-Text:after="HandleValueChanged"
Placeholder="@ResourcesKey.Research" />
</div>

View File

@@ -1,30 +1,33 @@
using GameIdeas.BlazorApp.Shared.Components.Select.Models;
using GameIdeas.BlazorApp.Shared.Components.SliderRange;
using GameIdeas.BlazorApp.Shared.Models;
using GameIdeas.Resources;
using GameIdeas.Shared.Dto;
using GameIdeas.Shared.Enum;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
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; }
private readonly List<SortTypeDto> SortTypes = [
new() { SortType = SortType.Ascending, Label = "Ascendant" },
new() { SortType = SortType.Descending, Label = "Descendant" }
public static readonly List<SortTypeDto> SortTypes = [
new() { SortType = SortType.Ascending, Label = ResourcesKey.Ascending },
new() { SortType = SortType.Descending, Label = ResourcesKey.Descending }
];
private readonly List<SortPropertyDto> GameProperties = [
new() { SortProperty = game => game.Title!, Label = "Titre" },
new() { SortProperty = game => game.ReleaseDate!, Label = "Date de parution" }
public static readonly List<SortPropertyDto> GameProperties = [
new() { PropertyName = nameof(GameIdeas.Shared.Model.Game.Title), Label = ResourcesKey.Title },
new() { PropertyName = nameof(GameIdeas.Shared.Model.Game.ReleaseDate), Label = ResourcesKey.ReleaseDate },
new() { PropertyName = nameof(GameIdeas.Shared.Model.Game.CreationDate), Label = ResourcesKey.CreateDate },
new() { PropertyName = nameof(GameIdeas.Shared.Model.Game.ModificationDate), Label = ResourcesKey.UpdateDate },
new() { PropertyName = nameof(GameIdeas.Shared.Model.Game.StorageSpace), Label = ResourcesKey.StorageSize },
new() { PropertyName = nameof(GameIdeas.Shared.Model.Game.Interest), Label = ResourcesKey.Interest }
];
private SelectParams<SortPropertyDto, SortTypeDto> SelectParams = new();
@@ -36,10 +39,8 @@ public partial class GameFilter
{
Headers = SortTypes,
GetHeaderLabel = header => header.Label,
DefaultHeaders = SortTypes.Where(h => h.SortType == SortType.Ascending).ToList(),
Items = GameProperties,
GetItemLabel = item => item.Label,
DefaultItems = GameProperties.Where(p => p.Label == "Titre").ToList()
};
}

View File

@@ -0,0 +1,19 @@
using GameIdeas.Shared.Dto;
namespace GameIdeas.BlazorApp.Pages.Games.Filter;
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 List<int>? StorageSpaceIds { 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,9 +36,9 @@
</div>
<AdvancedGameFilter @bind-GameFilter=GameFilter Categories="Categories" />
<AdvancedGameFilter GameFilter=GameFilter GameFilterChanged="HandleFilterChanged" Categories="Categories" />
</div>
<Popup @ref=ManualAddPopup BackdropFilterClicked="HandleBackdropManualAddClicked" Closable=false>
<GameCreationForm Categories="Categories" OnSubmit="HandleFetchDatas" />
<GameCreationForm Categories="Categories" OnSubmit="() => HandleFetchDatas()" />
</Popup>

View File

@@ -1,7 +1,9 @@
using GameIdeas.BlazorApp.Pages.Games.Filter;
using GameIdeas.BlazorApp.Pages.Games.Gateways;
using GameIdeas.BlazorApp.Shared.Components.Popup;
using GameIdeas.BlazorApp.Shared.Models;
using GameIdeas.Shared.Dto;
using GameIdeas.Shared.Enum;
using Microsoft.AspNetCore.Components;
namespace GameIdeas.BlazorApp.Pages.Games;
@@ -11,17 +13,26 @@ 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;
GameFilter.SortType = Filter.GameFilter.SortTypes
.First(st => st.SortType == SortType.Ascending);
GameFilter.SortProperty= Filter.GameFilter.GameProperties
.First(gp => gp.PropertyName == nameof(GameIdeas.Shared.Model.Game.Title));
await HandleFetchDatas();
await base.OnInitializedAsync();
}
private void HandleAddClicked(AddType addType)
{
switch (addType)
@@ -39,14 +50,16 @@ public partial class Game
{
ManualAddPopup?.Close();
}
private async Task HandleFetchDatas()
private async Task HandleFetchDatas(bool loadCategories = true, bool displayLoader = true)
{
try
{
IsLoading = true;
IsLoading = displayLoader;
Categories = await GameGateway.FetchCategories();
GamesDto = await GameGateway.FetchGames(new PaggingDto() { CurrentPage = 1, NumberPerPage = 50 });
if (loadCategories)
Categories = await GameGateway.FetchCategories();
GamesDto = await GameGateway.FetchGames(GameFilter, CurrentPage);
}
catch (Exception)
{
@@ -57,4 +70,9 @@ public partial class Game
IsLoading = false;
}
}
private async Task HandleFilterChanged(GameFilterParams args)
{
GameFilter = args;
await HandleFetchDatas(loadCategories: false, displayLoader: false);
}
}

View File

@@ -1,4 +1,5 @@
using GameIdeas.BlazorApp.Services;
using GameIdeas.BlazorApp.Pages.Games.Filter;
using GameIdeas.BlazorApp.Services;
using GameIdeas.BlazorApp.Shared.Constants;
using GameIdeas.BlazorApp.Shared.Exceptions;
using GameIdeas.Resources;
@@ -8,7 +9,7 @@ namespace GameIdeas.BlazorApp.Pages.Games.Gateways;
public class GameGateway(IHttpClientService httpClientService) : IGameGateway
{
public async Task<int> CreateGame(GameDto game)
public async Task<int> CreateGame(GameDetailDto game)
{
try
{
@@ -34,11 +35,26 @@ 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,
StorageSpaces = filterParams.StorageSpaceIds,
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(),
ReleaseYears = filterParams.ReleaseYears,
TagIds = filterParams.Tags?.Select(d => d.Id ?? 0).ToList(),
};
var result = await httpClientService.FetchDataAsync<IEnumerable<GameDto>>(Endpoints.Game.Fetch(filter));
return result ?? throw new InvalidOperationException(ResourcesKey.ErrorFetchGames);
}
catch (Exception)

View File

@@ -1,10 +1,11 @@
using GameIdeas.Shared.Dto;
using GameIdeas.BlazorApp.Pages.Games.Filter;
using GameIdeas.Shared.Dto;
namespace GameIdeas.BlazorApp.Pages.Games.Gateways;
public interface IGameGateway
{
Task<CategoriesDto> FetchCategories();
Task<int> CreateGame(GameDto game);
Task<IEnumerable<GameDto>> FetchGames(PaggingDto pagging);
Task<int> CreateGame(GameDetailDto game);
Task<IEnumerable<GameDto>> FetchGames(GameFilterParams filter, int currentPage);
}

View File

@@ -27,8 +27,7 @@ public partial class GameHeader : ComponentBase
SelectParams = new()
{
Items = AddTypes.ToList(),
GetItemLabel = item => item.Value,
DefaultItems = []
GetItemLabel = item => item.Value
};
base.OnInitialized();

View File

@@ -2,7 +2,7 @@
@using GameIdeas.Shared.Constants
<div class="search-container">
<input @ref=InputText
<input id="searchInput"
type="text"
class="search-field"
placeholder="@Placeholder"
@@ -10,7 +10,7 @@
style="@(IsDisable ? "pointer-events: none" : "")"
@bind=@Text
@bind:event="oninput"
@bind:after=HandleTextChanged
@bind:after="HandleTextChanged"
@onfocusin=HandleFocusIn/>
<div class="buttons">
@@ -25,5 +25,4 @@
@GetSearchIcon()
</div>
</div>
</div>
</div>

View File

@@ -1,5 +1,4 @@
using GameIdeas.BlazorApp.Shared.Constants;
using GameIdeas.Shared.Constants;
using Microsoft.AspNetCore.Components;
namespace GameIdeas.BlazorApp.Shared.Components.Search;
@@ -15,21 +14,31 @@ public partial class SearchInput
[Parameter] public EventCallback FocusIn { get; set; }
[Parameter] public SearchInputIcon Icon { get; set; }
private ElementReference InputText;
private System.Timers.Timer? Timer;
protected override void OnInitialized()
{
Text = string.Empty;
Timer = new()
{
Interval = 500,
AutoReset = false,
};
Timer.Elapsed += async (_, _) => await TextChanged.InvokeAsync(Text);
base.OnInitialized();
}
public void SetText(string str)
{
Text = str;
}
private async Task HandleTextChanged()
private void HandleTextChanged()
{
await TextChanged.InvokeAsync(Text);
Timer?.Stop();
Timer?.Start();
}
private async Task HandleClearClicked()

View File

@@ -3,10 +3,8 @@
public class SelectParams<TItem, THeader>
{
public List<TItem> Items { get; set; } = [];
public List<TItem> DefaultItems { get; set; } = [];
public Func<TItem, string> GetItemLabel { get; set; } = _ => string.Empty;
public List<THeader> Headers { get; set; } = [];
public List<THeader> DefaultHeaders { get; set; } = [];
public Func<THeader, string> GetHeaderLabel { get; set; } = _ => string.Empty;
public Func<string, TItem>? AddItem { get; set; }

View File

@@ -37,16 +37,6 @@ public partial class Select<TItem, THeader>
protected override void OnInitialized()
{
QuickAddEditContext = new EditContext(AddLabel);
if (Params.DefaultItems.Count != 0)
{
Values.AddRange(Params.DefaultItems);
}
if (Params.DefaultHeaders.Count != 0)
{
HeaderValues.AddRange(Params.DefaultHeaders);
}
}
private void HandleButtonClicked()

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

@@ -27,7 +27,6 @@
</div>
<script src="_framework/blazor.webassembly.js"></script>
<script src="Shared/Components/BackdropFilter/BackdropFilter.razor.js"></script>
<script src="Shared/Components/Select/MultipleSelectList.razor.js"></script>
<script src="Pages/Games/Components/GameCreationForm.razor.js"></script>
</body>

View File

@@ -22,6 +22,8 @@ public class Translations (TranslationService translationService)
public string StorageSizeMo => translationService.Translate(nameof(StorageSizeMo));
public string LastModification => translationService.Translate(nameof(LastModification));
public string ReleaseDate => translationService.Translate(nameof(ReleaseDate));
public string CreateDate => translationService.Translate(nameof(CreateDate));
public string UpdateDate => translationService.Translate(nameof(UpdateDate));
public string Title => translationService.Translate(nameof(Title));
public string Interest => translationService.Translate(nameof(Interest));
public string Properties => translationService.Translate(nameof(Properties));
@@ -40,6 +42,12 @@ public class Translations (TranslationService translationService)
public string InvalidInterest => translationService.Translate(nameof(InvalidInterest));
public string Unknown => translationService.Translate(nameof(Unknown));
public string ErrorFetchGames => translationService.Translate(nameof(ErrorFetchGames));
public string Ascending => translationService.Translate(nameof(Ascending));
public string Descending => translationService.Translate(nameof(Descending));
public string ErrorStorageSpaceLabel => translationService.Translate(nameof(ErrorStorageSpaceLabel));
public string MinStorageSpaceFormat => translationService.Translate(nameof(MinStorageSpaceFormat));
public string MaxStorageSpaceFormat => translationService.Translate(nameof(MaxStorageSpaceFormat));
public string MinMaxStorageSpaceFormat => translationService.Translate(nameof(MinMaxStorageSpaceFormat));
}
public static class ResourcesKey
@@ -70,6 +78,8 @@ public static class ResourcesKey
public static string StorageSizeMo => _instance?.StorageSizeMo ?? throw new InvalidOperationException("ResourcesKey.StorageSizeMo is not initialized.");
public static string LastModification => _instance?.LastModification ?? throw new InvalidOperationException("ResourcesKey.LastModification is not initialized.");
public static string ReleaseDate => _instance?.ReleaseDate ?? throw new InvalidOperationException("ResourcesKey.ReleaseDate is not initialized.");
public static string CreateDate => _instance?.CreateDate ?? throw new InvalidOperationException("ResourcesKey.CreateDate is not initialized.");
public static string UpdateDate => _instance?.UpdateDate ?? throw new InvalidOperationException("ResourcesKey.UpdateDate is not initialized.");
public static string Title => _instance?.Title ?? throw new InvalidOperationException("ResourcesKey.Title is not initialized.");
public static string Interest => _instance?.Interest ?? throw new InvalidOperationException("ResourcesKey.Interest is not initialized.");
public static string Properties => _instance?.Properties ?? throw new InvalidOperationException("ResourcesKey.Properties is not initialized.");
@@ -88,4 +98,10 @@ public static class ResourcesKey
public static string InvalidInterest => _instance?.InvalidInterest ?? throw new InvalidOperationException("ResourcesKey.InvalidInterest is not initialized.");
public static string Unknown => _instance?.Unknown ?? throw new InvalidOperationException("ResourcesKey.Unknown is not initialized.");
public static string ErrorFetchGames => _instance?.ErrorFetchGames ?? throw new InvalidOperationException("ResourcesKey.ErrorFetchGames is not initialized.");
public static string Ascending => _instance?.Ascending ?? throw new InvalidOperationException("ResourcesKey.Ascending is not initialized.");
public static string Descending => _instance?.Descending ?? throw new InvalidOperationException("ResourcesKey.Descending is not initialized.");
public static string ErrorStorageSpaceLabel => _instance?.ErrorStorageSpaceLabel ?? throw new InvalidOperationException("ResourcesKey.ErrorStorageSpaceLabel is not initialized.");
public static string MinStorageSpaceFormat => _instance?.MinStorageSpaceFormat ?? throw new InvalidOperationException("ResourcesKey.MinStorageSpaceFormat is not initialized.");
public static string MaxStorageSpaceFormat => _instance?.MaxStorageSpaceFormat ?? throw new InvalidOperationException("ResourcesKey.MaxStorageSpaceFormat is not initialized.");
public static string MinMaxStorageSpaceFormat => _instance?.MinMaxStorageSpaceFormat ?? throw new InvalidOperationException("ResourcesKey.MinMaxStorageSpaceFormat is not initialized.");
}

View File

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

View File

@@ -7,4 +7,6 @@ public class CategoriesDto
public List<TagDto>? Tags { get; set; }
public List<DeveloperDto>? Developers { get; set; }
public List<PublisherDto>? Publishers { get; set; }
public List<int>? ReleaseYears { get; set; }
public List<StorageSpaceDto>? StorageSpaces { get; set; }
}

View File

@@ -0,0 +1,20 @@
namespace GameIdeas.Shared.Dto;
public class GameDetailDto
{
public int? Id { get; set; }
public string? Title { get; set; }
public DateTime? ReleaseDate { get; set; }
public DateTime? CreationDate { get; set; }
public int CreationUserId { get; set; }
public DateTime? ModificationDate { get; set; }
public int? ModificationUserId { get; set; }
public double? StorageSpace { get; set; }
public string? Description { get; set; }
public int Interest { get; set; } = 3;
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; }
}

View File

@@ -5,18 +5,8 @@ public class GameDto
public int? Id { get; set; }
public string? Title { get; set; }
public DateTime? ReleaseDate { get; set; }
public DateTime? CreationDate { get; set; }
public UserDto? CreationUser { get; set; }
public int CreationUserId { get; set; }
public DateTime? ModificationDate { get; set; }
public UserDto? ModificationUser { get; set; }
public int? ModificationUserId { get; set; }
public IEnumerable<PlatformDto>? Platforms { get; set; }
public IEnumerable<TagDto>? Tags { get; set; }
public double? StorageSpace { get; set; }
public string? Description { get; set; }
public int Interest { get; set; } = 3;
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; }
}

View File

@@ -1,19 +1,20 @@

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 int MinInterest { get; set; } = 1;
public int MaxInterest { get; set; } = 5;
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; }
public int? MaxInterest { get; set; }
public List<int>? ReleaseYears { get; set; }
public int? MinStorageSize { get; set; }
public int? MaxStorageSize { get; set; }
public List<int>? StorageSpaces { 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<GameDto, object>? SortProperty { get; set; }
public string PropertyName { get; set; } = string.Empty;
public string Label { get; set; } = string.Empty;
}

View File

@@ -0,0 +1,8 @@
namespace GameIdeas.Shared.Dto;
public class StorageSpaceDto
{
public int? MinSize { get; set; }
public int? MaxSize { get; set; }
public int Id { get; set; }
}

View File

@@ -1,5 +1,5 @@
using GameIdeas.Shared.Dto;
using GameIdeas.WebAPI.Services.Interfaces;
using GameIdeas.WebAPI.Services.Categories;
using Microsoft.AspNetCore.Mvc;
namespace GameIdeas.WebAPI.Controllers;

View File

@@ -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,16 +7,19 @@ 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>();
[HttpGet]
public async Task<ActionResult<IEnumerable<GameDto>>> GetGames([FromQuery] PaggingDto pagging)
public async Task<ActionResult<IEnumerable<GameDto>>> SearchGames([FromQuery] GameFilterDto filter)
{
try
{
return Ok(await gameService.GetGames(pagging));
return Ok(await gameReadService.GetGames(filter));
}
catch (Exception e)
{
@@ -26,11 +29,11 @@ public class GameController(IGameService gameService, ILoggerFactory loggerFacto
}
[HttpGet("{id:int}")]
public async Task<ActionResult<GameDto>> GetGameById(int id)
public async Task<ActionResult<GameDetailDto>> GetGameById(int id)
{
try
{
return Ok(await gameService.GetGameById(id));
return Ok(await gameReadService.GetGameById(id));
}
catch (Exception e)
{
@@ -40,11 +43,11 @@ public class GameController(IGameService gameService, ILoggerFactory loggerFacto
}
[HttpPost("Create")]
public async Task<ActionResult<int>> CreateGame([FromBody] GameDto game)
public async Task<ActionResult<int>> CreateGame([FromBody] GameDetailDto game)
{
try
{
var gameResult = await gameService.CreateGame(game);
var gameResult = await gameWriteService.CreateGame(game);
return Created("/Create", gameResult.Id);
}
catch (Exception e)
@@ -55,11 +58,11 @@ public class GameController(IGameService gameService, ILoggerFactory loggerFacto
}
[HttpPut("Update")]
public async Task<ActionResult<int>> UpdateGame([FromBody] GameDto game)
public async Task<ActionResult<int>> UpdateGame([FromBody] GameDetailDto game)
{
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)
{

View File

@@ -18,6 +18,8 @@
"StorageSizeMo": "Taille d'espace en Mo",
"LastModification": "Dernière modifications",
"ReleaseDate": "Date de parution",
"CreateDate": "Date de création",
"UpdateDate": "Date de modification",
"Title": "Titre",
"Interest": "Intérêt",
"Properties": "Propriétés",
@@ -35,6 +37,11 @@
"InvalidTitle": "Le titre est incorrect",
"InvalidInterest": "L'interêt est incorrect",
"Unknown": "Inconnu",
"ErrorFetchGames": "Erreur lors de la récupération des jeux"
"ErrorFetchGames": "Erreur lors de la récupération des jeux",
"Ascending": "Ascendant",
"Descending": "Descendant",
"ErrorStorageSpaceLabel": "Erreur lors de la génération des label de l'espace de stockage",
"MinStorageSpaceFormat": "Jusqu'à {0}",
"MaxStorageSpaceFormat": "Plus de {0}",
"MinMaxStorageSpaceFormat": "{0} à {1}"
}

View File

@@ -8,16 +8,14 @@ public class GameProfile : Profile
{
public GameProfile()
{
CreateMap<Game, GameDto>()
CreateMap<Game, GameDetailDto>()
.ForMember(d => d.Id, o => o.MapFrom(s => s.Id))
.ForMember(d => d.Title, o => o.MapFrom(s => s.Title))
.ForMember(d => d.ReleaseDate, o => o.MapFrom(s => s.ReleaseDate))
.ForMember(d => d.CreationDate, o => o.MapFrom(s => s.CreationDate))
.ForMember(d => d.CreationUserId, o => o.MapFrom(s => s.CreationUserId))
.ForMember(d => d.CreationUser, o => o.MapFrom(s => s.CreationUser))
.ForMember(d => d.ModificationDate, o => o.MapFrom(s => s.ModificationDate))
.ForMember(d => d.ModificationUserId, o => o.MapFrom(s => s.ModificationUserId))
.ForMember(d => d.ModificationUser, o => o.MapFrom(s => s.ModificationUser))
.ForMember(d => d.StorageSpace, o => o.MapFrom(s => s.StorageSpace))
.ForMember(d => d.Description, o => o.MapFrom(s => s.Description))
.ForMember(d => d.Interest, o => o.MapFrom(s => s.Interest))
@@ -27,5 +25,15 @@ public class GameProfile : Profile
.ForMember(d => d.Publishers, o => o.MapFrom(s => s.GamePublishers.Select(p => p.Publisher)))
.ForMember(d => d.Developers, o => o.MapFrom(s => s.GameDevelopers.Select(gd => gd.Developer)))
.ReverseMap();
CreateMap<Game, GameDto>()
.ForMember(d => d.Id, o => o.MapFrom(s => s.Id))
.ForMember(d => d.Title, o => o.MapFrom(s => s.Title))
.ForMember(d => d.ReleaseDate, o => o.MapFrom(s => s.ReleaseDate))
.ForMember(d => d.Platforms, o => o.MapFrom(s => s.GamePlatforms.Select(p => new PlatformDto() { Id = p.Platform.Id, Label = p.Platform.Label, Url = p.Url })))
.ForMember(d => d.Tags, o => o.MapFrom(s => s.GameTags.Select(t => t.Tag)))
.ForMember(d => d.StorageSpace, o => o.MapFrom(s => s.StorageSpace))
.ForMember(d => d.Interest, o => o.MapFrom(s => s.Interest))
.ReverseMap();
}
}

View File

@@ -1,10 +1,8 @@
using GameIdeas.Resources;
using GameIdeas.WebAPI.Context;
using GameIdeas.WebAPI.Profiles;
using GameIdeas.WebAPI.Services;
using GameIdeas.WebAPI.Services.Interfaces;
using GameIdeas.WebAPI.Services.Categories;
using GameIdeas.WebAPI.Services.Games;
using Microsoft.EntityFrameworkCore;
using System.Text.Json.Serialization;
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
@@ -32,7 +30,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());

View File

@@ -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()
};
}
}

View File

@@ -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();
}

View File

@@ -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();
}
}
}

View File

@@ -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)

View File

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

View File

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

View File

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