示例#1
0
 public List <CartProduct> GetCartItems(ShoppingCart cart)        /* Tạm: không biết cách nào khác để lấy cartitem từ cookie tốt hơn */
 {
     using (var _db = new EcomContext())
     {
         var cartItems = new List <CartProduct>();
         foreach (var item in cart.CartProducts)
         {
             var cartProduct = new CartProduct
             {
                 Id       = item.Id,
                 CartId   = cart.Id,
                 Product  = _db.Products.Include(p => p.Brand).FirstOrDefault(p => p.Id == item.ProductId),
                 Quantity = item.Quantity
             };
             cartItems.Add(cartProduct);
         }
         return(cartItems);
     }
 }
示例#2
0
        public void RemoveFromCart(string cartId, int productId)
        {
            using var ecomContext = new EcomContext();
            if (ecomContext.ShoppingCarts.FirstOrDefault(p => p.Id == cartId) == null)
            {
                return;
            }
            var cartProduct = ecomContext.CartProducts
                              .FirstOrDefault(p => p.CartId == cartId &&
                                              p.ProductId == productId);
            bool isExist = cartProduct != null;

            if (isExist == false)
            {
                return;
            }

            ecomContext.CartProducts.Remove(cartProduct);
            ecomContext.SaveChanges();
        }
示例#3
0
        public void CartItem_IncreaseQuantity(string cartId, int productId)
        {
            using var ecomContext = new EcomContext();
            if (ecomContext.ShoppingCarts.FirstOrDefault(p => p.Id == cartId) == null)
            {
                return;
            }

            var cartProduct = ecomContext.CartProducts
                              .FirstOrDefault(p => p.CartId == cartId &&
                                              p.ProductId == productId);
            bool isExist = cartProduct != null;

            if (isExist == false)
            {
                return;
            }
            cartProduct.Quantity += 1;
            ecomContext.SaveChanges();
        }
示例#4
0
        private void UpdateOrder()
        {
            using (var _db = new EcomContext())
            {
                // Remove all Old-Order-items
                //
                var _orderItems = _db.OrderItems.Where(
                    p => p.OrderId == Order.Id
                    );
                _db.OrderItems.RemoveRange(_orderItems);
                _db.SaveChanges();

                // update Order
                //
                var order = _db.Orders.FirstOrDefault(
                    p => p.Id == Order.Id
                    );
                order.ObjectAssign(Order);

                // Insert new items
                //
                foreach (var item in OrderItems)
                {
                    var product = _db.Products.FirstOrDefault(
                        p => p.Name == item.Product.Name
                        );

                    if (product != null)
                    {
                        _db.OrderItems.Add(new OrderItem
                        {
                            OrderId   = Order.Id,
                            ProductId = product.Id,
                            Quantity  = item.Quantity
                        });
                    }
                }
                _db.SaveChanges();
            }
        }
示例#5
0
        private void InsertOrder()
        {
            using (var _db = new EcomContext())
            {
                _db.Orders.Add(Order);
                foreach (var item in OrderItems)
                {
                    var product = _db.Products.FirstOrDefault(
                        p => p.Name == item.Product.Name
                        );

                    if (product != null)
                    {
                        _db.OrderItems.Add(new OrderItem
                        {
                            OrderId   = Order.Id,
                            ProductId = product.Id,
                            Quantity  = item.Quantity
                        });
                    }
                }
                _db.SaveChanges();
            }
        }
示例#6
0
        public IEnumerable <ShopListItem> GetShopList(ShopListOptions options, ShoppingCart cart, out int total)
        {
            List <Product> productList;

            using EcomContext ecomContext = new EcomContext();
            var optionCategory = ecomContext.Categories
                                 .Include(p => p.ParentCategory)
                                 .FirstOrDefault(p => p.Id == options.CategoryId);

            productList = ecomContext.Products
                          .AsEnumerable()
                          .Where(p => (options.CategoryId == null) || IsSubCategory(p.CategoryId, optionCategory))
                          .Where(p => (options.BrandId == null) || (p.BrandId == options.BrandId))
                          .Where(p => (options.SearchText == null) ||
                                 (FullTextSearchFunction(p.ProductName, options.SearchText) ||
                                  FullTextSearchFunction(p.Description, options.SearchText)))
                          .Where(p => PriceLimitCheck(options.MinPrice, options.MaxPrice, p.UnitPrice))
                          .OrderBy(p => p, new SortByComparer(options.SortBy)) /*   Apply Sort Options   */


                          .ThenBy(p => p.UnitPrice, new PriceSortComparer(options.PriceSort))


                          .ThenBy(p => p.ProductName, new AlphabetSortComparer(options.AlphabetSort))
                          .ToList();

            total = productList.Count();        /* Get Total Matched */

            var shopList = productList
                           .Select(p => GetShopListItem(p, cart))
                           .Skip(((options.CurrentPage ?? 1) - 1) * (options.PageSize ?? 20)) /*  Pagination  */
                           .Take(options.PageSize ?? 20)
                           .ToList();

            return(shopList);
        }
示例#7
0
 public AccountController()
 {
     _db = new EcomContext();
 }
示例#8
0
 public IEnumerable <ProductReview> GetProductReviews(int productId)
 {
     using var ecomContext = new EcomContext();
     return(ecomContext.ProductReviews.Where(p => p.ProductId == productId).ToList());
 }
示例#9
0
 public IEnumerable <Product> GetProductsByCategory(int categoryId)
 {
     using var ecomContext = new EcomContext();
     return(ecomContext.Products.Where(p => p.CategoryId == categoryId).ToList());
 }
示例#10
0
 public IEnumerable <Product> GetProducstByBrand(int brandId)
 {
     using var ecomContext = new EcomContext();
     return(ecomContext.Products.Where(p => p.BrandId == brandId).ToList());
 }
 public List <Supplier> Suppliers()
 {
     using var db = new EcomContext();
     return(db.Suppliers.ToList());
 }
 public List <Category> Categories()
 {
     using var db = new EcomContext();
     return(db.Categories.ToList());
 }
示例#13
0
 public IEnumerable <Category> GetParentCategories()
 {
     using var ecomContext = new EcomContext();
     return(ecomContext.Categories.Include(p => p.ParentCategory).Where(p => p.ParentCategoryId == null).ToList());
 }
示例#14
0
 public IEnumerable <Brand> GetAllBrands()
 {
     using var ecomContext = new EcomContext();
     return(ecomContext.Brands.ToList());
 }
示例#15
0
 public Brand GetBrandById(int id)
 {
     using var ecomContext = new EcomContext();
     return(ecomContext.Brands.First(p => p.Id == id));
 }
示例#16
0
 public HomeController()
 {
     _db = new EcomContext();
 }
 public List <Order> Orders()
 {
     using var db = new EcomContext();
     return(db.Orders.ToList());
 }
示例#18
0
 public ProductController()
 {
     _db = new EcomContext();
 }
 public List <Category> GetCategoriesWithProducts()
 {
     using var db = new EcomContext();
     return(db.Categories.Include(p => p.Products).ToList());
 }