Refactor RecAction query to use ReadRecActionViewQuery
Replaced ReadRecActionQuery and its handler with ReadRecActionViewQuery and ReadRecActionViewQueryHandler. Updated controller methods to use the new query type. This change clarifies and consolidates RecAction view query logic, improving code organization and domain alignment.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
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.RecActionViews.Queries;
|
||||
|
||||
public record ReadRecActionQueryBase
|
||||
{
|
||||
public long ProfileId { get; init; }
|
||||
|
||||
public ReadRecActionViewQuery ToReadQuery(Action<ReadRecActionViewQuery> modify)
|
||||
{
|
||||
ReadRecActionViewQuery query = new(this);
|
||||
modify(query);
|
||||
return query;
|
||||
}
|
||||
}
|
||||
|
||||
public record ReadRecActionViewQuery : ReadRecActionQueryBase, IRequest<IEnumerable<RecActionDto>>
|
||||
{
|
||||
public ReadRecActionViewQuery(ReadRecActionQueryBase root) : base(root) { }
|
||||
|
||||
public bool? Invoked { get; set; } = null;
|
||||
|
||||
public ReadRecActionViewQuery() { }
|
||||
}
|
||||
|
||||
public class ReadRecActionViewQueryHandler(IRepository<RecActionView> repo, IMapper mapper) : IRequestHandler<ReadRecActionViewQuery, IEnumerable<RecActionDto>>
|
||||
{
|
||||
public async Task<IEnumerable<RecActionDto>> Handle(ReadRecActionViewQuery 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.Include(act => act.EndpointAuth).ToListAsync(cancel);
|
||||
|
||||
if (actions.Count == 0)
|
||||
throw new NotFoundException($"No actions found for the profile {request.ProfileId}.");
|
||||
|
||||
return mapper.Map<IEnumerable<RecActionDto>>(actions);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user