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(); } }
public void AddNewUser(User user) { using (var context = new ShoppingWebDBEntities()) { context.AddToUsers(user); context.SaveChanges(); } }
public User FindByUserName(string userName) { if (userName == null) throw new NullReferenceException(); using (var context = new ShoppingWebDBEntities()) { return context.Users.SingleOrDefault(u => u.UserName == userName); } }
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(); } }
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); } }