Add 'Last' option to ReadResultViewQuery for latest result

Added a Last property to ReadResultViewQuery to allow fetching only the most recent ResultView entity. Updated the handler logic to return either the last result or all results based on this property.
This commit is contained in:
2026-03-02 16:17:21 +01:00
parent 0e7870b556
commit 0c8d7f6b3c

View File

@@ -20,6 +20,8 @@ public record ReadResultViewQuery : IRequest<IEnumerable<ResultViewDto>>
public bool IncludeAction { get; init; } = true; public bool IncludeAction { get; init; } = true;
public bool IncludeProfile { get; init; } = false; public bool IncludeProfile { get; init; } = false;
public bool Last { 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>>
@@ -43,7 +45,7 @@ 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 = await q.ToListAsync(cancel); var entities = request.Last ? [await q.OrderBy(rv => rv.AddedWhen).LastOrDefaultAsync(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: {