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); } }
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(); }
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(); }
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(); } }
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(); } }
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); }
public AccountController() { _db = new EcomContext(); }
public IEnumerable <ProductReview> GetProductReviews(int productId) { using var ecomContext = new EcomContext(); return(ecomContext.ProductReviews.Where(p => p.ProductId == productId).ToList()); }
public IEnumerable <Product> GetProductsByCategory(int categoryId) { using var ecomContext = new EcomContext(); return(ecomContext.Products.Where(p => p.CategoryId == categoryId).ToList()); }
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()); }
public IEnumerable <Category> GetParentCategories() { using var ecomContext = new EcomContext(); return(ecomContext.Categories.Include(p => p.ParentCategory).Where(p => p.ParentCategoryId == null).ToList()); }
public IEnumerable <Brand> GetAllBrands() { using var ecomContext = new EcomContext(); return(ecomContext.Brands.ToList()); }
public Brand GetBrandById(int id) { using var ecomContext = new EcomContext(); return(ecomContext.Brands.First(p => p.Id == id)); }
public HomeController() { _db = new EcomContext(); }
public List <Order> Orders() { using var db = new EcomContext(); return(db.Orders.ToList()); }
public ProductController() { _db = new EcomContext(); }
public List <Category> GetCategoriesWithProducts() { using var db = new EcomContext(); return(db.Categories.Include(p => p.Products).ToList()); }