using AutoMapper;
using Kean.Application.Command.Interfaces;
using Kean.Application.Command.ViewModels;
using Kean.Domain;
using Kean.Domain.Order.Commands;
using System.Linq;
using System.Threading.Tasks;
namespace Kean.Application.Command.Implements
{
///
/// 订单命令服务实现
///
public class OrderService : IOrderService
{
private readonly ICommandBus _bus; // 命令总线
private readonly IMapper _mapper; // 模型映射
private readonly INotification _notifications; // 总线通知
///
/// 依赖注入
///
public OrderService(
ICommandBus bus,
IMapper mapper,
INotification notifications)
{
_bus = bus;
_mapper = mapper;
_notifications = notifications;
}
/*
* 实现 Kean.Application.Command.Interfaces.IOrderService.CreateType 方法
*/
public async Task<(int Id, Failure Failure)> CreateType(Ordtyp type)
{
var command = _mapper.Map(type);
await _bus.Execute(command);
return (command.Id, _notifications.FirstOrDefault());
}
/*
* 实现 Kean.Application.Command.Interfaces.IOrderService.ModifyType 方法
*/
public async Task<(bool Success, Failure Failure)> ModifyType(Ordtyp type)
{
await _bus.Execute(_mapper.Map(type));
var failure = _notifications.FirstOrDefault();
return (failure == null, failure);
}
/*
* 实现 Kean.Application.Command.Interfaces.IOrderService.DeleteType 方法
*/
public async Task<(bool Success, Failure Failure)> DeleteType(int id)
{
await _bus.Execute(new DeleteTypeCommand { Id = id });
var failure = _notifications.FirstOrDefault();
return (failure == null, failure);
}
/*
* 实现 Kean.Application.Command.Interfaces.IOrderService.CreateFlow 方法
*/
public async Task<(int Id, Failure Failure)> CreateFlow(Flow flow)
{
var command = _mapper.Map(flow);
await _bus.Execute(command);
return (command.Id, _notifications.FirstOrDefault());
}
/*
* 实现 Kean.Application.Command.Interfaces.IOrderService.ModifyFlow 方法
*/
public async Task<(bool Success, Failure Failure)> ModifyFlow(Flow flow)
{
await _bus.Execute(_mapper.Map(flow));
var failure = _notifications.FirstOrDefault();
return (failure == null, failure);
}
/*
* 实现 Kean.Application.Command.Interfaces.IOrderService.DeleteFlow 方法
*/
public async Task<(bool Success, Failure Failure)> DeleteFlow(int id)
{
await _bus.Execute(new DeleteFlowCommand { Id = id });
var failure = _notifications.FirstOrDefault();
return (failure == null, failure);
}
/*
* 实现 Kean.Application.Command.Interfaces.IOrderService.CreateOrder 方法
*/
public async Task<(int Id, Failure Failure)> CreateOrder(Order order)
{
var command = _mapper.Map(order);
await _bus.Execute(command);
return (command.Id, _notifications.FirstOrDefault());
}
/*
* 实现 Kean.Application.Command.Interfaces.IOrderService.ModifyOrder 方法
*/
public async Task<(bool Success, Failure Failure)> ModifyOrder(Order order)
{
await _bus.Execute(_mapper.Map(order));
var failure = _notifications.FirstOrDefault();
return (failure == null, failure);
}
/*
* 实现 Kean.Application.Command.Interfaces.IOrderService.DeleteOrder 方法
*/
public async System.Threading.Tasks.Task DeleteOrder(int id, bool state)
{
await _bus.Execute(new DeleteOrderCommand
{
Id = id,
State = state
});
}
}
}