0
0
mirror of https://github.com/alex289/CleanArchitecture.git synced 2025-06-30 18:42:56 +00:00
CleanArchitecture/CleanArchitecture.Domain/Commands/Users/ChangePassword/ChangePasswordCommandHandler.cs
2023-03-22 19:25:44 +01:00

71 lines
2.1 KiB
C#

using System.Threading;
using System.Threading.Tasks;
using CleanArchitecture.Domain.Errors;
using CleanArchitecture.Domain.Events.User;
using CleanArchitecture.Domain.Interfaces;
using CleanArchitecture.Domain.Interfaces.Repositories;
using CleanArchitecture.Domain.Notifications;
using MediatR;
using BC = BCrypt.Net.BCrypt;
namespace CleanArchitecture.Domain.Commands.Users.ChangePassword;
public sealed class ChangePasswordCommandHandler : CommandHandlerBase,
IRequestHandler<ChangePasswordCommand>
{
private readonly IUser _user;
private readonly IUserRepository _userRepository;
public ChangePasswordCommandHandler(
IMediatorHandler bus,
IUnitOfWork unitOfWork,
INotificationHandler<DomainNotification> notifications,
IUserRepository userRepository,
IUser user) : base(bus, unitOfWork, notifications)
{
_userRepository = userRepository;
_user = user;
}
public async Task Handle(ChangePasswordCommand request, CancellationToken cancellationToken)
{
if (!await TestValidityAsync(request))
{
return;
}
var user = await _userRepository.GetByIdAsync(_user.GetUserId());
if (user == null)
{
await NotifyAsync(
new DomainNotification(
request.MessageType,
$"There is no User with Id {_user.GetUserId()}",
ErrorCodes.ObjectNotFound));
return;
}
if (!BC.Verify(request.Password, user.Password))
{
await NotifyAsync(
new DomainNotification(
request.MessageType,
"The password is incorrect",
DomainErrorCodes.UserPasswordIncorrect));
return;
}
var passwordHash = BC.HashPassword(request.NewPassword);
user.SetPassword(passwordHash);
_userRepository.Update(user);
if (await CommitAsync())
{
await Bus.RaiseEventAsync(new PasswordChangedEvent(user.Id));
}
}
}