public void List_ProductList_SutablePage() { 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" } }); var sut = new ProductController(mock.Object); sut.PageSize = 2; //Action method returns ViewResult.Model WebUI.Models.ProductListViewModel result = (ProductListViewModel)sut.List(null, 1).Model; Product[] prodArray = result.Products.ToArray(); Assert.IsTrue(prodArray.Length == 2); Assert.AreEqual(prodArray[0].Name, "P1"); Assert.AreEqual(prodArray[1].Name, "P2"); }
public void Can_Filter_Products() { Mock <IProductRepository> mock = new Mock <IProductRepository>(); mock.Setup(m => m.Products).Returns(new Product[] { new Product { ProductID = 1, Name = "P1", Category = "Football" }, new Product { ProductID = 2, Name = "P2", Category = "PingPong" }, new Product { ProductID = 3, Name = "P3", Category = "Football" }, new Product { ProductID = 4, Name = "P4", Category = "PingPong" }, new Product { ProductID = 5, Name = "P5", Category = "PingPong" } }); var sut = new ProductController(mock.Object); sut.PageSize = 4; //Action method returns ViewResult.Model WebUI.Models.ProductListViewModel result = (ProductListViewModel)sut.List("Football", 1).Model; Product[] prodArray = result.Products.ToArray(); Assert.IsTrue(prodArray.Length == 2); Assert.IsTrue(prodArray[0].Name == "P1" && prodArray[0].Category == "Football"); Assert.IsTrue(prodArray[1].Name == "P3" && prodArray[0].Category == "Football"); }
public ActionResult List(string category, int page = 1) { //var products = repository.VProductAndDescriptions.Distinct(); var products = repository.Products; if (!string.IsNullOrEmpty(category)) { products = products.Where(x => x.ProductCategory.Name == category); //products = products.Join(repository.Products.Where(x=>x.ProductCategory.Name == category), x=>x.ProductID,y=>y.ProductID, (x,y)=>x).AsQueryable(); } //var t = products.Count(); var viewProducts = products.Select(x => new VProductAndDescription { ProductID = x.ProductID, Name = x.Name, Description = x.Name, ProductModel = x.ProductModel.Name, Culture = x.Name }); ProductListViewModel result = new ProductListViewModel { CurrentCategory = category, Products = viewProducts. OrderBy(x => x.Name).Skip((page - 1) * PageSize).Take(PageSize), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = products.Count() } }; return View(result); }
public ActionResult Products(int id, int page = 1) { int PageSize = 3; //ICategoryRepository categoryRepo = new CategoryRepository(); //IProductRepository productRepo = new ProductRepository(); IList<Product> products = productRepo.GetProductsByCategory(id); ProductListViewModel productList = new ProductListViewModel(); productList.Products = products.OrderBy(p => p.Id).Skip((page - 1) * PageSize).Take(PageSize); ; productList.CategoryName = categoryRepo.GetAllCategories().First(cat => cat.Id == id).Name; PageInfo pagingInfo = new PageInfo(); pagingInfo.TotalProducts = products.Count; pagingInfo.PageSize = PageSize; pagingInfo.CurrentPage = page; productList.PagingInfo = pagingInfo; return View(productList); }
public ViewResult List(string category, int page=1) { var viewModel = new ProductListViewModel { 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.Count(e => e.Category == category) }, CurrentCategory = category }; return View(viewModel); }