public ActionResult SearchLot(string search, int page = 1) { if (search == null) { search = ""; } if (!ModelState.IsValid) { return(RedirectToAction("List", "Lots")); } var allLots = lotsRepository.Lots.Where(p => p.Name.Contains(search) && p.IsCompleted == false); var count = allLots.Count(); var lots = allLots.OrderBy(p => p.LotID) .Skip((page - 1) * PageSize) .Take(PageSize); LotsListViewModel model = new LotsListViewModel { Lots = lots, PageModel = new PageModel { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = count }, CurrentCategoryName = search, }; return(View(model)); }
public ViewResult List(int?categoryId, int page = 1) { var selectCategory = categoriesRepository.Categories.FirstOrDefault(x => x.CategoryId == categoryId); IEnumerable <Lot> categoryLots; if (selectCategory == null) { categoryLots = lotsRepository.Lots; } else { categoryLots = selectCategory.Lots; } LotsListViewModel model = new LotsListViewModel { Lots = categoryLots.OrderBy(p => p.LotID) .Skip((page - 1) * PageSize) .Take(PageSize), PageModel = new PageModel { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = categoryLots.Count() }, CurrentCategoryName = selectCategory == null ? "All" : selectCategory.CategoryName, CurrentCategoryId = selectCategory == null ? null : (int?)selectCategory.CategoryId }; return(View(model)); }
public void Paginate_Tes() { // Arrange Mock <ICategoriesRepository> category = new Mock <ICategoriesRepository>(); category.Setup(m => m.Categories).Returns(new[] { new Category { CategoryId = 1, CategoryName = "Cat1", Lots = new List <Lot>() }, new Category { CategoryId = 2, CategoryName = "Cat2", Lots = new List <Lot>() }, new Category { CategoryId = 3, CategoryName = "Cat3", Lots = new List <Lot>() } }.AsQueryable()); Mock <ILotsRepository> mock = new Mock <ILotsRepository>(); mock.Setup(m => m.Lots).Returns(new[] { new Lot { LotID = 1, Name = "P1", IsCompleted = false }, new Lot { LotID = 2, Name = "P2", IsCompleted = false }, new Lot { LotID = 3, Name = "P3", IsCompleted = false }, new Lot { LotID = 4, Name = "P4", IsCompleted = false }, }.AsQueryable()); LotsController controller = new LotsController(mock.Object, category.Object) { PageSize = 3 }; // Act LotsListViewModel result = (LotsListViewModel)controller.List(null, 2).Model; //Assert Lot[] prodArray = result.Lots.ToArray(); Assert.IsTrue(prodArray.Length == 1); Assert.AreEqual(prodArray[0].Name, "P4"); }
public ViewResult Index(int? current_category, int? current_city, string part_name) { LotRepository lotRep = new LotRepository(); Expression<Func<lot, bool>> filter = x => (x.cityID == current_city && current_city != null || current_city == null) && (x.categoryID == current_category && current_category != null || current_category == null) && (x.name.IndexOf(part_name.Trim()) != -1 && part_name.Trim() != "" || part_name.Trim() == "") && x.statusID == (int)Status.approve; List<lot> lots = lotRep.Get(filter).ToList(); DropDownInit(); LotsListViewModel model = new LotsListViewModel { lots = lots }; return View(model); }
public ViewResult Index(string category, string searchString, int page = 1) { IEnumerable <LotEntity> lotsForSale = _lotService.GetAllForSaleLotEntities().Where(l => searchString == null || Regex.IsMatch(l.Name, searchString, RegexOptions.IgnoreCase)); IEnumerable <LotViewModel> sortedLots = lotsForSale.Where(l => category == null || l.Category.Name == category).Select(l => l.ToMvcLot()).OrderBy(l => l.StartDate) .Reverse().Skip((page - 1) * pageSize).Take(pageSize); LotsListViewModel model = new LotsListViewModel { Lots = sortedLots, PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = pageSize, TotalItems = category == null?lotsForSale.Count() : lotsForSale.Where(l => l.Category.Name == category).Count() }, CurrentCategory = category, CurrentSearchString = searchString }; return(View(model)); }
public ViewResult List(string category, int page = 1) { LotsListViewModel model = new LotsListViewModel { Lots = repository.Lots .Where(p => category == null || p.Category == category) .OrderBy(p => p.LotID) .Skip((page - 1) * PageSize) .Take(PageSize), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = category == null? repository.Lots.Count() : repository.Lots.Where(e => e.Category == category).Count() }, CurrentCategory = category }; return(View(model)); }
public ViewResult Index() { try { LotRepository lotRep = new LotRepository(); Expression<Func<lot, bool>> filter = x => x.statusID == (int)Status.approve; List<lot> lots = lotRep.Get(filter).ToList(); DropDownInit(); LotsListViewModel model = new LotsListViewModel { lots = lots }; return View(model); } catch (Exception error) { return View(); } }
public void Can_Send_Pagination_View_Model() { // Arrange Mock <ILotRepository> mock = new Mock <ILotRepository>(); mock.Setup(m => m.Lots).Returns(new Lot[] { new Lot { LotID = 1, Name = "P1" }, new Lot { LotID = 2, Name = "P2" }, new Lot { LotID = 3, Name = "P3" }, new Lot { LotID = 4, Name = "P4" }, new Lot { LotID = 5, Name = "P5" } }.AsQueryable()); // Arrange LotController controller = new LotController(mock.Object); controller.PageSize = 3; // Act LotsListViewModel result = (LotsListViewModel)controller.List(null, 2).Model; // Assert PagingInfo pageInfo = result.PagingInfo; Assert.AreEqual(pageInfo.CurrentPage, 2); Assert.AreEqual(pageInfo.ItemsPerPage, 3); Assert.AreEqual(pageInfo.TotalItems, 5); Assert.AreEqual(pageInfo.TotalPages, 2); }
public void Can_Paginate() { // Arrange Mock <ILotRepository> mock = new Mock <ILotRepository>(); mock.Setup(m => m.Lots).Returns(new Lot[] { new Lot { LotID = 1, Name = "P1" }, new Lot { LotID = 2, Name = "P2" }, new Lot { LotID = 3, Name = "P3" }, new Lot { LotID = 4, Name = "P4" }, new Lot { LotID = 5, Name = "P5" } }.AsQueryable()); // create a controller and make the page size 3 items LotController controller = new LotController(mock.Object); controller.PageSize = 3; // Act LotsListViewModel result = (LotsListViewModel)controller.List(null, 2).Model; // Assert Lot[] prodArray = result.Lots.ToArray(); Assert.IsTrue(prodArray.Length == 2); Assert.AreEqual(prodArray[0].Name, "P4"); Assert.AreEqual(prodArray[1].Name, "P5"); }
public void ListView_Model() { // Arrange Mock <ICategoriesRepository> category = new Mock <ICategoriesRepository>(); category.Setup(m => m.Categories).Returns(new[] { new Category { CategoryId = 1, CategoryName = "Cat1", Lots = new List <Lot>() }, new Category { CategoryId = 2, CategoryName = "Cat2", Lots = new List <Lot>() }, new Category { CategoryId = 3, CategoryName = "Cat3", Lots = new List <Lot>() } }.AsQueryable()); Mock <ILotsRepository> mock = new Mock <ILotsRepository>(); mock.Setup(m => m.Lots).Returns(new Lot[] { new Lot { LotID = 1, Name = "P1" }, new Lot { LotID = 2, Name = "P2" }, new Lot { LotID = 3, Name = "P3" }, new Lot { LotID = 4, Name = "P4" }, new Lot { LotID = 5, Name = "P5" } }.AsQueryable()); LotsController controller = new LotsController(mock.Object, category.Object); controller.PageSize = 3; // Act LotsListViewModel result = (LotsListViewModel)controller.List(null, 2).Model; // Assert PageModel pageInfo = result.PageModel; Assert.AreEqual(pageInfo.CurrentPage, 2); Assert.AreEqual(pageInfo.ItemsPerPage, 3); Assert.AreEqual(pageInfo.TotalItems, 5); Assert.AreEqual(pageInfo.TotalPages, 2); }