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 modify) { ReadRecActionQuery query = new(this); modify(query); return query; } } public record ReadRecActionQuery : ReadRecActionQueryBase, IRequest> { public ReadRecActionQuery(ReadRecActionQueryBase root) : base(root) { } public bool? Invoked { get; set; } = null; public ReadRecActionQuery() { } } public class ReadRecActionQueryHandler(IRepository repo, IMapper mapper) : IRequestHandler> { public async Task> 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>(actions); } }