Files
social-media/src/Application/Users/Queries/GetCurrentUser/GetCurrentUser.cs
2024-06-30 15:48:26 -04:00

54 lines
2.0 KiB
C#

using Hutopy.Application.Common.Interfaces;
namespace Hutopy.Application.Users.Queries.GetCurrentUser;
public record GetCurrentUserQuery : IRequest<UserDto>;
public class GetCurrentUserQueryHandler(
IApplicationDbContext context,
IMapper mapper,
IIdentityService identityService
)
: IRequestHandler<GetCurrentUserQuery, UserDto>
{
public async Task<UserDto> Handle(GetCurrentUserQuery request, CancellationToken cancellationToken)
{
var identityUser = await identityService.GetCurrentUserAsync();
var currentUserId = Guid.Parse(identityUser!.Id!);
var transactions = await context.UserTransactions
.Where(x => x.ApplicationUserId == currentUserId.ToString())
.OrderBy(x => x.LastModified)
.ProjectTo<UserTransactionDto>(mapper.ConfigurationProvider)
.Where(x => x.IsConfirmed == true)
.ToListAsync(cancellationToken);
var roles = await identityService.GetCurrentUserRolesAsync();
var user = new UserDto
{
Id = currentUserId,
FirstName = identityUser.FirstName ?? "",
LastName = identityUser.LastName ?? "",
UserName = identityUser.UserName ?? "",
Occupation = identityUser.Occupation ?? "",
PhoneNumber = identityUser.PhoneNumber ?? "",
Email = identityUser.Email ?? "",
BirthDate = identityUser.BirthDate ?? "",
Country = identityUser.Country ?? "",
City = identityUser.City ?? "",
Address = identityUser.Address ?? "",
About = identityUser.About ?? "",
Description = identityUser.Description ?? "",
SocialNetworks = identityUser.SocialNetworks,
ProfileColors = identityUser.ProfileColors,
StoredDataUrls = identityUser.StoredDataUrls,
UserTransactions = transactions,
TotalBalance = transactions.Sum(x => x.Amount),
UserRoles = roles,
};
return user;
}
}