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:
33
src/ReC.Application/Profile/Queries/ReadProfileViewQuery.cs
Normal file
33
src/ReC.Application/Profile/Queries/ReadProfileViewQuery.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user