Beispiel #1
0
        private void CreateOrderItem(OrderEntity newOrder, OrderModel model)
        {
            var newOrderItems = new List <OrderItemEntity>();

            var groupOrderItemModels = model.Items
                                       .GroupBy(x => x.ProductId)
                                       .Select(gr => new OrderItemModel
            {
                ProductId = gr.Key,
                Amount    = gr.Sum(x => x.Amount)
            });

            foreach (var orderItem in groupOrderItemModels)
            {
                var newOrderItem = orderItem.Adapt <OrderItemEntity>();
                newOrderItem.OrderId = newOrder.OrderId;
                newOrderItem.Order   = newOrder;

                newOrderItem.Product = productRepository
                                       .GetById(orderItem.ProductId);
                if (newOrderItem.Product == null)
                {
                    throw new ValidatorException("Unkonwn product");
                }

                newOrderItem.UnitPrice = newOrderItem.Product.Price;

                orderItemValidator.Validate(newOrderItem);

                newOrderItem.Product.Amount -= newOrderItem.Amount;

                newOrderItems.Add(newOrderItem);
            }

            orderItemRepository.AddBulk(newOrderItems);
        }