Example #1
0
 public int GetProductCountPerCategory(int categoryId)
 {
     using (var context = new ECommerceDbDataContext(_conStr))
     {
         return context.Products.Where(p => p.CategoryId == categoryId).Count();
     }
 }
Example #2
0
 public IEnumerable<Category> GetCategories()
 {
     using (var context = new ECommerceDbDataContext(_conStr))
     {
         return context.Categories.ToList();
     }
 }
Example #3
0
 public void AddProduct(Product product)
 {
     using(var context = new ECommerceDbDataContext(_conStr))
     {
         context.Products.InsertOnSubmit(product);
         context.SubmitChanges();
     }
 }
Example #4
0
 public void AddImage(List<Image> images)
 {
     using(var context = new ECommerceDbDataContext(_conStr))
     {
         context.Images.InsertAllOnSubmit(images);
         context.SubmitChanges();
     }
 }
Example #5
0
 public void AddCategory(Category category)
 {
     using(var context = new ECommerceDbDataContext(_conStr))
     {
         context.Categories.InsertOnSubmit(category);
         context.SubmitChanges();
     }
 }
Example #6
0
 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);
     }
 }
Example #7
0
        public void DeleteItem(int itemId)
        {
            using (var context = new ECommerceDbDataContext(_conStr))
            {
                context.ExecuteCommand("DELETE FROM CartItems WHERE CartItemsId = {0}", itemId);

            }
        }
Example #8
0
 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();
     }
 }
Example #9
0
 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();
     }
 }
Example #10
0
 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;
     }
 }
Example #11
0
 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();
     }
 }
Example #12
0
 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();
     }
 }
Example #13
0
        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();
            }
        }
Example #14
0
        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;
            }
        }
Example #15
0
 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();
     }
 }
Example #16
0
 private bool CheckIfItemIsAlreadyInCart(CartItem items)
 {
     using (var context = new ECommerceDbDataContext(_conStr))
     {
         return context.CartItems.Any(c => c.CartId == items.CartId && c.ProductId == items.ProductId);
     }
 }