private ProductSubcategory GetProductSubcategoryById(int subcategoryId)
 {
     using (var db = new AdventureWorks2008R2Entities())
     {
         return db.ProductSubcategories.Single<ProductSubcategory>(cat => cat.ProductSubcategoryID == subcategoryId);
     }
 }
        public JsonResult GetCartItems()
        {
            using (var db = new AdventureWorks2008R2Entities())
            {
                var cartItems =
                    GetCartItemsByID(this.HttpContext.User.Identity.Name, db).AsEnumerable();

                var dataRows = (cartItems.Select(cartItem => new
                    {
                        product = new
                            {
                                Id = cartItem.ProductID,
                                cartItem.Product.Name,
                                LargeUrl =
                                                                 VirtualPathUtility.ToAbsolute(
                                                                     "~/Image/ProductThumbnail?productPhotoID=" +
                                                                     cartItem.Product.ProductProductPhotoes
                                                                             .FirstOrDefault<ProductProductPhoto>()
                                                                             .ProductPhotoID)
                            },
                        date = cartItem.DateCreated,
                        quantity = cartItem.Quantity
                    })).ToArray();

                return Json(dataRows, JsonRequestBehavior.AllowGet);
            }
        }
 private IList<ProductCategory> GetProductCategories()
 {
     using (var db = new AdventureWorks2008R2Entities())
     {
         return db.ProductCategories.Include("ProductSubcategories").ToList();
     }
 }
        public void AddToCart(string shoppingCartID, int productID, int quantity)
        {
            using (var db = new AdventureWorks2008R2Entities())
            {
                ShoppingCartItem myItem =
                    (from c in db.ShoppingCartItems
                     where c.ShoppingCartID == shoppingCartID && c.ProductID == productID
                     select c).FirstOrDefault();
                if (myItem == null)
                {
                    var cartadd = new ShoppingCartItem
                        {
                            ShoppingCartID = shoppingCartID,
                            Quantity = quantity,
                            ProductID = productID,
                            DateCreated = DateTime.Now,
                            ModifiedDate = DateTime.Now
                        };
                    db.ShoppingCartItems.AddObject(cartadd);
                }
                else
                {
                    myItem.Quantity += quantity;
                }

                db.SaveChanges();
            }
        }
Beispiel #5
0
 public void CreateProductInDatabase(Product product)
 {
     using (var db = new AdventureWorks2008R2Entities())
     {
         db.Products.AddObject(product);
         db.SaveChanges();
     }
 }
        public IQueryable<ShoppingCartItem> GetCartItemsByID(string shoppingCartID, AdventureWorks2008R2Entities db)
        {
            var cartItems = from cart in db.ShoppingCartItems
                                where cart.ShoppingCartID == shoppingCartID
                                select cart;

                return cartItems;
        }
Beispiel #7
0
 public IQueryable<Product> GetTop100Products()
 {
     using (var db = new AdventureWorks2008R2Entities())
     {
         return db.Products.Top("100");
     }
 }
        //
        // GET: /ShoppingCart/
        public ActionResult Index()
        {
            using (var db = new AdventureWorks2008R2Entities())
            {
                var cartItems =
                    GetCartItemsByID(this.HttpContext.User.Identity.Name, db).AsEnumerable();
                ViewBag.CartTotal = GetTotal(this.HttpContext.User.Identity.Name);

                return View(cartItems);
            }
        }
 public Product GetProductById(int productID)
 {
     using (var db = new AdventureWorks2008R2Entities())
     {
         Product product = db.Products.Where<Product>(p => p.ProductID == productID).FirstOrDefault<Product>();
         return product;
     }
 }
        private int RemoveFromCart(string shoppingCartId, int cartItemId)
        {
            using (var db = new AdventureWorks2008R2Entities())
            {
                int itemCount = 0;
                //Get the cart
                var cartItem = db.ShoppingCartItems.Single(
                    cart => cart.ShoppingCartID == shoppingCartId
                            && cart.ShoppingCartItemID == cartItemId);

                if (cartItem != null)
                {
                    if (cartItem.Quantity > 1)
                    {
                        cartItem.Quantity--;
                        itemCount = cartItem.Quantity;
                    }
                    else
                    {
                        db.ShoppingCartItems.DeleteObject(cartItem);
                    }
                    db.SaveChanges();
                }

                return itemCount;
            }
        }
        private decimal GetTotal(string shoppingCartID)
        {
            using (var db = new AdventureWorks2008R2Entities())
            {
                decimal? total =
                (from cartItems in db.ShoppingCartItems
                 where cartItems.ShoppingCartID == shoppingCartID
                 select (int?)cartItems.Quantity * cartItems.Product.ListPrice)
                .Sum();

                return total ?? decimal.Zero;
            }
        }
        private int GetCount(string shoppingCartID)
        {
            using (var db = new AdventureWorks2008R2Entities())
            {
                int? count = (from cartItems in db.ShoppingCartItems
                              where cartItems.ShoppingCartID == shoppingCartID
                              select (int?) cartItems.Quantity).Sum();

                return count ?? 0;
            }
        }