Skip to content

Commit

Permalink
includeDeleted Param (#255)
Browse files Browse the repository at this point in the history
* Test the includeDeleted parameter.

* Add includeDeleted param to ListLeaderboards.

* Add query includeDeleted query param.
  • Loading branch information
TheTedder authored Oct 29, 2024
1 parent 08f8b06 commit a569c2f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
7 changes: 7 additions & 0 deletions LeaderboardBackend.Test/Leaderboards.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ public async Task GetLeaderboards()
Name = "Link: The Faces of Evil",
Slug = "link-faces-of-evil",
Info = "Nobody should play this one.",
UpdatedAt = _clock.GetCurrentInstant(),
DeletedAt = _clock.GetCurrentInstant()
}
];
Expand All @@ -350,6 +351,12 @@ public async Task GetLeaderboards()
await context.SaveChangesAsync();
LeaderboardViewModel[] returned = await _apiClient.Get<LeaderboardViewModel[]>("/api/leaderboards", new());
returned.Should().BeEquivalentTo(boards.Take(2), config => config.Excluding(lb => lb.Categories));

LeaderboardViewModel[] returned2 = await _apiClient.Get<LeaderboardViewModel[]>("/api/leaderboards?includeDeleted=false", new());
returned2.Should().BeEquivalentTo(boards.Take(2), config => config.Excluding(lb => lb.Categories));

LeaderboardViewModel[] returned3 = await _apiClient.Get<LeaderboardViewModel[]>("/api/leaderboards?includeDeleted=true", new());
returned3.Should().BeEquivalentTo(boards, config => config.Excluding(lb => lb.Categories));
}

[Test]
Expand Down
4 changes: 2 additions & 2 deletions LeaderboardBackend/Controllers/LeaderboardsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public async Task<ActionResult<LeaderboardViewModel>> GetLeaderboardBySlug([From
[HttpGet("api/leaderboards")]
[SwaggerOperation("Gets all leaderboards.", OperationId = "listLeaderboards")]
[SwaggerResponse(200)]
public async Task<ActionResult<List<LeaderboardViewModel>>> GetLeaderboards()
public async Task<ActionResult<List<LeaderboardViewModel>>> GetLeaderboards([FromQuery] bool includeDeleted = false)
{
// TODO: Paginate.

List<Leaderboard> result = await leaderboardService.ListLeaderboards();
List<Leaderboard> result = await leaderboardService.ListLeaderboards(includeDeleted);
return Ok(result.Select(LeaderboardViewModel.MapFrom));
}

Expand Down
2 changes: 1 addition & 1 deletion LeaderboardBackend/Services/ILeaderboardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface ILeaderboardService
{
Task<Leaderboard?> GetLeaderboard(long id);
Task<Leaderboard?> GetLeaderboardBySlug(string slug);
Task<List<Leaderboard>> ListLeaderboards();
Task<List<Leaderboard>> ListLeaderboards(bool includeDeleted);
Task<CreateLeaderboardResult> CreateLeaderboard(CreateLeaderboardRequest request);
Task<RestoreLeaderboardResult> RestoreLeaderboard(long id);
Task<DeleteResult> DeleteLeaderboard(long id);
Expand Down
8 changes: 5 additions & 3 deletions LeaderboardBackend/Services/Impl/LeaderboardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ await applicationContext.Leaderboards
.FirstOrDefaultAsync(b => b.Slug == slug && b.DeletedAt == null);

// FIXME: Paginate these
public async Task<List<Leaderboard>> ListLeaderboards() =>
await applicationContext.Leaderboards
.Where(lb => lb.DeletedAt == null).ToListAsync();
public async Task<List<Leaderboard>> ListLeaderboards(bool includeDeleted)
{
IQueryable<Leaderboard> lbs = applicationContext.Leaderboards;
return await (includeDeleted ? lbs : lbs.Where(lb => lb.DeletedAt == null)).ToListAsync();
}

public async Task<CreateLeaderboardResult> CreateLeaderboard(CreateLeaderboardRequest request)
{
Expand Down
10 changes: 10 additions & 0 deletions LeaderboardBackend/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,16 @@
],
"summary": "Gets all leaderboards.",
"operationId": "listLeaderboards",
"parameters": [
{
"name": "includeDeleted",
"in": "query",
"schema": {
"type": "boolean",
"default": false
}
}
],
"responses": {
"400": {
"description": "Bad Request",
Expand Down

0 comments on commit a569c2f

Please sign in to comment.