using AutoMapper;
using Kean.Domain.Basic.Commands;
using Kean.Domain.Basic.Events;
using Kean.Domain.Basic.Repositories;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Kean.Domain.Basic.CommandHandlers
{
///
/// 删除用户命令处理程序
///
public sealed class DeleteUserCommandHandler : CommandHandler
{
private readonly ICommandBus _commandBus; // 命令总线
private readonly IMapper _mapper; // 模型映射
private readonly IUserRepository _userRepository; // 用户仓库
///
/// 依赖注入
///
public DeleteUserCommandHandler(
ICommandBus commandBus,
IMapper mapper,
IUserRepository userRepository)
{
_commandBus = commandBus;
_mapper = mapper;
_userRepository = userRepository;
}
///
/// 处理程序
///
public override async Task Handle(DeleteUserCommand command, CancellationToken cancellationToken)
{
if (command.ValidationResult.IsValid)
{
List result = new();
foreach (var item in command.Id)
{
await _userRepository.Delete(item);
result.Add(item);
}
command.Id = result;
await _commandBus.Trigger(_mapper.Map(command), cancellationToken);
}
else
{
await _commandBus.Notify(command.ValidationResult,
cancellationToken: cancellationToken);
}
}
}
}