From 70c2f7342d01c5125b157626f2faf7598eeb8843 Mon Sep 17 00:00:00 2001 From: TekH Date: Thu, 16 Apr 2026 10:49:15 +0200 Subject: [PATCH] Refactor result view query to support LastBatch retrieval Replaced Last with LastBatch in ReadResultViewQuery to enable fetching all results from the most recent batch. Updated handler logic and tests accordingly, and added GetLastBatchEntitiesAsync to retrieve entities by latest BatchId. --- .../Results/Queries/ReadResultViewQuery.cs | 22 +++++++++++++++++-- .../InvokeBatchDuplicateGuardTests.cs | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/ReC.Application/Results/Queries/ReadResultViewQuery.cs b/src/ReC.Application/Results/Queries/ReadResultViewQuery.cs index f48b0c5..5aec3fb 100644 --- a/src/ReC.Application/Results/Queries/ReadResultViewQuery.cs +++ b/src/ReC.Application/Results/Queries/ReadResultViewQuery.cs @@ -23,7 +23,7 @@ public record ReadResultViewQuery : IRequest> public bool IncludeProfile { get; init; } = false; - public bool Last { get; init; } = false; + public bool LastBatch { get; init; } = false; } public class ReadResultViewQueryHandler(IRepository repo, IMapper mapper) : IRequestHandler> @@ -50,7 +50,9 @@ public class ReadResultViewQueryHandler(IRepository repo, IMapper ma if(request.IncludeProfile) q = q.Include(rv => rv.Profile); - var entities = request.Last ? [await q.OrderBy(rv => rv.AddedWhen).LastOrDefaultAsync(cancel)] : await q.ToListAsync(cancel); + var entities = request.LastBatch + ? await GetLastBatchEntitiesAsync(q, cancel) + : await q.ToListAsync(cancel); if (entities.Count == 0) throw new NotFoundException($"No result views found for the given criteria. Criteria: { @@ -63,4 +65,20 @@ public class ReadResultViewQueryHandler(IRepository repo, IMapper ma return mapper.Map>(entities); } + + private static async Task> GetLastBatchEntitiesAsync(IQueryable q, CancellationToken cancel) + { + var lastBatchId = await q + .Where(rv => rv.BatchId != null) + .OrderByDescending(rv => rv.AddedWhen) + .Select(rv => rv.BatchId) + .FirstOrDefaultAsync(cancel); + + if (lastBatchId is null) + return []; + + return await q + .Where(rv => rv.BatchId == lastBatchId) + .ToListAsync(cancel); + } } \ No newline at end of file diff --git a/tests/ReC.Tests/Application/RecActions/InvokeBatchDuplicateGuardTests.cs b/tests/ReC.Tests/Application/RecActions/InvokeBatchDuplicateGuardTests.cs index 41a0475..27df0a7 100644 --- a/tests/ReC.Tests/Application/RecActions/InvokeBatchDuplicateGuardTests.cs +++ b/tests/ReC.Tests/Application/RecActions/InvokeBatchDuplicateGuardTests.cs @@ -32,7 +32,7 @@ public class InvokeBatchDuplicateGuardTests : RecApplicationTestBase { ProfileId = ProfileId, IncludeAction = false, - Last = true + LastBatch = true }); var existingBatchId = results.FirstOrDefault()?.BatchId;