示例#1
0
        public PagedResult <PostHeader> GetPostsOfFollowedUsers(int currentUserId, PagerSpecification specification = null)
        {
            specification ??= new PagerSpecification();

            if (specification.PageSize <= 0)
            {
                specification.PageSize = 9;
            }

            if (specification.PageNumber <= 0)
            {
                specification.PageNumber = 1;
            }

            IQueryable <Post> query = DbContext.Posts;

            var postsQuery = query
                             .Where(p => p.User.Followers.Any(f => f.FollowerId == currentUserId))
                             .OrderByDescending(p => p.CreationDate)
                             .Select(PostHeaderSelector(currentUserId));

            int allResultsCount = postsQuery.Count();

            var posts = postsQuery.Skip((specification.PageNumber - 1) * specification.PageSize)
                        .Take(specification.PageSize)
                        .ToList();

            return(new PagedResult <PostHeader> {
                AllResultsCount = allResultsCount,
                PageNumber = specification.PageNumber,
                PageSize = specification.PageSize,
                Results = posts
            });
        }
示例#2
0
        public PagedResult <UserDetails> FindUsers(int currentUserId, string searchTerm, PagerSpecification specification = null)
        {
            specification ??= new PagerSpecification();

            if (specification.PageSize <= 0)
            {
                specification.PageSize = 9;
            }

            if (specification.PageNumber <= 0)
            {
                specification.PageNumber = 1;
            }

            IQueryable <User> query = DbContext.Users;

            var usersQuery = query
                             .Where(u => u.DisplayName.Contains(searchTerm))
                             .OrderBy(u => u.DisplayName)
                             .Select(u => new UserDetails
            {
                Id                      = u.Id,
                DisplayName             = u.DisplayName,
                HasImage                = u.HasImage,
                isFollowedByCurrentUser = u.Followers.Any(f => f.FollowerId == currentUserId),
                NumberOfFollowers       = u.Followers.Count()
            });

            int allResultsCount = usersQuery.Count();

            var users = usersQuery.Skip((specification.PageNumber - 1) * specification.PageSize)
                        .Take(specification.PageSize)
                        .ToList();

            return(new PagedResult <UserDetails>
            {
                AllResultsCount = allResultsCount,
                PageNumber = specification.PageNumber,
                PageSize = specification.PageSize,
                Results = users
            });
        }