From cb12cfcbc25c5ad89f437a3cadce2a07fb0ebca0 Mon Sep 17 00:00:00 2001 From: TekH Date: Mon, 3 Aug 2026 16:29:35 +0200 Subject: [PATCH] Refactor query handlers to remove NotFoundException Refactored query handlers (`ReadProfileViewQueryHandler`, `ReadRecActionViewQueryHandler`, and `ReadResultViewQueryHandler`) to return empty results instead of throwing `NotFoundException` when no data is found. Simplified the logic by removing null or empty result checks and directly returning mapped results. Updated corresponding test cases to align with the new behavior: - Removed `try-catch` blocks for `NotFoundException`. - Adjusted assertions to handle empty results. - Removed the test case for `ReadRecActionViewQuery` that expected `NotFoundException`. This change reflects a design shift to let the calling code handle empty results instead of relying on exceptions. --- .../Profile/Queries/ReadProfileViewQuery.cs | 4 +-- .../Queries/ReadRecActionViewQuery.cs | 3 -- .../Results/Queries/ReadResultViewQuery.cs | 9 ------ .../Application/Profile/ProfileQueryTests.cs | 25 ++++++--------- .../RecActions/RecActionQueryTests.cs | 31 +++---------------- .../Application/Results/ResultQueryTests.cs | 4 --- 6 files changed, 15 insertions(+), 61 deletions(-) diff --git a/src/ReC.Application/Profile/Queries/ReadProfileViewQuery.cs b/src/ReC.Application/Profile/Queries/ReadProfileViewQuery.cs index a762d33..8db8395 100644 --- a/src/ReC.Application/Profile/Queries/ReadProfileViewQuery.cs +++ b/src/ReC.Application/Profile/Queries/ReadProfileViewQuery.cs @@ -29,8 +29,6 @@ public class ReadProfileViewQueryHandler(IRepository repo, IMapper var profiles = await query.ToListAsync(cancel); - return profiles is null || profiles.Count == 0 - ? throw new NotFoundException($"Profile {request.Id} not found.") - : mapper.Map>(profiles); + return mapper.Map>(profiles); } } \ No newline at end of file diff --git a/src/ReC.Application/RecActions/Queries/ReadRecActionViewQuery.cs b/src/ReC.Application/RecActions/Queries/ReadRecActionViewQuery.cs index 4501cd4..ea326ec 100644 --- a/src/ReC.Application/RecActions/Queries/ReadRecActionViewQuery.cs +++ b/src/ReC.Application/RecActions/Queries/ReadRecActionViewQuery.cs @@ -31,9 +31,6 @@ public class ReadRecActionViewQueryHandler(IRepository repo, IMap var actions = await query.ToListAsync(cancel); - if (actions.Count == 0) - throw new NotFoundException($"No actions found for the profile {request.ProfileId}."); - return mapper.Map>(actions); } } \ No newline at end of file diff --git a/src/ReC.Application/Results/Queries/ReadResultViewQuery.cs b/src/ReC.Application/Results/Queries/ReadResultViewQuery.cs index 5aec3fb..9cdcf04 100644 --- a/src/ReC.Application/Results/Queries/ReadResultViewQuery.cs +++ b/src/ReC.Application/Results/Queries/ReadResultViewQuery.cs @@ -54,15 +54,6 @@ public class ReadResultViewQueryHandler(IRepository repo, IMapper ma ? await GetLastBatchEntitiesAsync(q, cancel) : await q.ToListAsync(cancel); - if (entities.Count == 0) - throw new NotFoundException($"No result views found for the given criteria. Criteria: { - JsonSerializer.Serialize(request, options: new() - { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - WriteIndented = true - })}" - ); - return mapper.Map>(entities); } diff --git a/tests/ReC.Tests/Application/Profile/ProfileQueryTests.cs b/tests/ReC.Tests/Application/Profile/ProfileQueryTests.cs index 8202bbf..fc53f7a 100644 --- a/tests/ReC.Tests/Application/Profile/ProfileQueryTests.cs +++ b/tests/ReC.Tests/Application/Profile/ProfileQueryTests.cs @@ -30,23 +30,18 @@ public class ProfileQueryTests : RecApplicationTestBase var (sender, scope) = CreateScopedSender(); using var _ = scope; - try + var profiles = await sender.Send(new ReadProfileViewQuery { - var profiles = await sender.Send(new ReadProfileViewQuery - { - Id = profileId, - IncludeActions = false - }); + Id = profileId, + IncludeActions = false + }); - var profile = profiles.Single(); - - Assert.That(profile.Id, Is.EqualTo(profileId)); - Assert.That(profile.ProfileName, Is.Not.Null.And.Not.Empty); - Assert.That(profile.Active, Is.True); - } - catch (NotFoundException) + var profile = profiles.SingleOrDefault(); + Assert.Multiple(() => { - Assert.Pass("NotFound is acceptable when profile does not exist"); - } + Assert.That(profile?.Id, Is.EqualTo(profileId)); + Assert.That(profile?.ProfileName, Is.Not.Null.And.Not.Empty); + Assert.That(profile?.Active, Is.True); + }); } } diff --git a/tests/ReC.Tests/Application/RecActions/RecActionQueryTests.cs b/tests/ReC.Tests/Application/RecActions/RecActionQueryTests.cs index 03419b0..07f9aba 100644 --- a/tests/ReC.Tests/Application/RecActions/RecActionQueryTests.cs +++ b/tests/ReC.Tests/Application/RecActions/RecActionQueryTests.cs @@ -30,34 +30,11 @@ public class RecActionQueryTests : RecApplicationTestBase var (sender, scope) = CreateScopedSender(); using var _ = scope; - try + var actions = await sender.Send(new ReadRecActionViewQuery { - var actions = await sender.Send(new ReadRecActionViewQuery - { - ProfileId = profileId - }); + ProfileId = profileId + }); - Assert.That(actions, Is.Not.Empty); - Assert.That(actions.All(a => a.ProfileId == profileId)); - } - catch (NotFoundException) - { - Assert.Pass("NotFound is acceptable when test data is unavailable"); - } - } - - [Test] - public void ReadRecActionViewQuery_with_unknown_profile_throws_not_found() - { - var (sender, scope) = CreateScopedSender(); - using var _ = scope; - - var invalidProfileId = long.MaxValue; - - Assert.ThrowsAsync(async () => - await sender.Send(new ReadRecActionViewQuery - { - ProfileId = invalidProfileId - })); + Assert.That(actions.All(a => a.ProfileId == profileId)); } } diff --git a/tests/ReC.Tests/Application/Results/ResultQueryTests.cs b/tests/ReC.Tests/Application/Results/ResultQueryTests.cs index bc2b9c4..7afe4f4 100644 --- a/tests/ReC.Tests/Application/Results/ResultQueryTests.cs +++ b/tests/ReC.Tests/Application/Results/ResultQueryTests.cs @@ -34,9 +34,5 @@ public class ResultQueryTests : RecApplicationTestBase Assert.Pass("Read completed for unknown action id."); } - catch (NotFoundException) - { - Assert.Pass("NotFound is acceptable for unknown action"); - } } }