using AutoMapper;
using Kean.Domain.Basic.Commands;
using Kean.Domain.Basic.Events;
using Kean.Domain.Basic.Models;
using Kean.Domain.Basic.Repositories;
using System.Threading;
using System.Threading.Tasks;
namespace Kean.Domain.Basic.CommandHandlers
{
///
/// 创建角色命令处理程序
///
public sealed class CreateRoleCommandHandler : CommandHandler
{
private readonly ICommandBus _commandBus; // 命令总线
private readonly IMapper _mapper; // 模型映射
private readonly IRoleRepository _roleRepository; // 角色仓库
///
/// 依赖注入
///
public CreateRoleCommandHandler(
ICommandBus commandBus,
IMapper mapper,
IRoleRepository roleRepository)
{
_commandBus = commandBus;
_mapper = mapper;
_roleRepository = roleRepository;
}
///
/// 处理程序
///
public override async Task Handle(CreateRoleCommand command, CancellationToken cancellationToken)
{
if (command.ValidationResult.IsValid)
{
if (await _roleRepository.IsExist(command.Name, null))
{
await _commandBus.Notify(nameof(command.Name), "角色名重复", command.Name, nameof(ErrorCode.Conflict),
cancellationToken: cancellationToken);
return;
}
Output(nameof(command.Id), await _roleRepository.Create(_mapper.Map(command)));
await _commandBus.Trigger(_mapper.Map(command), cancellationToken);
}
else
{
await _commandBus.Notify(command.ValidationResult,
cancellationToken: cancellationToken);
}
}
}
}