public ActionResult Index(int? page) { var viewModel = new ProductsListViewModel { Products = this.productsListQuery.GetPagedList(page ?? 1, DefaultPageSize) }; return View(viewModel); }
public ViewResult Index1(string categorys, int page = 1) { ProductsListViewModel model = new ProductsListViewModel { Products = repository.Products .Where(p => categorys == null || p.Category == categorys) .OrderBy(p => p.ProductID) .Skip((page - 1) * PageSize) .Take(PageSize), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = categorys == null ? repository.Products.Count() : repository.Products.Where(e => e.Category == categorys).Count() }, CurrentCategory = categorys }; return View(model); }
public void Can_Paginate() { //arrange Mock <IProductRepository> mock = new Mock <IProductRepository>(); mock.Setup(m => m.Products).Returns((new Product[] { new Product { ProductID = 1, Name = "P1" }, new Product { ProductID = 2, Name = "P2" }, new Product { ProductID = 3, Name = "P3" }, new Product { ProductID = 4, Name = "P4" }, new Product { ProductID = 5, Name = "P5" } }).AsQueryable <Product>()); ProductController controller = new ProductController(mock.Object); controller.PageSize = 3; //act ProductsListViewModel result = controller.List(null, 2).ViewData.Model as ProductsListViewModel; //assert Product[] prodArray = result.Products.ToArray(); Assert.True(prodArray.Length == 2); Assert.Equal("P4", prodArray[0].Name); Assert.Equal("P5", prodArray[1].Name); }
public ViewResult List(string category, int page = 1) { ViewBag.myCount = repository.Products.Count <Product>(); ProductsListViewModel model = new ProductsListViewModel { Products = repository.Products .Where(p => category == null || p.Category == category) .OrderBy(p => p.ProductID) .Skip((page - 1) * PageSize) .Take(PageSize), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = category == null?repository.Products.Count() : repository.Products.Where(e => e.Category == category).Count() }, CurrentCategory = category }; return(View(model)); }
// public IActionResult Index() => View(repository.Products); public ViewResult Index(string category, int productPage = 1) { var plvm = new ProductsListViewModel { Products = repository.Products .Where(p => category == null | p.Category == category) .OrderBy(p => p.ProductID) .Skip((productPage - 1) * PageSize) .Take(PageSize), PagingInfo = new PagingInfo { CurrentPage = productPage, ItemsPerPage = PageSize, TotalItems = category == null? repository.Products.Count() : repository.Products.Where(e => e.Category == category).Count() }, CurrentCategory = category }; return(View(plvm)); }
public void Can_Send_Pagination_View_Model() { Mock <IProductRepository> mock = new Mock <IProductRepository>(); mock.Setup(p => p.Products).Returns(new Product[] { new Product { ProductID = 1, Name = "P1" }, new Product { ProductID = 2, Name = "P2" }, new Product { ProductID = 3, Name = "P3" }, new Product { ProductID = 4, Name = "P4" }, new Product { ProductID = 5, Name = "P5" }, }); var controller = new ProductController(mock.Object) { PageSize = 3 }; ProductsListViewModel result = controller.List(2).Model as ProductsListViewModel; PagingInfo pageInfo = result.PagingInfo; Assert.IsTrue(pageInfo.CurrentPage == 2); Assert.AreEqual(pageInfo.ItemsPerPage, 3); Assert.AreEqual(pageInfo.TotalItems, 5); Assert.AreEqual(pageInfo.TotalPages, 2); }
public void Can_Send_Pagination_View_Model() { // Arrange Mock <IProductRepository> mock = new Mock <IProductRepository>(); mock.Setup(m => m.Products).Returns(new Product[] { new Product { Id = 1, Name = "P1" }, new Product { Id = 2, Name = "P2" }, new Product { Id = 3, Name = "P3" }, new Product { Id = 4, Name = "P4" }, new Product { Id = 5, Name = "P5" } }); // Arrange ProductController controller = new ProductController(mock.Object); controller.PageSize = 3; // Act ProductsListViewModel result = (ProductsListViewModel)controller.List(null, 2).Model; // Assert PagingInfo pageInfo = result.PagingInfo; Assert.AreEqual(pageInfo.CurrentPage, 2); Assert.AreEqual(pageInfo.ItemsPerPage, 3); Assert.AreEqual(pageInfo.TotalItens, 5); Assert.AreEqual(pageInfo.TotalPages, 2); }
public void Generate_Specific_Category_Product_Count() { // Arrange Mock <IProductRepository> mock = new Mock <IProductRepository>(); mock.Setup(m => m.Products).Returns(new Product[] { new Product { ProductID = 1, Category = "C1" }, new Product { ProductID = 2, Category = "C1" }, new Product { ProductID = 3, Category = "C2" }, new Product { ProductID = 4, Category = "C2" }, new Product { ProductID = 5, Category = "C2" } }); // Arrange ProductController controller = new ProductController(mock.Object); controller.PageSize = 2; // Act ProductsListViewModel result = (ProductsListViewModel)controller.List("C2").Model; // Assert PagingInfo pageInfo = result.PagingInfo; Assert.AreEqual(pageInfo.TotalItems, 3); }
public void Can_Use_Repository() { // Arrange Mock <IStoreRepository> mock = new Mock <IStoreRepository>(); mock.Setup(m => m.Products).Returns((new Product[] { new Product { ProductID = 1, Name = "P1" }, new Product { ProductID = 2, Name = "P2" } }).AsQueryable <Product>()); HomeController controller = new HomeController(mock.Object); // Act ProductsListViewModel result = (controller.Index() as ViewResult).ViewData.Model as ProductsListViewModel; // Assert Product[] prodArray = result.Products.ToArray(); Assert.True(prodArray.Length == 2); Assert.Equal("P1", prodArray[0].Name); Assert.Equal("P2", prodArray[1].Name); }
public void Can_Send_Pagination_View_Model() { Mock <IStoreRepository> mock = new Mock <IStoreRepository>(); mock.Setup(m => m.Products).Returns((new Product[] { new Product { ProductID = 1, Name = "p1" }, new Product { ProductID = 2, Name = "p2" }, new Product { ProductID = 3, Name = "p3" }, new Product { ProductID = 4, Name = "p4" }, new Product { ProductID = 5, Name = "p5" }, }).AsQueryable <Product>()); HomeController homeController = new HomeController(mock.Object) { _PageSize = 3 }; ProductsListViewModel result = (homeController.Index(2) as ViewResult).ViewData.Model as ProductsListViewModel; // Assert PagingInfo pageInfo = result.PagingInfo; Assert.Equal(2, pageInfo.CurrentPage); Assert.Equal(3, pageInfo.ItemsPerPage); Assert.Equal(5, pageInfo.TotalItems); Assert.Equal(2, pageInfo.TotalPages); }
public void Can_Paginate() { // Arrange Mock <IProductRepository> mock = new Mock <IProductRepository>(); mock.Setup(m => m.Products).Returns(new Product[] { new Product { ProductID = 1, Name = "P1" }, new Product { ProductID = 2, Name = "P2" }, new Product { ProductID = 3, Name = "P3" }, new Product { ProductID = 4, Name = "P4" }, new Product { ProductID = 5, Name = "P5" } }); ProductController controller = new ProductController(mock.Object); controller.PageSize = 3; // Act // Act ProductsListViewModel result = (ProductsListViewModel)controller.List(null, 2).Model; // Assert Product[] prodArray = result.Products.ToArray(); Assert.IsTrue(prodArray.Length == 2); Assert.AreEqual(prodArray[0].Name, "P4"); Assert.AreEqual(prodArray[1].Name, "P5"); }
public ViewResult List(string category, int page = 1) { ProductsListViewModel model = new ProductsListViewModel { Products = repository.Products.OrderBy(p => p.ProductID). Where(p => p.Category == null || p.Category == category || category == null). Skip((page - 1) * PageSize). Take(PageSize), PagingInfo = new PagingInfo { TotalItems = category == null?repository.Products.Count() : repository.Products.Where(p => p.Category == category).Count(), CurrentPage = page, ItemsPerPage = PageSize }, CurrentCategory = category }; var r = repository.Products.OrderByDescending(e => e.Price) .Take(3) .Select(e => new { e.Name, e.Price }); bool a = r == null; return(View(model)); }
public ActionResult List(string category, int page = 1) { //if no game in progress then go back to the intro page. if (!Session.GetGameInProgress()) //this variable is always true or null { return(RedirectToAction("Index", "Home")); } int remainingMilliseconds = Session.GetRemainingTime(); // countdown time variable. Cart cart; int productId = 0; string updateMsg = string.Empty; // Check if "cartObj" key exists and get it back. if (Session["cartObj"] != null) { cart = (Cart)Session["cartObj"]; } else { cart = new Cart(); } // simulate further shopping by the NPC IEnumerable <Product> list = repository.Products.ToList <Product>(); Session.RunNpcSweep(cart, list); TempData["npcCart"] = cart.LinesOther; //store NPC cart // Check if "navDictionary" key exists if (TempData["navDictionary"] != null) { // get category (but only if null) and page Dictionary <string, object> dict = TempData["navDictionary"] as Dictionary <string, object>; if (category == null) { category = ((string)dict["category"] == string.Empty ? null : (string)dict["category"]); } page = (int)dict["page"]; productId = (int)dict["productId"]; updateMsg = (string)dict["message"]; } #region legacy pattern code //Original code correctly works for one category per product only. //ProductsListViewModel model = new ProductsListViewModel //{ // Products = repository.Products // .Where(p => category == null || p.Category == category) // .OrderBy(p => p.ProductID) // .Skip((page - 1) * PageSize) // .Take(PageSize), // PagingInfo = new PagingInfo // { // CurrentPage = page, // ItemsPerPage = PageSize, // TotalItems = category == null ? repository.Products.Count() : repository.Products.Where(e => e.Category == category).Count() // }, // CurrentCategory = category //}; #endregion //merge product and cart info var productList = repository.Products.Where(p => category == null || p.Categories.EmptyArrayIfNull().Contains(category)) .OrderBy(p => p.ProductID) .Skip((page - 1) * PageSize) .Take(PageSize); MergeProductsStockWithCart(productList, cart); int totalNpcQuantity = cart.ComputeTotalQuantitiesOther(); ProductsListViewModel model = new ProductsListViewModel { Products = productList, PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = category == null?repository.Products.Count() : repository.Products.Where(e => e.Categories.EmptyArrayIfNull().Contains(category)).Count() }, CurrentCategory = category, CountDownMilliseconds = remainingMilliseconds, TotalQuantity = totalNpcQuantity, UpdatedProductId = productId, UpdatedMessage = updateMsg }; return(View(model)); }
public ActionResult List(string itemType, string customerCode, string saleId) { var sale = applicationDataContext.Sales.Where(x => x.SaleID == saleId).Single(); string employeeId = string.Empty; if (Session["EmployeeID"] != null) { employeeId = Session["EmployeeID"].ToString(); } decimal?itemTypeId = null; if (!String.IsNullOrEmpty(itemType)) { var i = wmsDataContext.ItemTypes.Where(x => x.Description == itemType).SingleOrDefault(); if (i != null) { itemTypeId = i.ID; } } List <SaleProductInfo> saleProductInfos; if (!string.IsNullOrEmpty(employeeId) && sale.SaleType == "Pre-Allocated") { saleProductInfos = applicationDataContext.AssetAllocations.Expand("Sale, SaleProduct, SaleProduct/Product, Order").Where(x => x.Sale.SaleID == saleId && x.EmployeeID == employeeId && x.Order == null).ToList().Select(x => new SaleProductInfo { SaleProduct = x.SaleProduct, AssetAllocation = x, InStock = true }).ToList(); } else { saleProductInfos = applicationDataContext.SaleProducts.Expand("Product").Where(x => x.Sale.SaleID == saleId).ToList().Select(x => new SaleProductInfo { SaleProduct = x, InStock = x.QtyAvailable > 0 }).ToList(); } if (itemTypeId.HasValue) { saleProductInfos = saleProductInfos.Where(x => x.SaleProduct.Product.ItemType_ID == itemTypeId).ToList(); } var customer = applicationDataContext.Customers.Where(x => x.CustomerCode == customerCode).SingleOrDefault(); if (customer != null) { Session["CustomerName"] = customer.CustomerName; Session["CustomerCode"] = customer.CustomerCode; Session["SaleID"] = saleId; } ProductsListViewModel model = new ProductsListViewModel { SaleProductInfos = saleProductInfos.AsEnumerable(), SelectedItemType = itemType, Customer = customer, SaleId = saleId }; return(View(model)); }
public async Task <IActionResult> showBySeach(string Input, int pn = 1) { int showpage = 6; LogDetails log = new LogDetails() { VisitTime = DateTime.Now, IP = Request.HttpContext.Connection.RemoteIpAddress.ToString(), Description = "جستجو: " + Input }; _LogDetails.Add(log); IEnumerable <Product> ProductTAGS = new List <Product>(); IEnumerable <Product> pr = await RepoPrc.GetProductsSearchAsync(showpage, pn, Input); //List<Product> prlis = await RepoPrc.GetProductsSearchAsync(showpage, pn, Input); if (RepoPrc.TotalCountSearch(Input) / showpage < pn) { if (!FirstReleted) { TafazolReleted = pn; FirstReleted = true; } //int TafazolPage = pn; //ProductTAGS = ProductInfoREPO.GetProductByTag(showpage, Tagpn, Input); ProductTAGS = await ProductInfoREPO.GetProductByTagAsync(showpage, pn - RepoPrc.TotalCountSearch(Input) / showpage, Input); //prlis.InsertRange() //ProductTAGS = resr.ToList; //ProductTAGS= _mapper.Map<Product, GetProductByTag>(resProductTAGS); /// this.Database.SqlQuery<YourEntityType>("storedProcedureName",params); } List <productSingleImage> singleImagesList = new List <productSingleImage>(); foreach (var item in pr) { singleImagesList.Add( new productSingleImage() { mainImages = item.mainImages, Category = item.Category, Description = item.Description, Name = item.Name, ProductID = item.ProductID } ); } foreach (var item in ProductTAGS) { singleImagesList.Add( new productSingleImage() { mainImages = item.mainImages, //Category = item.Category, Description = item.Description, Name = item.Name, ProductID = item.ProductID } ); } PagingInfo pagin = new PagingInfo { CurrentPage = pn, TotalItems = RepoPrc.TotalCountSearch(Input) + ProductInfoREPO.TotalCountSearchTag(Input), ItemsPerPage = showpage }; ProductsListViewModel productsListViewModel = new ProductsListViewModel() { Products = singleImagesList, Current = Input, PagingInfo = pagin, fromContoller = "showBySeach" }; return(View("showListproduct", productsListViewModel)); //TempData["productsListViewModel"] = JsonConvert.SerializeObject(productsListViewModel); //return RedirectToAction("showListproduct", "Product", new { @pn = pn }); }
public ActionResult Category(string id, int?page = null, int?count = null) { var category = _context.Categories.FirstOrDefault(x => x.Key == id); IQueryable <Product> products = _context.Products; if (category == null) { products = products .OrderBy(x => x.CategoryId) .ThenBy(x => x.Name); } else { products = products .Where(x => x.CategoryId == category.Id) .OrderBy(x => x.Name); } var skus = products.Select(x => x.SKU); var ratings = _context.GetProductRatings(skus); ViewData["Ratings"] = ratings; ViewData["Category"] = category ?? new Category { Name = "All Categories" }; /**** Paging Logic ****/ var model = products; var resultsCount = model.Count(); var pageSize = count.GetValueOrDefault(DefaultPageSize); var currentPage = page.GetValueOrDefault(1); var pageCount = resultsCount / pageSize + (resultsCount % pageSize > 0 ? 1 : 0); var previousPage = (currentPage - 1 > 0) ? currentPage - 1 : (int?)null; var nextPage = (currentPage + 1 <= pageCount) ? currentPage + 1 : (int?)null; model = model .Skip((currentPage - 1) * pageSize) .Take(pageSize); ViewData["PageSize"] = pageSize; ViewData["ResultsCount"] = resultsCount; ViewData["CurrentPage"] = currentPage; ViewData["PageCount"] = pageCount; ViewData["PreviousPage"] = previousPage; ViewData["NextPage"] = nextPage; /**** End Paging Logic ****/ var vm = new ProductsListViewModel { Products = products.Select(x => new ProductViewModel { MSRP = x.MSRP, Name = x.Name, Price = x.Price, SKU = x.SKU, Rating = ratings.FirstOrDefault(y => x.SKU == y.SKU), }) }; if (Request.IsAjaxRequest()) { return(PartialView("ProductList", vm)); } return(View("ProductList", vm)); }
public async Task <IActionResult> Index(string category, ProductSearchFiltersViewModel filtersViewModel, int?page) { ViewBag.FiltersViewModel = filtersViewModel; var products = context.Products .Include(p => p.Manufacturer) .Where(p => category == null || p.Category.NameForUrl == category); if (filtersViewModel.MaxProdPrice > 0) { products = products .Where(p => p.Price >= filtersViewModel.MinProdPrice && p.Price <= filtersViewModel.MaxProdPrice); } // if if (filtersViewModel.Manufacturers != null && filtersViewModel.Manufacturers.Values.Any(m => m.IsSelected)) { products = products .Where(p => filtersViewModel.Manufacturers .Any(m => m.Key == p.ManufacturerId && m.Value.IsSelected) ); } // if if (filtersViewModel.ProdSpecsWithValues != null && filtersViewModel.ProdSpecsWithValues.Values .Any(pswv => pswv.Values.Any(psv => psv.IsSelected))) { foreach (var item in filtersViewModel.ProdSpecsWithValues) { products = products .Where(p => p.ProductSpecificationValues .Any(pPsv => pPsv.ProductSpecificationId == item.Key && item.Value.Any(psv => psv.Key == pPsv.Id && psv.Value.IsSelected) ) ); } // foreach } // if var viewModel = new ProductsListViewModel { Products = await products.OrderBy(p => p.Id) .Skip(((page ?? 1) - 1) * pageSize) .Take(pageSize) .ToListAsync(), PagingInfo = new PagingInfo { CurrentPage = page ?? 1, ItemsPerPage = pageSize, TotalItems = await products.CountAsync(), ElementSizeClass = "pagination-lg" }, CurrentCategory = context.Categories .FirstOrDefault(c => c.NameForUrl == category) }; ViewBag.SelectedCategory = viewModel.CurrentCategory; viewModel.PagingInfo.PageUrlValues["category"] = category; return(View(viewModel)); }
public ViewResult Index() { ProductsListViewModel obj = new ProductsListViewModel(allProducts.AllProducts, allProducts.HitProducts, allProducts.NoveltyProducts); return(View(obj)); }
public ViewResult List(string category, int page = 1) { var products = repository.Products.Include(p => p.Category); ProductsListViewModel model = new ProductsListViewModel { Products = products.OrderBy(p => p.ID) .Skip((page - 1) * PageSize) .Take(PageSize), PagingInfo = new PagingInfoViewModel { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = products.Count() } }; return View(model); }
public ProductsListPage() { InitializeComponent(); BindingContext = new ProductsListViewModel(); }
public ViewResult List(string category, string sort = "SortByNameUp", int page = 1) { ProductsListViewModel model = new ProductsListViewModel(); if (sort == "SortByNameUp") { categ = repository.Categories.Where(c => c.Type == category).FirstOrDefault(); model = new ProductsListViewModel { Products = repository.Products .Where(p => p.Category == categ || category == null) .OrderBy(product => product.Name) .Skip((page - 1) * pageSize) .Take(pageSize), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = pageSize, TotalItems = category == null? repository.Products.Count() : repository.Products.Where(product => product.Category == categ).Count() }, CurrentCategory = category, CurrentSort = sort }; } else if (sort == "SortByNameDown") { categ = repository.Categories.Where(c => c.Type == category).FirstOrDefault(); model = new ProductsListViewModel { Products = repository.Products .Where(p => p.Category == categ || category == null) .OrderByDescending(product => product.Name) .Skip((page - 1) * pageSize) .Take(pageSize), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = pageSize, TotalItems = category == null? repository.Products.Count() : repository.Products.Where(product => product.Category == categ).Count() }, CurrentCategory = category, CurrentSort = sort }; } else if (sort == "SortByPriceDown") { categ = repository.Categories.Where(c => c.Type == category).FirstOrDefault(); model = new ProductsListViewModel { Products = repository.Products .Where(p => p.Category == categ || category == null) .OrderByDescending(product => product.Price) .Skip((page - 1) * pageSize) .Take(pageSize), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = pageSize, TotalItems = category == null? repository.Products.Count() : repository.Products.Where(product => product.Category == categ).Count() }, CurrentCategory = category, CurrentSort = sort }; } else if (sort == "SortByPriceUp") { categ = repository.Categories.Where(c => c.Type == category).FirstOrDefault(); model = new ProductsListViewModel { Products = repository.Products .Where(p => p.Category == categ || category == null) .OrderBy(product => product.Price) .Skip((page - 1) * pageSize) .Take(pageSize), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = pageSize, TotalItems = category == null? repository.Products.Count() : repository.Products.Where(product => product.Category == categ).Count() }, CurrentCategory = category, CurrentSort = sort }; } else if (sort == "SortByCategoryDown") { categ = repository.Categories.Where(c => c.Type == category).FirstOrDefault(); model = new ProductsListViewModel { Products = repository.Products .Where(p => p.Category == categ || category == null) .OrderByDescending(product => product.Category.Type) .Skip((page - 1) * pageSize) .Take(pageSize), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = pageSize, TotalItems = category == null? repository.Products.Count() : repository.Products.Where(product => product.Category == categ).Count() }, CurrentCategory = category, CurrentSort = sort }; } else if (sort == "SortByCategoryUp") { categ = repository.Categories.Where(c => c.Type == category).FirstOrDefault(); model = new ProductsListViewModel { Products = repository.Products .Where(p => p.Category == categ || category == null) .OrderBy(product => product.Category.Type) .Skip((page - 1) * pageSize) .Take(pageSize), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = pageSize, TotalItems = category == null? repository.Products.Count() : repository.Products.Where(product => product.Category == categ).Count() }, CurrentCategory = category, CurrentSort = sort }; } return(View(model)); }
public void can_filter_products() { //setup //mock product repository Mock <IProductRepository> _mock = new Mock <IProductRepository>(); _mock.Setup(m => m.Products).Returns( new List <Product> { new Product() { ProductID = 1, Name = "Product1", Category = "c1" }, new Product() { ProductID = 1, Name = "Product2", Category = "c1" }, new Product() { ProductID = 1, Name = "Product3", Category = "c1" }, new Product() { ProductID = 1, Name = "Product4", Category = "c1" }, new Product() { ProductID = 1, Name = "Product5", Category = "c2" }, new Product() { ProductID = 1, Name = "Product6", Category = "c2" }, new Product() { ProductID = 1, Name = "Product7", Category = "c2" }, new Product() { ProductID = 1, Name = "Product8", Category = "c2" }, new Product() { ProductID = 1, Name = "Product9", Category = "c2" }, new Product() { ProductID = 1, Name = "Product10", Category = "c2" }, new Product() { ProductID = 1, Name = "Product11", Category = "c3" }, new Product() { ProductID = 1, Name = "Product12", Category = "c3" } }.AsQueryable() ); //target ProductController target = new ProductController(_mock.Object); ProductsListViewModel _m = (ProductsListViewModel)target.List("c3").Model; Assert.AreEqual(2, _m.Products.Count()); }
public ActionResult ProductList(int?subId, SortCategories sortBy = SortCategories.All, int page = 1, int pageSize = 4) { if (subId == null) { return(HttpNotFound()); } IEnumerable <Product> pro = manager.GetProducts().Where(p => p.SubCategoryId == subId); switch (sortBy) { case SortCategories.Name: pro = pro.OrderBy(p => p.Name); break; case SortCategories.PriceHigh: pro = pro.OrderByDescending(p => p.Price); break; case SortCategories.PriceLower: pro = pro.OrderBy(p => p.Price); break; case SortCategories.Reviews: pro.ToList(); break; case SortCategories.BestShiping: pro.ToList(); break; default: pro.ToList(); break; } List <ProductStoreViewModel> prod = pro .Skip((page - 1) * pageSize) .Take(pageSize) .Select(p => new ProductStoreViewModel { Name = p.Name, ShortDescription = p.Description, LongDescription = p.LongDescription, Price = p.Price, Path = p.Images.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).First(), ProductID = p.Id, SubCategoryId = p.SubCategoryId.Value, }).ToList(); int totalCount = pro.Count(); var products = ListExtensions.ToPaginatedList(prod, page, pageSize, totalCount); ProductsListViewModel model = new ProductsListViewModel { ProductList = products, pageSizes = new List <SelectListItem> { new SelectListItem { Text = "4", Value = "4" }, new SelectListItem { Text = "8", Value = "8" }, new SelectListItem { Text = "12", Value = "12" }, new SelectListItem { Text = "24", Value = "24" }, }, CurrentSubCategoryId = subId.Value, SubCategoryName = manager.GetSubCategoryById(subId.Value).Name, CurrentPage = products.PageIndex }; if (Request.IsAjaxRequest()) { return(PartialView("_PartialProductList", model)); } return(View(model)); }
public void send_pagination_view_model() { //mock product repository Mock <IProductRepository> _mock = new Mock <IProductRepository>(); _mock.Setup(m => m.Products).Returns( new List <Product> { new Product() { ProductID = 1, Name = "Product1" }, new Product() { ProductID = 1, Name = "Product2" }, new Product() { ProductID = 1, Name = "Product3" }, new Product() { ProductID = 1, Name = "Product4" }, new Product() { ProductID = 1, Name = "Product5" }, new Product() { ProductID = 1, Name = "Product6" }, new Product() { ProductID = 1, Name = "Product7" }, new Product() { ProductID = 1, Name = "Product8" }, new Product() { ProductID = 1, Name = "Product9" }, new Product() { ProductID = 1, Name = "Product10" }, new Product() { ProductID = 1, Name = "Product11" }, new Product() { ProductID = 1, Name = "Product12" } }.AsQueryable() ); //target class ProductController target = new ProductController(_mock.Object); target.pageSize = 4; ProductsListViewModel _m = (ProductsListViewModel)target.List(null, 2).Model; Assert.AreEqual(2, _m.PagingInfo.CurrentPage); Assert.AreEqual(12, _m.PagingInfo.TotalItems); Assert.AreEqual(3, _m.PagingInfo.TotalPages); }
public ViewResult List(string category, int page = 1, SortingType sortingOption = 0) { int categoryId = 0; if (_categoryRepository != null) { categoryId = _categoryRepository.GetCategoryId(category); } IEnumerable <Product> products = _productRepository.Products.Where(x => x.IsDeleted == false).OrderBy(x => x.ProductId); switch (sortingOption) { case SortingType.NoSorting: break; case SortingType.NameAZ: products = products.OrderBy(x => x.Name); break; case SortingType.NameZA: products = products.OrderByDescending(x => x.Name); break; case SortingType.HighestPrice: products = products.OrderByDescending(x => x.Price); break; case SortingType.LowestPrice: products = products.OrderBy(x => x.Price); break; case SortingType.MostPopular: products = products.OrderByDescending(x => x.NumberOfBought).ThenByDescending(x => x.Visits); break; } if (page == 1) { products = products.Where(x => category == null || x.CategoryId == categoryId).Take(PageSize); } else { products = products.Where(x => category == null || x.CategoryId == categoryId).Skip((page - 1) * PageSize); } var model = new ProductsListViewModel { Products = products, PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = category == null?_productRepository.Products.Count() : _productRepository.Products.Count(e => e.CategoryId == categoryId) }, CurrentCategory = category, CurrentSorting = sortingOption }; return(View(model)); }
public void generate_category_specific_product_count() { //mock product repository Mock <IProductRepository> _mock = new Mock <IProductRepository>(); _mock.Setup(m => m.Products).Returns( new List <Product> { new Product() { ProductID = 1, Name = "Product1", Category = "c8" }, new Product() { ProductID = 1, Name = "Product2", Category = "c8" }, new Product() { ProductID = 1, Name = "Product3", Category = "c8" }, new Product() { ProductID = 1, Name = "Product4", Category = "c8" }, new Product() { ProductID = 1, Name = "Product5", Category = "c2" }, new Product() { ProductID = 1, Name = "Product6", Category = "c2" }, new Product() { ProductID = 1, Name = "Product7", Category = "c2" }, new Product() { ProductID = 1, Name = "Product8", Category = "c2" }, new Product() { ProductID = 1, Name = "Product9", Category = "c2" }, new Product() { ProductID = 1, Name = "Product10", Category = "c2" }, new Product() { ProductID = 1, Name = "Product11", Category = "c3" }, new Product() { ProductID = 1, Name = "Product12", Category = "c3" } }.AsQueryable() ); //target class ProductController target = new ProductController(_mock.Object); target.pageSize = 10; ProductsListViewModel count_c3 = (ProductsListViewModel)target.List("c3", 1).Model; ProductsListViewModel count_c2 = (ProductsListViewModel)target.List("c2", 1).Model; ProductsListViewModel count_c8 = (ProductsListViewModel)target.List("c8", 1).Model; Assert.AreEqual(2, count_c3.Products.Count()); Assert.AreEqual(6, count_c2.Products.Count()); Assert.AreEqual(4, count_c8.Products.Count()); }
public PartialViewResult SearchBox(string category, int page = 1, string searchText = null) { category = category == "All" ? null : category; ProductsListViewModel model; //Search with category & text if (category != null && !string.IsNullOrEmpty(searchText)) { model = new ProductsListViewModel { Products = repository.Products.Where (p => (p.Category == category) && (p.Name.ToUpper()).Contains(searchText.ToUpper())) .OrderBy(p => p.ProductID).Skip((page - 1) * PageSize).Take(PageSize), PagingInfo = new PageInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = repository.Products.Where(p => (p.Category == category) && (p.Name.ToUpper()).Contains(searchText.ToUpper())).Count() }, CurrentCategory = category }; } // search with category & text empty else if (category != null && string.IsNullOrEmpty(searchText)) { model = new ProductsListViewModel { Products = repository.Products.Where(p => p.Category == category).OrderBy(p => p.ProductID).Skip((page - 1) * PageSize).Take(PageSize), PagingInfo = new PageInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = repository.Products.Where(e => e.Category == category).Count() }, CurrentCategory = category }; } // search with category null & text else if (category == null && !string.IsNullOrEmpty(searchText)) { model = new ProductsListViewModel { Products = repository.Products.Where (p => (p.Name.ToUpper()).Contains(searchText.ToUpper())) .OrderBy(p => p.ProductID).Skip((page - 1) * PageSize).Take(PageSize), PagingInfo = new PageInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = repository.Products.Where(p => (p.Name.ToUpper()).Contains(searchText.ToUpper())).Count() }, CurrentCategory = category }; } // search with category null & text empty else { model = new ProductsListViewModel { Products = repository.Products.OrderBy(p => p.ProductID).Skip((page - 1) * PageSize).Take(PageSize), PagingInfo = new PageInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = repository.Products.Count() }, CurrentCategory = category }; } ViewBag.ActionName = "Search"; return(PartialView("List", model)); #region old //if (!string.IsNullOrEmpty(searchText)) //{ // model = new ProductsListViewModel // { // Products = repository.Products.Where // (p => // ( // category != null ? // (p.Category == category) && (p.Name.ToUpper()).Contains(searchText.ToUpper()) // : // ((p.Name.ToUpper()).Contains(searchText.ToUpper())) // ) // ) // .OrderBy(p => p.ProductID).Skip((page - 1) * PageSize).Take(PageSize), // PagingInfo = new PageInfo // { // CurrentPage = page, // ItemsPerPage = PageSize, // TotalItems = category == null ? repository.Products.Count() : repository.Products.Where(e => e.Category == category).Count() // }, // CurrentCategory = category // }; //} //else //{ // model = new ProductsListViewModel // { // Products = repository.Products.Where(p => category == null || p.Category == category).OrderBy(p => p.ProductID).Skip((page - 1) * PageSize).Take(PageSize), // PagingInfo = new PageInfo // { // CurrentPage = page, // ItemsPerPage = PageSize, // TotalItems = (category == null) ? repository.Products.Count() : repository.Products.Where(e => e.Category == category).Count() // }, // CurrentCategory = (category == null) ? null : category // }; //} #endregion }
public ViewResult List(string category) { string _category = category; IEnumerable <Product> products = null; string currCategory = ""; string categoryDescr = ""; if (string.Equals("Computers", category, StringComparison.OrdinalIgnoreCase)) { products = _allProducts.Products.Where(i => i.Category.categoryName.Equals("Комп'ютери")).OrderBy(i => i.id); currCategory = "Комп'ютери"; } else { if (string.Equals("Laptops", category, StringComparison.OrdinalIgnoreCase)) { products = _allProducts.Products.Where(i => i.Category.categoryName.Equals("Ноутбуки")).OrderBy(i => i.id); currCategory = "Ноутбуки"; } else { if (string.Equals("Smartphones", category, StringComparison.OrdinalIgnoreCase)) { products = _allProducts.Products.Where(i => i.Category.categoryName.Equals("Смартфони")).OrderBy(i => i.id); currCategory = "Смартфони"; } else { if (string.Equals("Tablets", category, StringComparison.OrdinalIgnoreCase)) { products = _allProducts.Products.Where(i => i.Category.categoryName.Equals("Планшети")).OrderBy(i => i.id); currCategory = "Планшети"; } else { if (string.Equals("TVs", category, StringComparison.OrdinalIgnoreCase)) { products = _allProducts.Products.Where(i => i.Category.categoryName.Equals("Телевізори")).OrderBy(i => i.id); currCategory = "Телевізори"; } else { if (string.Equals("Cameras", category, StringComparison.OrdinalIgnoreCase)) { products = _allProducts.Products.Where(i => i.Category.categoryName.Equals("Фотоапарати")).OrderBy(i => i.id); currCategory = "Фотоапарати"; } else { if (string.Equals("Videocameras", category, StringComparison.OrdinalIgnoreCase)) { products = _allProducts.Products.Where(i => i.Category.categoryName.Equals("Відеокамери")).OrderBy(i => i.id); currCategory = "Відеокамери"; } } } } } } } categoryDescr = _allCategories.AllCategories.FirstOrDefault(i => i.categoryName.Equals(currCategory)).description; var productObj = new ProductsListViewModel { allProducts = products, currCategory = currCategory, categoryDescr = categoryDescr }; ViewBag.Title = currCategory; return(View(productObj)); }
public JsonResult ListJson(ProductsListViewModel model) { return(Json(model.Products, JsonRequestBehavior.AllowGet)); }