Add ReadProfileViewQuery and handler for profile views

Introduce CQRS/MediatR query and handler to fetch ProfileView by Id,
optionally including related Actions. Uses repository and AutoMapper,
throws NotFoundException if profile is missing, and returns ProfileViewDto.
This commit is contained in:
2026-01-14 16:12:54 +01:00
parent 849de7a204
commit bd2b5ff62f

View File

@@ -0,0 +1,33 @@
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<ProfileViewDto>
{
public long Id { get; init; }
public bool IncludeActions { get; init; } = true;
}
public class ReadProfileViewQueryHandler(IRepository<ProfileView> repo, IMapper mapper)
: IRequestHandler<ReadProfileViewQuery, ProfileViewDto>
{
public async Task<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);
return profile is null
? throw new NotFoundException($"Profile {request.Id} not found.")
: mapper.Map<ProfileViewDto>(profile);
}
}