public ViewResult List(int page = 1) { _repository.UserName = User?.Identity?.Name; var terminalsModel = new TerminalsListViewModel { Terminals = new List <ViewTerminal>(), PagingInfo = new PagingInfo { CurrentPage = 1, ItemsPerPage = PageSize, TotalItems = 0 } }; //if (!DbHelper.UserInRole(_repository.UserName, Constants.RightReadName, null)) // return View(terminalsModel); int maxPages; var terminals = _repository.Terminals as IList <Terminal> ?? _repository.Terminals.ToList(); var totalItems = terminals.Count; if (totalItems <= 0) { maxPages = 1; } else { maxPages = (int)Math.Ceiling((decimal)totalItems / PageSize); } terminalsModel.Terminals = from terminal in terminals.OrderBy(t => t.Id).Skip((page - 1) * PageSize).Take(PageSize) select new ViewTerminal(terminal) { GroupsIdsString = terminal.IdGroup > 0 ? terminal.IdGroup.ToString() : " - ", GroupsNamesString = terminal.Group?.Name ?? " - " }; terminalsModel.PagingInfo = new PagingInfo { CurrentPage = page > maxPages ? maxPages : page, ItemsPerPage = PageSize, TotalItems = totalItems }; ViewBag.CurrentController = GetType().ToString(); return(View(terminalsModel)); }
public void Can_Send_Pagination_View_Model() { // Организация (arrange) Mock <ITerminalRepository> mock = new Mock <ITerminalRepository>(); mock.Setup(m => m.Terminals).Returns(new List <Terminal> { new Terminal { Id = 1, Name = "Терм1", Address = "Аддр1", IdHasp = "1", Group = new Group() }, new Terminal { Id = 2, Name = "Терм2", Address = "Аддр2", IdHasp = "2", Group = new Group() }, new Terminal { Id = 3, Name = "Терм3", Address = "Аддр3", IdHasp = "3", Group = new Group() }, new Terminal { Id = 4, Name = "Терм4", Address = "Аддр4", IdHasp = "4", Group = new Group() }, new Terminal { Id = 5, Name = "Терм5", Address = "Аддр5", IdHasp = "5", Group = new Group() } }); TerminalMonitoringController controller = new TerminalMonitoringController(mock.Object); controller.PageSize = 3; // Действие (act) TerminalsListViewModel result = (TerminalsListViewModel)controller.List(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 <ITerminalRepository> mock = new Mock <ITerminalRepository>(); mock.Setup(m => m.Terminals).Returns(new List <Terminal> { new Terminal { Id = 1, Name = "Терм1", Address = "Аддр1", IdHasp = "1", Group = new Group() }, new Terminal { Id = 2, Name = "Терм2", Address = "Аддр2", IdHasp = "2", Group = new Group() }, new Terminal { Id = 3, Name = "Терм3", Address = "Аддр3", IdHasp = "3", Group = new Group() }, new Terminal { Id = 4, Name = "Терм4", Address = "Аддр4", IdHasp = "4", Group = new Group() }, new Terminal { Id = 5, Name = "Терм5", Address = "Аддр5", IdHasp = "5", Group = new Group() } }); TerminalMonitoringController controller = new TerminalMonitoringController(mock.Object); controller.PageSize = 3; // Действие (act) TerminalsListViewModel result = (TerminalsListViewModel)controller.List(2).Model; // Утверждение (assert) List <ViewTerminal> terminals = result.Terminals.ToList(); Assert.IsTrue(terminals.Count == 2); Assert.AreEqual(terminals[0].Name, "Терм4"); Assert.AreEqual(terminals[1].Name, "Терм5"); }