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) ? /* Apply Filters */ true : IsSubCategory(p.CategoryId, optionCategory)) .Where(p => (options.BrandId == null) ? true : (p.BrandId == options.BrandId)) .Where(p => (options.SearchText == null) ? true : (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 IActionResult ShopList(ShopListOptions options) { var repo = new EcomRepository(); var cart = PageMaster.GetShoppingCart(); int total; var model = new ShopListModel { ShopList = repo.GetShopList(options, cart, out total), Brands = repo.GetAllBrands() }; options.TotalItems = total; ViewBag.Options = options; ViewBag.CountCartItems = cart.CartProducts != null?cart.CartProducts.Count() : 0; return(View(model)); }
public IActionResult ShopList(ShopListOptions options) { ShoppingCart cart; if (SignInManager.IsSignedIn(User)) { string cartId = UserManager.GetUserId(User); cart = Repository.GetCartById(cartId); } else { try { string requestCookie = HttpContext.Request.Cookies[_cartCookieName]; cart = JsonConvert.DeserializeObject <ShoppingCart>(requestCookie); } catch (Exception) { cart = new ShoppingCart() { Id = Guid.NewGuid().ToString(), CartProducts = new List <CartProduct>() }; HttpContext.Response.Cookies .Append(_cartCookieName, JsonConvert.SerializeObject(cart)); } } var model = new ShopListModel(); int total; model.ShopList = Repository.GetShopList(options, cart, out total); model.Brands = Repository.GetAllBrands(); model.TotalPage = total / (options.PageSize ?? 20); ViewBag.Options = options; ViewBag.CountCartItems = cart.CartProducts.Count(); return(View(model)); }