Skip to content

Commit

Permalink
Ability to filter games by genre and group (#1599)
Browse files Browse the repository at this point in the history
* ability to filter games be genre
fixes #1532

* filter games by group
  • Loading branch information
vadosnaprimer authored Jul 10, 2023
1 parent c4b1fc5 commit 9ca8e12
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 1 deletion.
14 changes: 14 additions & 0 deletions TASVideos.Data/Entity/Game/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ public static IQueryable<Game> ForSystemCodes(this IQueryable<Game> query, IEnum
: query;
}

public static IQueryable<Game> ForGenre(this IQueryable<Game> query, string? genre)
{
return !string.IsNullOrWhiteSpace(genre)
? query.Where(g => g.GameGenres.Any(gg => gg.Genre!.DisplayName == genre))
: query;
}

public static IQueryable<Game> ForGroup(this IQueryable<Game> query, string? group)
{
return !string.IsNullOrWhiteSpace(group)
? query.Where(g => g.GameGroups.Any(gg => gg.GameGroup!.Name == group))
: query;
}

public static void SetGenres(this ICollection<GameGenre> genres, IEnumerable<int> genreIds)
{
genres.Clear();
Expand Down
14 changes: 14 additions & 0 deletions TASVideos/Pages/Games/List.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@
</div>
</div>
</div>
<div class="row mb-1">
<div class="col-5 mb-1">
<div class="input-group">
<label asp-for="@Model.Games.Genre" class="input-group-text"></label>
<select asp-for="@Model.Games.Genre" asp-items="@Model.GenreList" name="Genre" class="form-control"></select>
</div>
</div>
<div class="col-5 mb-1">
<div class="input-group">
<label asp-for="@Model.Games.Group" class="input-group-text"></label>
<select asp-for="@Model.Games.Group" asp-items="@Model.GroupList" name="Group" class="form-control"></select>
</div>
</div>
</div>
</form>
</div>
<div class="col">
Expand Down
28 changes: 28 additions & 0 deletions TASVideos/Pages/Games/List.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public ListModel(

public List<SelectListItem> LetterList { get; set; } = new();

public List<SelectListItem> GenreList { get; set; } = new();

public List<SelectListItem> GroupList { get; set; } = new();

public async Task OnGet()
{
if (ModelState.IsValid)
Expand All @@ -56,6 +60,24 @@ public async Task OnGet()
.ToListAsync();

LetterList.Insert(0, new SelectListItem { Text = "Any", Value = "" });

GenreList = await _db.Genres
.Select(g => g.DisplayName)
.Distinct()
.OrderBy(l => l)
.ToDropdown()
.ToListAsync();

GenreList.Insert(0, new SelectListItem { Text = "Any", Value = "" });

GroupList = await _db.GameGroups
.Select(g => g.Name)
.Distinct()
.OrderBy(l => l)
.ToDropdown()
.ToListAsync();

GroupList.Insert(0, new SelectListItem { Text = "Any", Value = "" });
}

public async Task<IActionResult> OnGetFrameRateDropDownForSystem(int systemId, bool includeEmpty)
Expand Down Expand Up @@ -130,6 +152,8 @@ private async Task<SystemPageOf<GameListModel>> GetPageOfGames(GameListRequest p
_db.Database.SetCommandTimeout(TimeSpan.FromSeconds(30));
data = await _db.Games
.ForSystemCode(paging.SystemCode)
.ForGenre(paging.Genre)
.ForGroup(paging.Group)
.Where(g => g.DisplayName.StartsWith(paging.Letter ?? ""))
.Where(g => EF.Functions.ToTsVector(g.DisplayName + " || " + g.Aliases + " || " + g.Abbreviation).Matches(EF.Functions.WebSearchToTsQuery(paging.SearchTerms)))
.Select(g => new GameListModel
Expand All @@ -144,6 +168,8 @@ private async Task<SystemPageOf<GameListModel>> GetPageOfGames(GameListRequest p
{
data = await _db.Games
.ForSystemCode(paging.SystemCode)
.ForGenre(paging.Genre)
.ForGroup(paging.Group)
.Where(g => g.DisplayName.StartsWith(paging.Letter ?? ""))
.Select(g => new GameListModel
{
Expand All @@ -158,6 +184,8 @@ private async Task<SystemPageOf<GameListModel>> GetPageOfGames(GameListRequest p
{
SystemCode = paging.SystemCode,
Letter = paging.Letter,
Genre = paging.Genre,
Group = paging.Group,
SearchTerms = paging.SearchTerms,
PageSize = data.PageSize,
CurrentPage = data.CurrentPage,
Expand Down
4 changes: 4 additions & 0 deletions TASVideos/Pages/Games/Models/GameListRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ public GameListRequest()

public string? Letter { get; set; }

public string? Genre { get; set; }

public string? Group { get; set; }

public string? SearchTerms { get; set; }
}
6 changes: 6 additions & 0 deletions TASVideos/Pages/Games/Models/SystemPageOf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public SystemPageOf(IEnumerable<T> items)
[Display(Name = "Starts with")]
public string? Letter { get; set; }

[Display(Name = "Genre")]
public string? Genre { get; set; }

[Display(Name = "Group")]
public string? Group { get; set; }

public string? SearchTerms { get; set; }

public static new SystemPageOf<T> Empty() => new(Enumerable.Empty<T>());
Expand Down
6 changes: 5 additions & 1 deletion TASVideos/Pages/Genres/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
{
<tr>
<td>@genre.DisplayName</td>
<td>@genre.GameCount</td>
<td>
<a href="/Games/List?Genre=@genre.DisplayName">
@genre.GameCount
</a>
</td>
<td>
<a class="btn btn-primary" asp-page="Edit" asp-route-id="@genre.Id">
<i class="fa fa-pencil"></i> Edit
Expand Down

0 comments on commit 9ca8e12

Please sign in to comment.