public async Task <IActionResult> GetUsers([FromQuery] PagingParams pagingParams) { IPagedList <User> usersPaged; if (User.Identity.IsAuthenticated) { var currentUser = await _context.Users.FindAsync(User.FindFirst(JwtRegisteredClaimNames.Jti).Value); var isAdmin = await _userManager.IsInRoleAsync(currentUser, "Admin"); usersPaged = await _context.Users.Include(p => p.Photos).Include(u => u.LikesFrom) .OrderByDescending(u => u.LastActive) .Where(u => u.Id != currentUser.Id) .ToPagedListAsync(pagingParams.PageNumber, pagingParams.PageSize); } else { usersPaged = await _context.Users.Include(p => p.Photos) .OrderByDescending(u => u.LastActive) .ToPagedListAsync(pagingParams.PageNumber, pagingParams.PageSize); } var users = _mapper.Map <IEnumerable <UserListDTO> >(usersPaged); var usersPagedList = _mapper.Map <PagedList>(usersPaged); var model = new UserPagedListDTO { Users = users, Pagination = usersPagedList }; return(Ok(model)); }
public async Task <IActionResult> SearchUsers([FromQuery] SearchParams searchParams, [FromQuery] PagingParams pagingParams) { IPagedList <User> usersPaged; var minBirthDate = DateTime.Today.AddYears(-searchParams.MaxAge - 1); var maxBirthDate = DateTime.Today.AddYears(-searchParams.MinAge); if (User.Identity.IsAuthenticated) { var currentUser = await _context.Users.FindAsync(User.FindFirst(JwtRegisteredClaimNames.Jti).Value); usersPaged = await _context.Users.Include(p => p.Photos).Include(u => u.LikesFrom) .OrderByDescending(u => searchParams.OrderBy == "createdOn" ? u.CreatedOn : (searchParams.OrderBy == "Age" ? u.BirthDate : u.LastActive)) .Where(u => u.Id != currentUser.Id && u.BirthDate >= minBirthDate && u.BirthDate <= maxBirthDate) .Where(g => searchParams.Gender == 0 ? true : (searchParams.Gender == 1 ? g.Gender == "رجل" : g.Gender == "إمرأة")) .Where(n => searchParams.Name == null ? true : n.NickName.Contains(searchParams.Name)) .ToPagedListAsync(pagingParams.PageNumber, pagingParams.PageSize); } else { usersPaged = await _context.Users.Include(p => p.Photos) .OrderByDescending(u => searchParams.OrderBy == "createdOn" ? u.CreatedOn : (searchParams.OrderBy == "Age" ? u.BirthDate : u.LastActive)) .Where(u => u.BirthDate >= minBirthDate && u.BirthDate <= maxBirthDate) .Where(g => searchParams.Gender == 0 ? true : (searchParams.Gender == 1 ? g.Gender == "رجل" : g.Gender == "إمرأة")) .Where(n => searchParams.Name == null ? true : n.NickName.Contains(searchParams.Name)) .ToPagedListAsync(pagingParams.PageNumber, pagingParams.PageSize); } var users = _mapper.Map <IEnumerable <UserListDTO> >(usersPaged); var usersPagedList = _mapper.Map <PagedList>(usersPaged); var model = new UserPagedListDTO { Users = users, Pagination = usersPagedList }; return(Ok(model)); }