public ICommandResult Handle(CreateOrderCommand command) { command.Validate(); if (command.Invalid) { return(new GenericCommandResult(false, "Pedido inválido", command.Notifications)); } var customer = _customerRepository.Get(command.Customer); var deliveryFree = _deliveryFreeRepository.Get(command.ZipCode); var discount = _discountRepository.Get(command.PromoCode); //Gerar o pedido var products = _productRepository.Get(ExtractGuids.Extract(command.Items)).ToList(); var order = new Order(customer, deliveryFree, discount); foreach (var item in command.Items) { var product = products.Where(x => x.Id == item.Product).FirstOrDefault(); order.AddItem(product, item.Quantity); } // 5. Agrupa as notificações AddNotifications(order.Notifications); if (Invalid) { return(new GenericCommandResult(false, "Falha ao gerar o pedido", Notifications)); } _orderRepository.Save(order); return(new GenericCommandResult(true, $"Pedido{order.Number} gerado com sucesso", order)); }
public ICommandResult Handle(CreateOrderCommand command) { // 0. Fail, Fast, Validation command.Validate(); if (command.Invalid) { return(new GenericCommandResult(false, "Pedido Inválido", command.Notifications)); } // 1. Recuperar o cliente var customer = _customerRepository.Get(command.Customer); // 2. Calcular a taxa de entrega var deliveryFree = _deliveryFreeRepository.Get(command.ZipCode); // 3. Obtém cupom de desconto var discount = _discountRepository.Get(command.PromoCode); // 4. Gerar o pedido var products = _productRepository.Get(ExtractGuids.Extract(command.Items)).ToList(); var order = new Order(customer, deliveryFree, discount); foreach (var item in command.Items) { var product = products.Where(x => x.Id == item.Product).FirstOrDefault(); order.AddItem(product, item.Quantity); } // 5. Agrupar as notificações AddNotifications(order.Notifications); // 6. Verifica se deu tudo certo if (Invalid) { return(new GenericCommandResult(false, "Falha oa gerar pedido", Notifications)); } // 7. Retorna o resultado _orderRepository.Save(order); return(new GenericCommandResult(true, $"Pedido {order.Number} gerado com sucesso", order)); }