using AutoMapper; using Kean.Domain.Basic.Commands; using Kean.Domain.Basic.Events; using Kean.Domain.Basic.Repositories; using Kean.Domain.Basic.SharedServices.Proxies; using System.Threading; using System.Threading.Tasks; namespace Kean.Domain.Basic.CommandHandlers { /// /// 重置密码命令处理程序 /// public sealed class ResetPasswordCommandHandler : CommandHandler { private readonly ICommandBus _commandBus; // 命令总线 private readonly IMapper _mapper; // 模型映射 private readonly IUserRepository _userRepository; // 用户仓库 private readonly IdentityProxy _identityProxy;// 身份域代理 /// /// 依赖注入 /// public ResetPasswordCommandHandler( ICommandBus commandBus, IMapper mapper, IUserRepository userRepository, IdentityProxy identityProxy) { _commandBus = commandBus; _mapper = mapper; _userRepository = userRepository; _identityProxy = identityProxy; } /// /// 处理程序 /// public override async Task Handle(ResetPasswordCommand command, CancellationToken cancellationToken) { if (command.ValidationResult.IsValid) { if (!await _userRepository.IsExist(command.Id)) { await _commandBus.Notify(nameof(command.Id), "用户不存在", command.Id, nameof(ErrorCode.Gone), cancellationToken: cancellationToken); return; } await _userRepository.ResetPassword(command.Id, s => _identityProxy.EncodePassword(s)); await _commandBus.Trigger(_mapper.Map(command), cancellationToken); } else { await _commandBus.Notify(command.ValidationResult, cancellationToken: cancellationToken); } } } }