Refactor UpdateUserCommandHandler and add authorization to VerifyOtpToken method

This commit is contained in:
masoodafar-web
2025-11-14 08:13:58 +03:30
parent 2a254b9043
commit b677aff165
2 changed files with 46 additions and 45 deletions

View File

@@ -13,53 +13,52 @@ public class UpdateUserCommandHandler : IRequestHandler<UpdateUserCommand, Unit>
public async Task<Unit> Handle(UpdateUserCommand request, CancellationToken cancellationToken)
{
var updatingUserRequest = new UpdateUserRequest();
var existUser = await _context.User.GetUserAsync(new GetUserRequest()
{
Id = request.Id
}, cancellationToken: cancellationToken);
if (existUser == null)
throw new NotFoundException("User not found");
if (!string.IsNullOrEmpty(request.FirstName))
{
updatingUserRequest.FirstName = request.FirstName;
}
if (!string.IsNullOrEmpty(request.LastName))
{
updatingUserRequest.LastName = request.LastName;
}
if (!string.IsNullOrEmpty(request.NationalCode))
{
updatingUserRequest.NationalCode = request.NationalCode;
}
if (request.BirthDate.HasValue)
{
updatingUserRequest.BirthDate = Timestamp.FromDateTime(DateTime.SpecifyKind(request.BirthDate.Value, DateTimeKind.Utc));;
}
if (!string.IsNullOrEmpty(request.AvatarPath))
{
updatingUserRequest.AvatarPath = request.AvatarPath;
}
// if (request.AvatarFile!= null)
// var updatingUserRequest = new UpdateUserRequest();
// var existUser = await _context.User.GetUserAsync(new GetUserRequest()
// {
// Id = request.Id
// }, cancellationToken: cancellationToken);
// if (existUser == null)
// throw new NotFoundException("User not found");
//
// if (!string.IsNullOrEmpty(request.FirstName))
// {
// updatingUserRequest.FirstName = request.FirstName;
// }
//
// if (!string.IsNullOrEmpty(request.LastName))
// {
// updatingUserRequest.LastName = request.LastName;
// }
// if (!string.IsNullOrEmpty(request.NationalCode))
// {
// updatingUserRequest.NationalCode = request.NationalCode;
// }
// if (request.BirthDate.HasValue)
// {
// updatingUserRequest.BirthDate = Timestamp.FromDateTime(DateTime.SpecifyKind(request.BirthDate.Value, DateTimeKind.Utc));;
// }
// if (!string.IsNullOrEmpty(request.AvatarPath))
// {
// updatingUserRequest.AvatarPath = request.AvatarPath;
// }
// // if (request.AvatarFile!= null)
// // {
// // }
// if (request.PushNotifications!=existUser.PushNotifications)
// {
// updatingUserRequest.PushNotifications = request.PushNotifications;
// }
//
// if (request.EmailNotifications!=existUser.EmailNotifications)
// {
// updatingUserRequest.EmailNotifications = request.EmailNotifications;
// }
//
// if (request.SmsNotifications!=existUser.SmsNotifications)
// {
// updatingUserRequest.SmsNotifications = request.SmsNotifications;
// }
if (request.PushNotifications!=existUser.PushNotifications)
{
updatingUserRequest.PushNotifications = request.PushNotifications;
}
if (request.EmailNotifications!=existUser.EmailNotifications)
{
updatingUserRequest.EmailNotifications = request.EmailNotifications;
}
if (request.SmsNotifications!=existUser.SmsNotifications)
{
updatingUserRequest.SmsNotifications = request.SmsNotifications;
}
await _context.User.UpdateUserAsync(request: request.Adapt<UpdateUserRequest>(),
cancellationToken: cancellationToken);

View File

@@ -39,10 +39,12 @@ public class UserService : UserContract.UserContractBase
{
return await _dispatchRequestToCQRS.Handle<CreateNewOtpTokenRequest, CreateNewOtpTokenCommand, CreateNewOtpTokenResponse>(request, context);
}
[Authorize(Roles = "user")]
public override async Task<VerifyOtpTokenResponse> VerifyOtpToken(VerifyOtpTokenRequest request, ServerCallContext context)
{
return await _dispatchRequestToCQRS.Handle<VerifyOtpTokenRequest, VerifyOtpTokenCommand, VerifyOtpTokenResponse>(request, context);
}
[Authorize(Roles = "user")]
public override async Task<AdminGetJwtTokenResponse> AdminGetJwtToken(AdminGetJwtTokenRequest request, ServerCallContext context)
{
return await _dispatchRequestToCQRS.Handle<AdminGetJwtTokenRequest, AdminGetJwtTokenQuery, AdminGetJwtTokenResponse>(request, context);