コード例 #1
0
 public void Dispose()
 {
     if (_db != null)
     {
         _db.Dispose();
         _db = null;
     }
 }
コード例 #2
0
 public IQueryable<Gadget> GetGadget([QueryString("gadgetID")] int? gadgetId)
 {
     var _db = new UsedGadgetsSale.Models.GadgetContext();
     IQueryable<Gadget> query = _db.Gadgets;
     if (gadgetId.HasValue && gadgetId > 0)
     {
         query = query.Where(p => p.GadgetID == gadgetId);
     }
     else
     {
         query = null;
     }
     return query;
 }
コード例 #3
0
 public IQueryable<Category> GetCategories()
 {
     var _db = new UsedGadgetsSale.Models.GadgetContext();
     IQueryable<Category> query = _db.Categories;
     return query;
 }
コード例 #4
0
 public void UpdateShoppingCartDatabase(String cartId, ShoppingCartUpdates[] CartItemUpdates)
 {
     using (var db = new UsedGadgetsSale.Models.GadgetContext())
     {
         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.Gadget.GadgetID == CartItemUpdates[i].GadgetId)
                     {
                         if (CartItemUpdates[i].PurchaseQuantity < 1 || CartItemUpdates[i].RemoveItem == true)
                         {
                             RemoveItem(cartId, cartItem.GadgetId);
                         }
                         else
                         {
                             UpdateItem(cartId, cartItem.GadgetId, CartItemUpdates[i].PurchaseQuantity);
                         }
                     }
                 }
             }
         }
         catch (Exception exp)
         {
             throw new Exception("ERROR: Unable to Update Cart Database - " + exp.Message.ToString(), exp);
         }
     }
 }
コード例 #5
0
 public void UpdateItem(string updateCartID, int updateProductID, int quantity)
 {
     using (var _db = new UsedGadgetsSale.Models.GadgetContext())
     {
         try
         {
             var myItem = (from c in _db.ShoppingCartItems where c.CartId == updateCartID && c.Gadget.GadgetID == 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);
         }
     }
 }
コード例 #6
0
 public void RemoveItem(string removeCartID, int removeProductID)
 {
     using (var _db = new UsedGadgetsSale.Models.GadgetContext())
     {
         try
         {
             var myItem = (from c in _db.ShoppingCartItems where c.CartId == removeCartID && c.Gadget.GadgetID == 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);
         }
     }
 }