public int GetProductCountPerCategory(int categoryId) { using (var context = new ECommerceDbDataContext(_conStr)) { return context.Products.Where(p => p.CategoryId == categoryId).Count(); } }
public IEnumerable<Category> GetCategories() { using (var context = new ECommerceDbDataContext(_conStr)) { return context.Categories.ToList(); } }
public void AddProduct(Product product) { using(var context = new ECommerceDbDataContext(_conStr)) { context.Products.InsertOnSubmit(product); context.SubmitChanges(); } }
public void AddImage(List<Image> images) { using(var context = new ECommerceDbDataContext(_conStr)) { context.Images.InsertAllOnSubmit(images); context.SubmitChanges(); } }
public void AddCategory(Category category) { using(var context = new ECommerceDbDataContext(_conStr)) { context.Categories.InsertOnSubmit(category); context.SubmitChanges(); } }
public int GetCartCount(int cartId) { using (var context = new ECommerceDbDataContext(_conStr)) { context.Log = new DebugTextWriter(); return context.CartItems.Where(c => c.CartId == cartId).Sum(q => q.Quantity); } }
public void DeleteItem(int itemId) { using (var context = new ECommerceDbDataContext(_conStr)) { context.ExecuteCommand("DELETE FROM CartItems WHERE CartItemsId = {0}", itemId); } }
public Product GetProductWithImages(int productId) { using(var context = new ECommerceDbDataContext(_conStr)) { var loadOptions = new DataLoadOptions(); loadOptions.LoadWith<Product>(p => p.Images); context.LoadOptions = loadOptions; return context.Products.Where(p => p.ProductId == productId).FirstOrDefault(); } }
public IEnumerable<Product> GetProductsWithImage(int categoryId, int page) { int skip = (page - 1) * 3; using(var context = new ECommerceDbDataContext(_conStr)) { var loadOptions = new DataLoadOptions(); loadOptions.LoadWith<Product>(p => p.Images); context.LoadOptions = loadOptions; return context.Products.Skip(skip).Take(3).Where(p => p.CategoryId == categoryId).ToList(); } }
public Cart CreateCart() { using(var context = new ECommerceDbDataContext(_conStr)) { Cart cart = new Cart(); cart.DateCreated = DateTime.Now; context.Carts.InsertOnSubmit(cart); context.SubmitChanges(); return cart; } }
public IEnumerable<CartItem> GetItemsInCart(int? cartId) { if (cartId == null) { return null; } using (var context = new ECommerceDbDataContext(_conStr)) { var loadOptions = new DataLoadOptions(); loadOptions.LoadWith<CartItem>(c => c.Product); context.LoadOptions = loadOptions; return context.CartItems.Where(c => c.CartId == cartId).ToList(); } }
public void AddUser(string username, string password) { string salt = PasswordHelper.GenerateRandomSalt(); string hash = PasswordHelper.HashPassword(password, salt); User user = new User { UserName = username, PasswordSalt = salt, PasswordHash = hash, }; using(var context = new ECommerceDbDataContext(_connectionString)) { context.Users.InsertOnSubmit(user); context.SubmitChanges(); } }
public void AddToCart(CartItem items) { using (var context = new ECommerceDbDataContext(_conStr)) { if(CheckIfItemIsAlreadyInCart(items)) { var updateItem = context.CartItems.Where(i => i.CartId == items.CartId && i.ProductId == items.ProductId).FirstOrDefault(); updateItem.Quantity += items.Quantity; } else { context.CartItems.InsertOnSubmit(items); } context.SubmitChanges(); } }
public User Login(string username, string password) { using (var context = new ECommerceDbDataContext(_connectionString)) { User user = context.Users.FirstOrDefault(u => u.UserName == username); if (user == null) { return null; } if (!PasswordHelper.PasswordMatch(password, user.PasswordSalt, user.PasswordHash)) { return null; } return user; } }
public void UpdateQuantity(int quantity, int itemId) { using (var context = new ECommerceDbDataContext(_conStr)) { var updateItem = context.CartItems.Where(i => i.CartItemsId == itemId).FirstOrDefault(); updateItem.Quantity = quantity; context.SubmitChanges(); } }
private bool CheckIfItemIsAlreadyInCart(CartItem items) { using (var context = new ECommerceDbDataContext(_conStr)) { return context.CartItems.Any(c => c.CartId == items.CartId && c.ProductId == items.ProductId); } }