Ejemplo n.º 1
0
        public ActionResult UsersList(UsersListViewModel model)
        {
            var viewModel = this.GetUsersListViewModel(model.CurrentPageNumber,
                model.SearchLogin, model.SearchName, model.SearchSurname);

            return View(viewModel);
        }
Ejemplo n.º 2
0
        private UsersListViewModel GetUsersListViewModel(int pageNumber,
            string searchLogin, string searchName, string searchSurname)
        {
            int itemsInPage = 10;

            var list = _personService.GetList();

            {
                if (!string.IsNullOrEmpty(searchLogin))
                {
                    list = list.Where(x => x.Login.Contains(searchLogin)).ToList();
                }

                if (!string.IsNullOrEmpty(searchName))
                {
                    list = list.Where(x => x.Name.Contains(searchName)).ToList();
                }

                if (!string.IsNullOrEmpty(searchSurname))
                {
                    list = list.Where(x => x.SecondName.Contains(searchSurname)).ToList();
                }
            }

            if (list.Count == 0)
            {
                return new UsersListViewModel()
                {
                    SearchResult = false
                };
            }

            int startRange = pageNumber * 10 - itemsInPage;
            int allPageCount = list.Count / itemsInPage;
            int ost = list.Count % itemsInPage;
            if (ost != 0) { allPageCount++; }

            int selectCount = ((pageNumber >= allPageCount && ost != 0) ? ost : itemsInPage);

            if (list.Count != 0)
            {
                list = list.OrderBy(x => x.Login).ToList();
                list = list.GetRange(startRange, selectCount);
            }

            var viewModel = new UsersListViewModel()
            {
                Users = list.Select(
                    user => new UserInfoViewModel()
                    {
                        Id = user.Id,
                        Email = user.Login,
                        Name = user.Name,
                        SecondName = user.SecondName,
                        LastName = user.LastName,
                        IsBanned =  user.IsBanned
                    }).ToList(),

                CurrentPageNumber = pageNumber,
                AllPageCount = allPageCount,
                ItemsPerPage = itemsInPage,
                SearchLogin = searchLogin,
                SearchName = searchName,
                SearchSurname = searchSurname,
                SearchResult = true
            };

            return viewModel;
        }