From 7f40e2e9f62eeeb72ec2fd7d525ca7412b3e8420 Mon Sep 17 00:00:00 2001 From: zysim <9867871+zysim@users.noreply.github.com> Date: Tue, 17 Sep 2024 17:32:17 +0800 Subject: [PATCH] Exclude deleted boards in action --- LeaderboardBackend.Test/Leaderboards.cs | 25 ++++++++++++++++++- .../Services/Impl/LeaderboardService.cs | 3 +-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/LeaderboardBackend.Test/Leaderboards.cs b/LeaderboardBackend.Test/Leaderboards.cs index d6d58277..dad523eb 100644 --- a/LeaderboardBackend.Test/Leaderboards.cs +++ b/LeaderboardBackend.Test/Leaderboards.cs @@ -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; @@ -135,7 +138,27 @@ await _apiClient.Post( } CreateLeaderboardRequest reqForInexistentBoard = _createBoardReqFaker.Generate(); - Func> act = async () => await _apiClient.Get($"/api/leaderboard?slug={reqForInexistentBoard.Slug}", new()); + Func> act = async () => await _apiClient.Get($"/api/leaderboard?slug={reqForInexistentBoard.Slug}", new()); + await act.Should().ThrowAsync().Where(e => e.Response.StatusCode == HttpStatusCode.NotFound); + } + + [Test] + public async Task GetLeaderboards_Deleted_BySlug_NotFound() + { + ApplicationContext context = _factory.Services.CreateScope().ServiceProvider.GetRequiredService(); + 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> act = async () => await _apiClient.Get($"/api/leaderboard?slug={board.Slug}", new()); await act.Should().ThrowAsync().Where(e => e.Response.StatusCode == HttpStatusCode.NotFound); } diff --git a/LeaderboardBackend/Services/Impl/LeaderboardService.cs b/LeaderboardBackend/Services/Impl/LeaderboardService.cs index 0f08e020..20678140 100644 --- a/LeaderboardBackend/Services/Impl/LeaderboardService.cs +++ b/LeaderboardBackend/Services/Impl/LeaderboardService.cs @@ -10,8 +10,7 @@ public class LeaderboardService(ApplicationContext applicationContext) : ILeader public async Task 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> GetLeaderboards(long[]? ids = null)