public void Dispose()
 {
     if (_db != null)
     {
         _db.Dispose();
         _db = null;
     }
 }
Esempio n. 2
0
        public bool AddProduct(string ProductName, string ProductDesc, string ProductPrice, string ProductCategory, string ProductImagePath)
        {
            var myProduct = new Product();
            myProduct.ProductName = ProductName;
            myProduct.Description = ProductDesc;
            myProduct.UnitPrice = Convert.ToDouble(ProductPrice);
            myProduct.ImagePath = ProductImagePath;
            myProduct.CategoryID = Convert.ToInt32(ProductCategory);

            using (ProductContext _db = new ProductContext())
            {
                // Add product to DB.
                _db.Products.Add(myProduct);
                _db.SaveChanges();
            }
            // Success.
            return true;
        }
Esempio n. 3
0
        public IQueryable<Product> GetProducts(
                        [QueryString("id")] int? categoryId,
                        [RouteData] string categoryName)
        {
            var _db = new ClusterCart.Models.ProductContext();
            IQueryable<Product> query = _db.Products;

            if (categoryId.HasValue && categoryId > 0)
            {
                query = query.Where(p => p.CategoryID == categoryId);
            }

            if (!String.IsNullOrEmpty(categoryName))
            {
                query = query.Where(p =>
                                    String.Compare(p.Category.CategoryName,
                                    categoryName) == 0);
            }
            return query;
        }
 public IQueryable<Product> GetProduct(
                 [QueryString("ProductID")] int? productId,
                 [RouteData] string productName)
 {
     var _db = new ClusterCart.Models.ProductContext();
     IQueryable<Product> query = _db.Products;
     if (productId.HasValue && productId > 0)
     {
         query = query.Where(p => p.ProductID == productId);
     }
     else if (!String.IsNullOrEmpty(productName))
     {
         query = query.Where(p =>
                   String.Compare(p.ProductName, productName) == 0);
     }
     else
     {
         query = null;
     }
     return query;
 }
 public void UpdateShoppingCartDatabase(String cartId, ShoppingCartUpdates[] CartItemUpdates)
 {
     using (var db = new ClusterCart.Models.ProductContext())
     {
         try
         {
             int CartItemCount = CartItemUpdates.Count();
             List<CartItem> myCart = GetCartItems();
             foreach (var cartItem in myCart)
             {
                 // Iterate through all rows within shopping cart list
                 for (int i = 0; i < CartItemCount; i++)
                 {
                     if (cartItem.Product.ProductID == CartItemUpdates[i].ProductId)
                     {
                         if (CartItemUpdates[i].PurchaseQuantity < 1 || CartItemUpdates[i].RemoveItem == true)
                         {
                             RemoveItem(cartId, cartItem.ProductId);
                         }
                         else
                         {
                             UpdateItem(cartId, cartItem.ProductId, CartItemUpdates[i].PurchaseQuantity);
                         }
                     }
                 }
             }
         }
         catch (Exception exp)
         {
             throw new Exception("ERROR: Unable to Update Cart Database - " + exp.Message.ToString(), exp);
         }
     }
 }
 public void UpdateItem(string updateCartID, int updateProductID, int quantity)
 {
     using (var _db = new ClusterCart.Models.ProductContext())
     {
         try
         {
             var myItem = (from c in _db.ShoppingCartItems where c.CartId == updateCartID && c.Product.ProductID == updateProductID select c).FirstOrDefault();
             if (myItem != null)
             {
                 myItem.Quantity = quantity;
                 _db.SaveChanges();
             }
         }
         catch (Exception exp)
         {
             throw new Exception("ERROR: Unable to Update Cart Item - " + exp.Message.ToString(), exp);
         }
     }
 }
 public void RemoveItem(string removeCartID, int removeProductID)
 {
     using (var _db = new ClusterCart.Models.ProductContext())
     {
         try
         {
             var myItem = (from c in _db.ShoppingCartItems where c.CartId == removeCartID && c.Product.ProductID == removeProductID select c).FirstOrDefault();
             if (myItem != null)
             {
                 // Remove Item.
                 _db.ShoppingCartItems.Remove(myItem);
                 _db.SaveChanges();
             }
         }
         catch (Exception exp)
         {
             throw new Exception("ERROR: Unable to Remove Cart Item - " + exp.Message.ToString(), exp);
         }
     }
 }
Esempio n. 8
0
 public IQueryable<Category> GetCategories()
 {
     var _db = new ClusterCart.Models.ProductContext();
     IQueryable<Category> query = _db.Categories;
     return query;
 }