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

            }
        }
Esempio n. 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();
     }
 }
Esempio n. 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();
     }
 }
Esempio n. 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;
     }
 }
Esempio n. 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();
     }
 }
Esempio n. 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();
     }
 }
Esempio n. 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();
            }
        }
Esempio n. 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;
            }
        }
Esempio n. 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();
     }
 }
Esempio n. 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);
     }
 }