예제 #1
0
파일: Shop.cs 프로젝트: bantaba/BedRockShop
 // public static OrderHeader CreateOrder
 public static void AddProductToCart(int productId, int customerId, int quantity)
  {
      using (var db = new ShopModel())
      {
          //LING (language integrated Query) & LAMBDA
          var cart = db.ShoppingCarts.Where(s => s.CustomerId == customerId).FirstOrDefault();
          if (cart == null)
          {
              //did not find a cart - create one
              cart = new ShoppingCart
              {
                  CustomerId = customerId
              };
              db.ShoppingCarts.Add(cart);
          }
          var product = db.Products.Where(p => p.ProductId == productId).FirstOrDefault();
          if (product != null)
          {
              var detailEntry = new ShoppingCartDetail
              {
                  ShoppingCartId = cart.ShoppingCartId,
                  ProductId = product.ProductId,
                  Quantity = quantity,
                  SubTotal = quantity * product.Price
              };
              db.ShoppingCartDetails.Add(detailEntry);                 
          }
          db.SaveChanges();
      }
  }
예제 #2
0
        public static void AddProductToCart(int productID, int customerID, int quantity)
        {
            using (var db = new ShopModel())
            {
                //LINQ & Lambda
               var cart =
                    db.ShoppingCarts.Where(s => s.CustomerID == customerID).FirstOrDefault();
                if (cart == null)
                {
                    // Did not find a cart
                    cart = new ShoppingCart
                    {
                        CustomerID = customerID
                    };
                    db.ShoppingCarts.Add(cart);
                }
                var product = db.Products.Where(p => p.ProductID == productID).FirstOrDefault();
                if (product != null)
                {
                    var detailEntry = new ShoppingCartDetail
                    {
                        ShoppingCartID = cart.ShoppingCartID,
                        ProductId = product.ProductID,
                        Quantity = quantity,
                        SubTotal = quantity * product.Price
                    };
                    db.ShoppingCartDetails.Add(detailEntry);
                }

                db.SaveChanges();
            }
        }