Exemple #1
0
        public void Handle(SellItemsRequest request)
        {
            var itemList = request.Items?.Select(MapOrderItem)
                           .ToList();

            var order = new Order(itemList);

            _orderRepository.Save(order);
        }
Exemple #2
0
        public void Handle(SellItemsRequest request)
        {
            var order = new Order
            {
                Status   = OrderStatus.CREATED,
                Items    = new List <OrderItem>(),
                Currency = "EUR",
                Total    = 0,
                Tax      = 0
            };

            foreach (var item in request.Items)
            {
                var product = _productCatalog.GetProductByName(item.ProductName);

                if (product == null)
                {
                    throw new UnknownProductException();
                }

                var unitaryTax = Math.Round(product.Price / 100 * product.Category.TaxPercentage, 2, MidpointRounding.AwayFromZero);
                var taxAmount  = Math.Round(unitaryTax * item.Quantity, 2, MidpointRounding.AwayFromZero);

                var unitaryTaxedAmount = product.Price + unitaryTax;
                var taxedAmount        = Math.Round(unitaryTaxedAmount * item.Quantity, 2, MidpointRounding.AwayFromZero);

                var orderItem = new OrderItem
                {
                    Product     = product,
                    Quantity    = item.Quantity,
                    Tax         = taxAmount,
                    TaxedAmount = taxedAmount
                };

                order.Items.Add(orderItem);
                order.Total += taxedAmount;
                order.Tax   += taxAmount;
            }

            _orderRepository.Save(order);
        }