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:
2026-01-14 16:30:41 +01:00
parent 453b6d1813
commit 7a4cdb3d1f

View File

@@ -8,26 +8,29 @@ using ReC.Domain.Views;
namespace ReC.Application.Profile.Queries; 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 bool IncludeActions { get; init; } = true;
} }
public class ReadProfileViewQueryHandler(IRepository<ProfileView> repo, IMapper mapper) 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 var query = request.IncludeActions
? repo.Query.Include(p => p.Actions) ? repo.Query.Include(p => p.Actions)
: repo.Query; : 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.") ? throw new NotFoundException($"Profile {request.Id} not found.")
: mapper.Map<ProfileViewDto>(profile); : mapper.Map<IEnumerable<ProfileViewDto>>(profiles);
} }
} }