Пример #1
0
        public OrderDto Create(CreateOrderInput input)
        {
            if (input == null)
            {
                throw new OrderNotFoundException();
            }
            var orderItemList = new List <OrderItemDto>();

            foreach (var item in input.OrderItems)
            {
                var product = productAppService.GetAllProducts().FirstOrDefault(i => i.Id == item.SelectedProductId);
                item.UnitPrice = product.ProductUnitPrice;
                item.SubTotal  = product.ProductUnitPrice * item.Quantity;
                item.Product   = product;
                orderItemList.Add(mapper.Map <OrderItemDto>(item));
            }

            using (var unitOfWork = unitOfWorkManager.Begin())
            {
                try
                {
                    //Create order
                    var model = new Order(input.OrderTotal, input.CustomerId, mapper.Map <List <OrderItem> >(orderItemList));
                    orderRepository.Insert(model);
                    //Update the qty in stock
                    productAppService.UpdateProductQuantityInStock(orderItemList);

                    unitOfWork.Complete();
                    return(mapper.Map <OrderDto>(model));
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }