Updated `InvokeBatchRecActionsCommandExtensions` to filter actions with `Invoked = false` using a lambda in `ToReadQuery`. Refactored `ReadRecActionQueryBase` to remove the `Invoked` property and updated `ToReadQuery` to accept a delegate for external query modifications. Moved the `Invoked` property to `ReadRecActionQuery` and added a parameterless constructor. These changes improve flexibility and enable dynamic query customization.
48 lines
1.5 KiB
C#
48 lines
1.5 KiB
C#
using MediatR;
|
|
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using ReC.Domain.Entities;
|
|
using AutoMapper;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using DigitalData.Core.Exceptions;
|
|
using ReC.Application.Common.Dto;
|
|
|
|
namespace ReC.Application.RecActions.Queries;
|
|
|
|
public record ReadRecActionQueryBase
|
|
{
|
|
public long ProfileId { get; init; }
|
|
|
|
public ReadRecActionQuery ToReadQuery(Action<ReadRecActionQuery> modify)
|
|
{
|
|
ReadRecActionQuery query = new(this);
|
|
modify(query);
|
|
return query;
|
|
}
|
|
}
|
|
|
|
public record ReadRecActionQuery : ReadRecActionQueryBase, IRequest<IEnumerable<RecActionDto>>
|
|
{
|
|
public ReadRecActionQuery(ReadRecActionQueryBase root) : base(root) { }
|
|
|
|
public bool? Invoked { get; set; } = null;
|
|
|
|
public ReadRecActionQuery() { }
|
|
}
|
|
|
|
public class ReadRecActionQueryHandler(IRepository<RecActionView> repo, IMapper mapper) : IRequestHandler<ReadRecActionQuery, IEnumerable<RecActionDto>>
|
|
{
|
|
public async Task<IEnumerable<RecActionDto>> Handle(ReadRecActionQuery request, CancellationToken cancel)
|
|
{
|
|
var query = repo.Where(act => act.ProfileId == request.ProfileId);
|
|
|
|
if (request.Invoked is bool invoked)
|
|
query = invoked ? query.Where(act => act.Root!.OutRes != null) : query.Where(act => act.Root!.OutRes == null);
|
|
|
|
var actions = await query.ToListAsync(cancel);
|
|
|
|
if(actions.Count == 0)
|
|
throw new NotFoundException($"No actions found for the profile {request.ProfileId}.");
|
|
|
|
return mapper.Map<IEnumerable<RecActionDto>>(actions);
|
|
}
|
|
} |