Example #1
0
        public Order CreateOrder(IUnitOfWork unitOfWork, ICartService cartService, IOrderService orderService, IOrderDetailService orderDetailService,
            IProductService productService, ICategoryService categoryService, Order order)
        {
            decimal orderTotal = 0;
            var cartItems = GetCartItems(cartService, productService, categoryService);
            foreach (var item in cartItems)
            {
                var orderDetail = new OrderDetail
                {
                    ProductId = item.ProductId,
                    OrderId = order.Id,
                    UnitPrice = item.Product.Price,
                    Quantity = item.Count
                };
                orderTotal += (item.Count * item.Product.Price);
                orderDetailService.Insert(orderDetail);
            }
            // update total into order
            order.Total = orderTotal;
            orderService.Update(order);

            // commit to changes
            unitOfWork.SaveChanges();

            //emty cart session
            EmptyCart(unitOfWork, cartService);

            return order;
        }