Skip to content

Commit

Permalink
Exclude deleted boards in action
Browse files Browse the repository at this point in the history
  • Loading branch information
zysim committed Sep 17, 2024
1 parent dfda9a8 commit 7f40e2e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
25 changes: 24 additions & 1 deletion LeaderboardBackend.Test/Leaderboards.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using LeaderboardBackend.Models.Entities;
using LeaderboardBackend.Models.Requests;
using LeaderboardBackend.Models.ViewModels;
using LeaderboardBackend.Test.TestApi;
using LeaderboardBackend.Test.TestApi.Extensions;
using Microsoft.Extensions.DependencyInjection;
using NodaTime;
using NUnit.Framework;

namespace LeaderboardBackend.Test;
Expand Down Expand Up @@ -135,7 +138,27 @@ await _apiClient.Post<LeaderboardViewModel>(
}

CreateLeaderboardRequest reqForInexistentBoard = _createBoardReqFaker.Generate();
Func<Task<string>> act = async () => await _apiClient.Get<string>($"/api/leaderboard?slug={reqForInexistentBoard.Slug}", new());
Func<Task<LeaderboardViewModel>> act = async () => await _apiClient.Get<LeaderboardViewModel>($"/api/leaderboard?slug={reqForInexistentBoard.Slug}", new());
await act.Should().ThrowAsync<RequestFailureException>().Where(e => e.Response.StatusCode == HttpStatusCode.NotFound);
}

[Test]
public async Task GetLeaderboards_Deleted_BySlug_NotFound()
{
ApplicationContext context = _factory.Services.CreateScope().ServiceProvider.GetRequiredService<ApplicationContext>();
Leaderboard board = new()
{
Name = "Should 404",
Slug = "should-404",
CreatedAt = Instant.FromUnixTimeSeconds(0),
UpdatedAt = Instant.FromUnixTimeSeconds(0),
DeletedAt = Instant.FromUnixTimeSeconds(0),
};

context.Leaderboards.Add(board);
await context.SaveChangesAsync();

Func<Task<LeaderboardViewModel>> act = async () => await _apiClient.Get<LeaderboardViewModel>($"/api/leaderboard?slug={board.Slug}", new());
await act.Should().ThrowAsync<RequestFailureException>().Where(e => e.Response.StatusCode == HttpStatusCode.NotFound);
}

Expand Down
3 changes: 1 addition & 2 deletions LeaderboardBackend/Services/Impl/LeaderboardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ public class LeaderboardService(ApplicationContext applicationContext) : ILeader

public async Task<Leaderboard?> GetLeaderboardBySlug(string slug) =>
await applicationContext.Leaderboards
.AsNoTracking()
.FirstOrDefaultAsync(x => x.Slug == slug);
.FirstOrDefaultAsync(b => b.Slug == slug && b.DeletedAt == null);

// FIXME: Paginate this
public async Task<List<Leaderboard>> GetLeaderboards(long[]? ids = null)
Expand Down

0 comments on commit 7f40e2e

Please sign in to comment.