public ActionResult Users(SearchViewModel model)
        {
            var users = GetUsers(model);

            if(users.PagingInfo.TotalItems==0)
                return RedirectToAction("NoUsers");

            if (users.PagingInfo.TotalPages == 1)
                users.PagingInfo.ButtonState = "hidden";
            else
                users.PagingInfo.ButtonState = "btn";

            return View(users);
        }
        //GET: MoreUsers
        public ActionResult MoreUsers(string[] search,int page)
        {
            if (!Request.IsAjaxRequest())
                return RedirectToAction("SearchingForFriends");

            var model = new SearchViewModel
            {
                FirstName = string.IsNullOrEmpty(search[0]) ? null : search[0],
                LastName=string.IsNullOrEmpty(search[1]) ? null : search[1],
                Country = string.IsNullOrEmpty(search[2]) ? null : search[2],
                City = string.IsNullOrEmpty(search[3]) ? null : search[3],
                Age=ToNullableInt(search[4]),
            };
            var users = GetUsers(model, page);

            return PartialView("UsersPartial", users);
        }
        private UsersViewModel GetUsers(SearchViewModel model,int page = 1)
        {
            var users = repository.ApplicationUsers;
            var currentUser = manager.FindById(User.Identity.GetUserId());

            if (model.FirstName != null)
                users = users.Where(u => u.FirstName == model.FirstName);

            if (model.LastName != null)
                users = users.Where(u => u.LastName == model.LastName);

            if (model.Age != null)
                users = users.Where(u => u.Age == model.Age);

            if (model.Country != null)
                users = users.Where(u => u.Country == model.Country);

            if (model.City != null)
                users = users.Where(u => u.City == model.City);

            var friendsId = repository.UserFriends.Where(u => u.UserId == currentUser.Id)
                            .Select(f => f.FriendId);

            var friends = repository.ApplicationUsers.Where(u => friendsId.Contains(u.Id) || u.Id == currentUser.Id).Select(u => u);

            users = users.Except(friends).OrderBy(u => u.Id);

            var foundUsers = new UsersViewModel
            {
                Users = users.Skip((page - 1) * UsersPerPage).Take(UsersPerPage),
                SearchModel=model,
                PagingInfo = new PagingInfo
                {
                    TotalPages = (int)Math.Ceiling((decimal)users.Count() / UsersPerPage),
                    TotalItems = users.Count(),
                }
            };
            TempData["InfoMessage"] = "Users " + foundUsers.PagingInfo.TotalItems;

            return foundUsers;
        }
Пример #4
0
        //GET: DisplayUsers
        public ActionResult DisplayUsers(SearchViewModel model,int page=1)
        {
            var users = repository.ApplicationUsers;
            var currentUser = manager.FindById(User.Identity.GetUserId());

            var adminsId = repository.UserRoles.Select(x => x.UserId);
            var admins = repository.ApplicationUsers.Where(u => adminsId.Contains(u.Id)).Select(u => u);
            users = users.Except(admins);

            if (model.FirstName != null)
                users = users.Where(u => u.FirstName == model.FirstName);

            if (model.LastName != null)
                users = users.Where(u => u.LastName == model.LastName);

            if (model.Age != null)
                users = users.Where(u => u.Age == model.Age);

            if (model.Country != null)
                users = users.Where(u => u.Country == model.Country);

            if (model.City != null)
                users = users.Where(u => u.City == model.City);

            if (users.Count() == 0)
                TempData["InfoMessage"] = "No user for this search";

            var foundUsers = new FriendsViewModel
            {
                Friends = users.OrderBy(u=>u.Id).Skip((page - 1) * UsersPerPage)
                .Take(UsersPerPage),

                PagingInfo = new PagingInfo
                {
                    TotalItems = users.Count(),
                    ItemsPerPage = UsersPerPage,
                    CurrentPage = page,
                }
            };

            return View(foundUsers);
        }