Exemplo n.º 1
0
        public void AddToCart(Product product, int amount)
        {
            var shoppingCartItem =
                _appDatabaseContext.ShoppingCartItems.
                SingleOrDefault(
                    s => s.Product.ProductId == product.ProductId &&
                    s.ShoppingCartId == ShoppingCartId);

            if (shoppingCartItem == null)
            {
                shoppingCartItem = new ShoppingCartItem
                {
                    ShoppingCartId = ShoppingCartId,
                    Product        = product,
                    Amount         = 1
                };

                _appDatabaseContext.ShoppingCartItems.Add(shoppingCartItem);
            }
            else
            {
                shoppingCartItem.Amount++;
            }
            _appDatabaseContext.SaveChanges();
        }
        public void CreateOrder(Order order)
        {
            order.OrderPlaced = DateTime.Now;

            var shoppingCartItems
                = _shoppingCart.ShoppingCartItems;

            order.OrderTotal = _shoppingCart.GetShoppingCartTotal();

            order.OrderDetails = new List <OrderDetail>();
            //adding the order with its details

            foreach (var shoppingCartItem in shoppingCartItems)
            {
                var orderDetail = new OrderDetail
                {
                    Amount    = shoppingCartItem.Amount,
                    ProductId = shoppingCartItem.Product.ProductId,
                    Price     = shoppingCartItem.Product.Price
                };

                order.OrderDetails.Add(orderDetail);
            }

            _appDbContext.Orders.Add(order);

            _appDbContext.SaveChanges();
        }