Files
imprink/src/Imprink.Application/Users/Commands/GetUserRolesHandler.cs
2025-06-20 14:56:29 +03:00

21 lines
841 B
C#

using AutoMapper;
using Imprink.Application.Exceptions;
using Imprink.Application.Users.Dtos;
using MediatR;
namespace Imprink.Application.Users.Commands;
public record GetUserRolesCommand(string Sub) : IRequest<IEnumerable<RoleDto>>;
public class GetUserRolesHandler(IUnitOfWork uw, IMapper mapper): IRequestHandler<GetUserRolesCommand, IEnumerable<RoleDto>>
{
public async Task<IEnumerable<RoleDto>> Handle(GetUserRolesCommand request, CancellationToken cancellationToken)
{
if (!await uw.UserRepository.UserExistsAsync(request.Sub, cancellationToken))
throw new NotFoundException("User with ID: " + request.Sub + " does not exist.");
var roles = await uw.UserRoleRepository.GetUserRolesAsync(request.Sub, cancellationToken);
return mapper.Map<IEnumerable<RoleDto>>(roles);
}
}