Files
ReC/src/ReC.Application/Profile/Queries/ReadProfileViewQuery.cs
TekH 7a4cdb3d1f 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.
2026-01-14 16:30:41 +01:00

36 lines
1.2 KiB
C#

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<IEnumerable<ProfileViewDto>>
{
public long? Id { get; init; } = null;
public bool IncludeActions { get; init; } = true;
}
public class ReadProfileViewQueryHandler(IRepository<ProfileView> repo, IMapper mapper)
: IRequestHandler<ReadProfileViewQuery, IEnumerable<ProfileViewDto>>
{
public async Task<IEnumerable<ProfileViewDto>> 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<IEnumerable<ProfileViewDto>>(profiles);
}
}