public Product FindById(int id)
 {
     using (var context = new ShoppingWebDBEntities())
     {
         return context.Products.SingleOrDefault(p => p.ProductId == id);
     }
 }
 public IEnumerable<Product> AllProducts()
 {
     using (var context = new ShoppingWebDBEntities())
     {
         return context.Products.ToArray();
     }
 }
示例#3
0
 public void AddNewUser(User user)
 {
     using (var context = new ShoppingWebDBEntities())
     {
         context.AddToUsers(user);
         context.SaveChanges();
     }
 }
示例#4
0
        public User FindByUserName(string userName)
        {
            if (userName == null)
                throw new NullReferenceException();

            using (var context = new ShoppingWebDBEntities())
            {
                return context.Users.SingleOrDefault(u => u.UserName == userName);
            }
        }
示例#5
0
 public void AppendPoint(string userName, int point)
 {
     using (var context = new ShoppingWebDBEntities())
     {
         var user = FindByUserName(userName);
         if (user == null) return;
         user.Point += point;
         context.Update(user);
         context.SaveChanges();
     }
 }
示例#6
0
        public int AddToCart(int productId)
        {
            using (var context = new ShoppingWebDBEntities())
            {

                var product = context.Products.Single(p => p.ProductId == productId);
                var cart = GetCartFromSession();
                if (cart.ContainsKey(productId)) cart[productId].Quantity++;
                else
                {
                    cart[productId] = new CartItem
                        {
                            Product = product,
                            Quantity = 1
                        };
                }
                return cart.Sum(kp => kp.Value.Quantity);
            }
        }