Support querying multiple profile views in ReadProfileView
Refactored ReadProfileViewQuery and handler to return multiple profiles as IEnumerable<ProfileViewDto>. Made Id parameter optional to allow fetching all profiles. Updated exception handling for empty results.
This commit is contained in:
@@ -8,26 +8,29 @@ using ReC.Domain.Views;
|
||||
|
||||
namespace ReC.Application.Profile.Queries;
|
||||
|
||||
public record ReadProfileViewQuery : IRequest<ProfileViewDto>
|
||||
public record ReadProfileViewQuery : IRequest<IEnumerable<ProfileViewDto>>
|
||||
{
|
||||
public long Id { get; init; }
|
||||
public long? Id { get; init; } = null;
|
||||
|
||||
public bool IncludeActions { get; init; } = true;
|
||||
}
|
||||
|
||||
public class ReadProfileViewQueryHandler(IRepository<ProfileView> repo, IMapper mapper)
|
||||
: IRequestHandler<ReadProfileViewQuery, ProfileViewDto>
|
||||
: IRequestHandler<ReadProfileViewQuery, IEnumerable<ProfileViewDto>>
|
||||
{
|
||||
public async Task<ProfileViewDto> Handle(ReadProfileViewQuery request, CancellationToken cancel)
|
||||
public async Task<IEnumerable<ProfileViewDto>> Handle(ReadProfileViewQuery request, CancellationToken cancel)
|
||||
{
|
||||
var query = request.IncludeActions
|
||||
? repo.Query.Include(p => p.Actions)
|
||||
: repo.Query;
|
||||
|
||||
var profile = await query.SingleOrDefaultAsync(p => p.Id == request.Id, cancel);
|
||||
if (request.Id is long id)
|
||||
query = query.Where(p => p.Id == id);
|
||||
|
||||
return profile is null
|
||||
var profiles = await query.ToListAsync(cancel);
|
||||
|
||||
return profiles is null || profiles.Count == 0
|
||||
? throw new NotFoundException($"Profile {request.Id} not found.")
|
||||
: mapper.Map<ProfileViewDto>(profile);
|
||||
: mapper.Map<IEnumerable<ProfileViewDto>>(profiles);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user