示例#1
0
        public void AddToCart(Product product, int quantity)
        {
            var shoppingCartItem = _sqlDbProductsContext.ShoppinCartItems.SingleOrDefault(
                s => s.Product.ProductId == product.ProductId && s.ShoppingCartId == ShoppingCartId);

            if (shoppingCartItem == null)
            {
                shoppingCartItem = new ShoppinCartItem
                {
                    ShoppingCartId = ShoppingCartId,
                    Product        = product,
                    Quantity       = 1
                };

                _sqlDbProductsContext.ShoppinCartItems.Add(shoppingCartItem);
            }
            else
            {
                shoppingCartItem.Quantity++;
            }
            _sqlDbProductsContext.SaveChanges();
        }
示例#2
0
        public void CreateOrder(Order order)
        {
            _sqlDbProductsContext.Orders.Add(order);

            var shoppingCartItems = _shoppingCart.ShoppinCartItems;

            foreach (var item in shoppingCartItems)
            {
                var orderDetail = new OrderDetail()
                {
                    Quantity  = item.Quantity,
                    ProductId = item.Product.ProductId,
                    OrderId   = order.OrderId,
                    Price     = item.Product.Price
                };

                _sqlDbProductsContext.OrderDetails.Add(orderDetail);
            }
            _sqlDbProductsContext.SaveChanges();
        }