山东雷驰
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.
 
 
 
 

54 lines
2.0 KiB

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
{
/// <summary>
/// 创建物料命令处理程序
/// </summary>
public sealed class ModifyItemCommandHandler : CommandHandler<ModifyItemCommand>
{
private readonly ICommandBus _commandBus; // 命令总线
private readonly IMapper _mapper; // 模型映射
private readonly IInterfaceRecordRepository _interfaceRecordRepository;
/// <summary>
/// 依赖注入
/// </summary>
public ModifyItemCommandHandler(
ICommandBus commandBus,
IInterfaceRecordRepository interfaceRecordRepository,
IMapper mapper)
{
_commandBus = commandBus;
_mapper = mapper;
_interfaceRecordRepository = interfaceRecordRepository;
}
/// <summary>
/// 处理程序
/// </summary>
public override async Task Handle(ModifyItemCommand command, CancellationToken cancellationToken)
{
if (await _interfaceRecordRepository.IsItemListCodeExist(command.Code, command.ItemName))
{
await _commandBus.Notify(nameof(command.Code), "物料类型重复", command.Code, nameof(ErrorCode.Conflict),
cancellationToken: cancellationToken);
return;
}
if (await _interfaceRecordRepository.IsItemListNameExist(command.Name, command.Name))
{
await _commandBus.Notify(nameof(command.Name), "物料类型名称重复", command.Name, nameof(ErrorCode.Conflict),
cancellationToken: cancellationToken);
return;
}
await _interfaceRecordRepository.ModifyItem(_mapper.Map<Item>(command));
}
}
}