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; namespace ReC.Application.Profile.Queries; public record ReadProfileViewQuery : IRequest> { public long? Id { get; init; } = null; public bool IncludeActions { get; init; } = true; } public class ReadProfileViewQueryHandler(IRepository repo, IMapper mapper) : IRequestHandler> { public async Task> Handle(ReadProfileViewQuery request, CancellationToken cancel) { var query = request.IncludeActions ? repo.Query.Include(p => p.Actions) : repo.Query; if (request.Id is long id) query = query.Where(p => p.Id == id); var profiles = await query.ToListAsync(cancel); return profiles is null || profiles.Count == 0 ? throw new NotFoundException($"Profile {request.Id} not found.") : mapper.Map>(profiles); } }