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.
This commit is contained in:
2026-04-16 10:49:15 +02:00
parent a10f917084
commit 70c2f7342d
2 changed files with 21 additions and 3 deletions

View File

@@ -23,7 +23,7 @@ public record ReadResultViewQuery : IRequest<IEnumerable<ResultViewDto>>
public bool IncludeProfile { get; init; } = false; public bool IncludeProfile { get; init; } = false;
public bool Last { get; init; } = false; public bool LastBatch { get; init; } = false;
} }
public class ReadResultViewQueryHandler(IRepository<ResultView> repo, IMapper mapper) : IRequestHandler<ReadResultViewQuery, IEnumerable<ResultViewDto>> public class ReadResultViewQueryHandler(IRepository<ResultView> repo, IMapper mapper) : IRequestHandler<ReadResultViewQuery, IEnumerable<ResultViewDto>>
@@ -50,7 +50,9 @@ public class ReadResultViewQueryHandler(IRepository<ResultView> repo, IMapper ma
if(request.IncludeProfile) if(request.IncludeProfile)
q = q.Include(rv => rv.Profile); 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) if (entities.Count == 0)
throw new NotFoundException($"No result views found for the given criteria. Criteria: { throw new NotFoundException($"No result views found for the given criteria. Criteria: {
@@ -63,4 +65,20 @@ public class ReadResultViewQueryHandler(IRepository<ResultView> repo, IMapper ma
return mapper.Map<IEnumerable<ResultViewDto>>(entities); return mapper.Map<IEnumerable<ResultViewDto>>(entities);
} }
private static async Task<List<ResultView>> GetLastBatchEntitiesAsync(IQueryable<ResultView> 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);
}
} }

View File

@@ -32,7 +32,7 @@ public class InvokeBatchDuplicateGuardTests : RecApplicationTestBase
{ {
ProfileId = ProfileId, ProfileId = ProfileId,
IncludeAction = false, IncludeAction = false,
Last = true LastBatch = true
}); });
var existingBatchId = results.FirstOrDefault()?.BatchId; var existingBatchId = results.FirstOrDefault()?.BatchId;