山东雷驰
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.0 KiB

3 months ago
using AutoMapper;
using Kean.Domain.Order.Commands;
using Kean.Domain.Order.Events;
using Kean.Domain.Order.Models;
using Kean.Domain.Order.Repositories;
using System.Threading;
using System.Threading.Tasks;
namespace Kean.Domain.Order.CommandHandlers
{
/// <summary>
/// 创建工作流命令处理程序
/// </summary>
public sealed class CreateFlowCommandHandler : CommandHandler<CreateFlowCommand>
{
private readonly ICommandBus _commandBus; // 命令总线
private readonly IMapper _mapper; // 模型映射
private readonly IOrderRepository _orderRepository; // 订单仓库
/// <summary>
/// 依赖注入
/// </summary>
public CreateFlowCommandHandler(
ICommandBus commandBus,
IMapper mapper,
IOrderRepository orderRepository)
{
_commandBus = commandBus;
_mapper = mapper;
_orderRepository = orderRepository;
}
/// <summary>
/// 处理程序
/// </summary>
public override async Task Handle(CreateFlowCommand command, CancellationToken cancellationToken)
{
if (command.ValidationResult.IsValid)
{
if (await _orderRepository.IsFlowNameExist(command.Name, null))
{
await _commandBus.Notify(nameof(command.Name), "工作流描述已存在", command.Name, nameof(ErrorCode.Conflict),
cancellationToken: cancellationToken);
return;
}
Output(nameof(command.Id), await _orderRepository.CreateFlow(_mapper.Map<Flow>(command)));
await _commandBus.Trigger(_mapper.Map<CreateFlowSuccessEvent>(command), cancellationToken);
}
else
{
await _commandBus.Notify(command.ValidationResult,
cancellationToken: cancellationToken);
}
}
}
}