42 lines
1.3 KiB
C#
42 lines
1.3 KiB
C#
using MediatR;
|
|
using Nashel.BuildingBlocks.Application.Abstractions;
|
|
using Nashel.Modules.Identity.Domain.Repositories;
|
|
|
|
namespace Nashel.Modules.Identity.Application.Queries.GetProfile;
|
|
|
|
public record GetProfileQuery : IRequest<ProfileResponse>;
|
|
|
|
public record ProfileResponse(Guid Id, string Phone, List<string> Roles);
|
|
|
|
public class GetProfileQueryHandler : IRequestHandler<GetProfileQuery, ProfileResponse>
|
|
{
|
|
private readonly IAccountRepository _accountRepository;
|
|
private readonly ICurrentUserService _currentUserService;
|
|
|
|
public GetProfileQueryHandler(IAccountRepository accountRepository, ICurrentUserService currentUserService)
|
|
{
|
|
_accountRepository = accountRepository;
|
|
_currentUserService = currentUserService;
|
|
}
|
|
|
|
public async Task<ProfileResponse> Handle(GetProfileQuery request, CancellationToken cancellationToken)
|
|
{
|
|
var userId = _currentUserService.UserId;
|
|
if (userId == null)
|
|
{
|
|
throw new UnauthorizedAccessException();
|
|
}
|
|
|
|
var account = await _accountRepository.GetByIdAsync(userId.Value, cancellationToken);
|
|
if (account == null)
|
|
{
|
|
throw new Exception("Account not found");
|
|
}
|
|
|
|
return new ProfileResponse(
|
|
account.Id,
|
|
account.Phone,
|
|
account.Roles.Select(r => r.ToString()).ToList());
|
|
}
|
|
}
|