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.
61 lines
1.9 KiB
C#
61 lines
1.9 KiB
C#
using AutoMapper;
|
|
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using DigitalData.Core.Exceptions;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using ReC.Application.Common.Dto;
|
|
using ReC.Domain.Views;
|
|
using System.Text.Json;
|
|
|
|
namespace ReC.Application.Results.Queries;
|
|
|
|
public record ReadResultViewQuery : IRequest<IEnumerable<ResultViewDto>>
|
|
{
|
|
public long? Id { get; init; } = null;
|
|
|
|
public long? ActionId { get; init; } = null;
|
|
|
|
public long? ProfileId { get; init; } = null;
|
|
|
|
public bool IncludeAction { get; init; } = true;
|
|
|
|
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 async Task<IEnumerable<ResultViewDto>> Handle(ReadResultViewQuery request, CancellationToken cancel)
|
|
{
|
|
var q = repo.Query;
|
|
|
|
if(request.Id is long id)
|
|
q = q.Where(rv => rv.Id == id);
|
|
|
|
if(request.ActionId is long actionId)
|
|
q = q.Where(rv => rv.ActionId == actionId);
|
|
|
|
if(request.ProfileId is long profileId)
|
|
q = q.Where(rv => rv.ProfileId == profileId);
|
|
|
|
if(request.IncludeAction)
|
|
q = q.Include(rv => rv.Action);
|
|
|
|
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);
|
|
|
|
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);
|
|
}
|
|
} |