Skip to content

Commit

Permalink
Add table pagination support to time attack page (#1063)
Browse files Browse the repository at this point in the history
With this the page is complete.
  • Loading branch information
SapiensAnatis authored Sep 5, 2024
1 parent 2f337dd commit 0f7b393
Show file tree
Hide file tree
Showing 19 changed files with 14,886 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Net.Http.Json;
using DragaliaAPI.Features.Web;
using DragaliaAPI.Features.Web.TimeAttack.Models;
using NSubstitute.Extensions;

namespace DragaliaAPI.Integration.Test.Features.Web.TimeAttack;

Expand Down Expand Up @@ -32,12 +34,14 @@ public async Task GetRankings_ReturnsRankings_FiltersDuplicateClearsFromSamePlay
{
await this.SeedTimeAttackData();

List<TimeAttackRanking>? rankings = await this.Client.GetFromJsonAsync<
List<TimeAttackRanking>
OffsetPagedResponse<TimeAttackRanking>? rankings = await this.Client.GetFromJsonAsync<
OffsetPagedResponse<TimeAttackRanking>
>("/api/time_attack/rankings/227010105");

rankings?.Pagination.TotalCount.Should().Be(2);

rankings
.Should()
?.Data.Should()
.BeEquivalentTo(
[
new()
Expand Down Expand Up @@ -77,12 +81,12 @@ public async Task GetRankings_ParsesTeamDataFromJson()

AssertionOptions.FormattingOptions.MaxLines = 10000;

List<TimeAttackRanking>? rankings = await this.Client.GetFromJsonAsync<
List<TimeAttackRanking>
OffsetPagedResponse<TimeAttackRanking>? rankings = await this.Client.GetFromJsonAsync<
OffsetPagedResponse<TimeAttackRanking>
>("/api/time_attack/rankings/227010104");

rankings
.Should()
?.Data.Should()
.BeEquivalentTo(
[
new TimeAttackRanking()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace DragaliaAPI.Features.Web.News;
[ApiController]
[Route("/api/news")]
[AllowAnonymous]
public sealed class NewsController(NewsService newsService) : ControllerBase
internal sealed class NewsController(NewsService newsService) : ControllerBase
{
[HttpGet]
public async Task<OffsetPagedResponse<NewsItem>> GetNews(
Expand Down
19 changes: 15 additions & 4 deletions DragaliaAPI/DragaliaAPI/Features/Web/OffsetPagedResponse.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
using System.Diagnostics.CodeAnalysis;

namespace DragaliaAPI.Features.Web;

public class OffsetPagedResponse<TData>
internal sealed class OffsetPagedResponse<TData>
{
public required OffsetPagingMetadata Pagination { get; set; }
public OffsetPagedResponse() { }

[SetsRequiredMembers]
public OffsetPagedResponse(int totalCount, IList<TData> data)
{
this.Pagination = new() { TotalCount = totalCount };
this.Data = data;
}

public required OffsetPagingMetadata Pagination { get; init; }

public required IList<TData> Data { get; set; }
public required IList<TData> Data { get; init; }
}

public class OffsetPagingMetadata
internal sealed class OffsetPagingMetadata
{
public required int TotalCount { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public async Task<ActionResult<List<TimeAttackQuest>>> GetQuests() =>
await timeAttackService.GetQuests();

[HttpGet("rankings/{questId:int}")]
public async Task<List<TimeAttackRanking>> GetRankings(
public async Task<OffsetPagedResponse<TimeAttackRanking>> GetRankings(
int questId,
[FromQuery] int offset = 0,
[FromQuery] int pageSize = 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public async Task<List<TimeAttackQuest>> GetQuests()
.ToList();
}

public async Task<List<TimeAttackRanking>> GetRankings(int questId, int offset, int pageSize)
public async Task<OffsetPagedResponse<TimeAttackRanking>> GetRankings(
int questId,
int offset,
int pageSize
)
{
var clears = LinqExtensions
.InnerJoin(
Expand Down Expand Up @@ -84,6 +88,8 @@ public async Task<List<TimeAttackRanking>> GetRankings(int questId, int offset,
.Distinct()
.AsCte("clears_unique_by_players");

int totalCount = await uniqueClears.CountAsyncLinqToDB();

var playerInfo = uniqueClears.GroupJoin(
apiContext.TimeAttackPlayers,
arg => arg.GameId,
Expand Down Expand Up @@ -113,23 +119,25 @@ public async Task<List<TimeAttackRanking>> GetRankings(int questId, int offset,

var results = await playerInfo.Skip(offset).Take(pageSize).ToListAsyncLinqToDB();

IEnumerable<TimeAttackRanking> mappedResults = results.Select(x =>
{
return new TimeAttackRanking()
List<TimeAttackRanking> mappedResults = results
.Select(x =>
{
Rank = (int)x.Rank,
Time = x.Time,
Players = x
.Players.Select(y => new TimeAttackPlayer()
{
Name = y.Name,
Units = MapUnits(y.PartyInfo),
})
.ToList(),
};
});
return new TimeAttackRanking()
{
Rank = (int)x.Rank,
Time = x.Time,
Players = x
.Players.Select(y => new TimeAttackPlayer()
{
Name = y.Name,
Units = MapUnits(y.PartyInfo),
})
.ToList(),
};
})
.ToList();

return mappedResults.ToList();
return new OffsetPagedResponse<TimeAttackRanking>(totalCount, mappedResults);
}

private static List<TimeAttackUnit> MapUnits(string partyInfoJson)
Expand Down
1 change: 0 additions & 1 deletion Website/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ PUBLIC_BAAS_URL=https://baas.lukefz.xyz/ # The URL of the BaaS server to use for
PUBLIC_BAAS_CLIENT_ID=dawnshard # The client ID to present to the BaaS during OAuth.
PUBLIC_VERSION=0.0.0 # A version number to add in the HEAD. Set by NerdBank.GitVersioning during a CI build.
PUBLIC_CDN_URL=https://cdn.minty.sbs/ # The base path of a CDN to acquire image assets from.
PUBLIC_ENABLE_TIME_ATTACK=false # Time attack page feature flag

DAWNSHARD_API_URL_SSR=http://localhost:5000/ # The internal URL of the main DragaliaAPI C# server.
1 change: 0 additions & 1 deletion Website/.env.development
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
PUBLIC_ENABLE_MSW=true # Enables Mock Service Worker for API calls and decreases cookie security.
PUBLIC_VERSION=development
PUBLIC_ENABLE_TIME_ATTACK=true

DAWNSHARD_API_URL_SSR=http://localhost:5000/
Loading

0 comments on commit 0f7b393

Please sign in to comment.