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:
@@ -23,7 +23,7 @@ public record ReadResultViewQuery : IRequest<IEnumerable<ResultViewDto>>
|
||||
|
||||
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>>
|
||||
@@ -50,7 +50,9 @@ public class ReadResultViewQueryHandler(IRepository<ResultView> 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<ResultView> repo, IMapper ma
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ public class InvokeBatchDuplicateGuardTests : RecApplicationTestBase
|
||||
{
|
||||
ProfileId = ProfileId,
|
||||
IncludeAction = false,
|
||||
Last = true
|
||||
LastBatch = true
|
||||
});
|
||||
|
||||
var existingBatchId = results.FirstOrDefault()?.BatchId;
|
||||
|
||||
Reference in New Issue
Block a user