山东雷驰
 
 
 
 

124 lines
4.3 KiB

using AutoMapper;
using Kean.Application.Command.Interfaces;
using Kean.Application.Command.ViewModels;
using Kean.Domain;
using Kean.Domain.Material.Commands;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Kean.Application.Command.Implements
{
/// <summary>
/// 物料信息命令服务实现
/// </summary>
public class MaterialService : IMaterialService
{
private readonly ICommandBus _bus; // 命令总线
private readonly IMapper _mapper; // 模型映射
private readonly INotification _notifications; // 总线通知
/// <summary>
/// 依赖注入
/// </summary>
public MaterialService(
ICommandBus bus,
IMapper mapper,
INotification notifications)
{
_bus = bus;
_mapper = mapper;
_notifications = notifications;
}
/*
* 实现 Kean.Application.Command.Interfaces.IMaterialService.CreateMaterial 方法
*/
public async Task<(int Id, Failure Failure)> CreateMaterial(Material material)
{
var command = _mapper.Map<CreateMaterialCommand>(material);
await _bus.Execute(command);
return (command.Id, _notifications.FirstOrDefault());
}
/*
* 实现 Kean.Application.Command.Interfaces.IMaterialService.ModifyMaterial 方法
*/
public async Task<(bool Success, Failure Failure)> ModifyMaterial(Material material)
{
await _bus.Execute(_mapper.Map<ModifyMaterialCommand>(material));
var failure = _notifications.FirstOrDefault();
return (failure == null, failure);
}
/*
* 实现 Kean.Application.Command.Interfaces.IMaterialService.DeleteMaterial 方法
*/
public async Task<IEnumerable<int>> DeleteMaterial(IEnumerable<int> id)
{
var command = new DeleteMaterialCommand { Id = id };
await _bus.Execute(command);
return command.Id;
}
/*
* 实现 Kean.Application.Command.Interfaces.IMaterialService.CreateCategory 方法
*/
public async Task<(int Id, Failure Failure)> CreateCategory(Category category)
{
var command = _mapper.Map<CreateCategoryCommand>(category);
await _bus.Execute(command);
return (command.Id, _notifications.FirstOrDefault());
}
/*
* 实现 Kean.Application.Command.Interfaces.IMaterialService.ModifyCategory 方法
*/
public async Task<(bool Success, Failure Failure)> ModifyCategory(Category category)
{
await _bus.Execute(_mapper.Map<ModifyCategoryCommand>(category));
var failure = _notifications.FirstOrDefault();
return (failure == null, failure);
}
/*
* 实现 Kean.Application.Command.Interfaces.IMaterialService.DeleteCategory 方法
*/
public async Task<IEnumerable<int>> DeleteCategory(IEnumerable<int> id)
{
var command = new DeleteCategoryCommand { Id = id };
await _bus.Execute(command);
return command.Id;
}
/*
* 实现 Kean.Application.Command.Interfaces.IMaterialService.CreateSafety 方法
*/
public async Task<(int Id, Failure Failure)> CreateSafety(Safety safety)
{
var command = _mapper.Map<CreateSafetyCommand>(safety);
await _bus.Execute(command);
return (command.Id, _notifications.FirstOrDefault());
}
/*
* 实现 Kean.Application.Command.Interfaces.IMaterialService.ModifySafety 方法
*/
public async Task<(bool Success, Failure Failure)> ModifySafety(Safety safety)
{
await _bus.Execute(_mapper.Map<ModifySafetyCommand>(safety));
var failure = _notifications.FirstOrDefault();
return (failure == null, failure);
}
/*
* 实现 Kean.Application.Command.Interfaces.IMaterialService.DeleteSafety 方法
*/
public async Task<IEnumerable<int>> DeleteSafety(IEnumerable<int> id)
{
var command = new DeleteSafetyCommand { Id = id };
await _bus.Execute(command);
return command.Id;
}
}
}