public async Task <PagedList <MemberDto> > GetMembersAsync(UserPagingParams pagingParams)
        {
            var query = _context.Users.AsQueryable();

            // Filter by Gender
            query = query.Where(x => x.UserName != pagingParams.CurrentUsername);
            query = query.Where(x => x.Gender == pagingParams.GenderToFilter);

            // Filter by Age
            var minDOB = DateTime.Today.AddYears(-pagingParams.MaxAge - 1);
            var maxDOB = DateTime.Today.AddYears(-pagingParams.MinAge);

            query = query.Where(x => x.DateOfBirth >= minDOB && x.DateOfBirth <= maxDOB);

            query = pagingParams.OrderBy switch
            {
                "created" => query.OrderByDescending(x => x.Created),
                _ => query.OrderByDescending(x => x.LastActive) // _ means default case
            };

            return(await PagedList <MemberDto> .CreateAsync(
                       query.ProjectTo <MemberDto>(_mapper.ConfigurationProvider).AsNoTracking(),
                       pagingParams.PageNumber,
                       pagingParams.PageSize));
        }
Exemplo n.º 2
0
        public async Task <ActionResult <IEnumerable <MemberDto> > > GetUsers([FromQuery] UserPagingParams pagingParams)
        {
            pagingParams.CurrentUsername = User.GetUserName();

            var gender = await _unitOfWork.UserRepository.GetUserGender(User.GetUserName());

            if (string.IsNullOrEmpty(pagingParams.GenderToFilter))
            {
                pagingParams.GenderToFilter = gender == "male" ? "female" : "male";
            }

            var users = await _unitOfWork.UserRepository.GetMembersAsync(pagingParams);

            Response.AddPaginationHeader(users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages);

            return(Ok(users));
        }
Exemplo n.º 3
0
        public async Task <PagedList <User> > GetUsers(UserPagingParams userPagingParams)
        {
            var users = _dataContext.Users.Include(p => p.Photos).OrderByDescending(u => u.LastActive).AsQueryable();

            users = users.Where(u => u.Id != userPagingParams.UserId);

            users = users.Where(u => u.Gender == userPagingParams.Gender);

            if (userPagingParams.Likers)
            {
                var userLikers = await GetUserLikes(userPagingParams.UserId, userPagingParams.Likers);

                users = users.Where(u => userLikers.Contains(u.Id));
            }
            if (userPagingParams.Likees)
            {
                var userLikees = await GetUserLikes(userPagingParams.UserId, userPagingParams.Likers);

                users = users.Where(u => userLikees.Contains(u.Id));
            }

            if (userPagingParams.MinAge != 18 || userPagingParams.MaxAge != 99)
            {
                var minDob = DateTime.Today.AddYears(-userPagingParams.MaxAge - 1);
                var maxDob = DateTime.Today.AddYears(-userPagingParams.MinAge);

                users = users.Where(u => u.DateOfBirth >= minDob && u.DateOfBirth <= maxDob);
            }

            if (!string.IsNullOrEmpty(userPagingParams.OrderBy))
            {
                switch (userPagingParams.OrderBy)
                {
                case "created":
                    users = users.OrderByDescending(u => u.Created);
                    break;

                default:
                    users = users.OrderByDescending(u => u.LastActive);
                    break;
                }
            }

            return(await PagedList <User> .CreateAsync(users, userPagingParams.PageNumber, userPagingParams.PageSize));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> GetUsers([FromQuery] UserPagingParams userPagingParams)
        {
            var currentUsrId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            var userFromRepo = await _repo.GetUser(currentUsrId);

            userPagingParams.UserId = currentUsrId;

            if (string.IsNullOrEmpty(userPagingParams.Gender))
            {
                userPagingParams.Gender = userFromRepo.Gender == "male" ? "female" : "male";
            }

            var users = await _repo.GetUsers(userPagingParams);

            var usersToReturn = _mapper.Map <IEnumerable <UserForListDto> >(users);



            Response.AddPagination(users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPage);

            return(Ok(usersToReturn));
        }