Exemple #1
0
        public OrderEventTests()
        {
            var _productFakeRepository = new ProductFakeRepository();

            _ids = _productFakeRepository.Take(3).Select(x => x.Id).ToArray();

            _customerRepository = new CustomerFakeRepository();
            _productRepository  = _productFakeRepository;

            // data fake
            // vos
            var document   = new Document("64074577003");
            var creditCard = new CreditCard("5361004355915434", 503, "16/06/2020", "Nicolas S. dos Santos");

            // models
            _customer = _customerRepository.GetByDocument(document);
            _products = _productRepository.GetById(_ids);
            _order    = Order.Factory.Create(_customer, creditCard);
        }
        public Task <bool> Handle(PlaceOrderCommand message, CancellationToken cancellationToken)
        {
            if (!message.IsValid())
            {
                NotifyValidationErrors(message);
                return(Task.FromResult(false));
            }

            // search the clinte on database
            var customer = _customerRepository.GetById(message.CustomerId);

            if (customer == null)
            {
                NotifyValidationError(new DomainNotification(message.MessageType, $"Cliente com o id {message.CustomerId} não foi localizado"));
                return(Task.FromResult(false));
            }

            // get range of products
            var products = _productRepository.GetById(message.OrderItems.Select(x => x.Product).ToArray())?.ToList();

            if (products == null || products.Count == 0)
            {
                NotifyValidationError(new DomainNotification(message.MessageType, $"nenhum produto foi encontrado"));
                return(Task.FromResult(false));
            }

            // create an order
            var order = Order.Factory.Create(customer, new CreditCard(message.CreditCard.Number, message.CreditCard.Cvv, message.CreditCard.Validate, message.CreditCard.PrintName));

            // after create then validate
            if (!order.IsValid())
            {
                NotifyValidationErrors(order);
                return(Task.FromResult(false));
            }

            // verify if some product wasn't found
            var ids = products.Select(x => x.Id).ToArray();

            message.OrderItems.ToList().ForEach((x) =>
            {
                if (ids.Contains(x.Product))
                {
                    var product = products.Where(y => y.Id == x.Product)?.First();
                    order.AddItem(product, x.Quantity);
                }
                else
                {
                    NotifyValidationError(new DomainNotification(message.MessageType, $"produto {x.Product} não foi encontrado"));
                }
            });

            // apply discount if has in command
            if (message.DiscountCupon != null)
            {
                order = _cupomService.CalcDiscount(message.DiscountCupon, order);
            }

            // place order
            order.Place();

            // if has notifications then notify the application
            if (order.HasNotifications)
            {
                DisparchNotifications(order);
            }

            // save a order
            _orderRepository.Add(order);

            // if already it´s ok then disparch all events
            if (Commit())
            {
                DisparchEvents(order.DomainEvents);
                return(Task.FromResult(true));
            }

            NotifyValidationError(new DomainNotification(message.MessageType, "houve algum problema ao criar o pedido"));
            return(Task.FromResult(false));
        }