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.
This commit is contained in:
2026-08-03 16:29:35 +02:00
parent 4380f17bdb
commit cb12cfcbc2
6 changed files with 15 additions and 61 deletions

View File

@@ -29,8 +29,6 @@ public class ReadProfileViewQueryHandler(IRepository<ProfileView> 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<IEnumerable<ProfileViewDto>>(profiles);
return mapper.Map<IEnumerable<ProfileViewDto>>(profiles);
}
}

View File

@@ -31,9 +31,6 @@ public class ReadRecActionViewQueryHandler(IRepository<RecActionView> 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<IEnumerable<RecActionViewDto>>(actions);
}
}

View File

@@ -54,15 +54,6 @@ public class ReadResultViewQueryHandler(IRepository<ResultView> 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<IEnumerable<ResultViewDto>>(entities);
}

View File

@@ -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);
});
}
}

View File

@@ -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<NotFoundException>(async () =>
await sender.Send(new ReadRecActionViewQuery
{
ProfileId = invalidProfileId
}));
Assert.That(actions.All(a => a.ProfileId == profileId));
}
}

View File

@@ -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");
}
}
}