示例#1
0
        public ActionResult <List <ReadPostModel> > Get(Guid userId, bool selfPosts, int?pageIndex, int?pageSize, string orderBy, Guid?interestId)
        {
            if (pageIndex == null || pageSize == null)
            {
                return(UnprocessableEntity());
            }

            Ordering <Post> ordering = null;

            if (orderBy != null)
            {
                if (orderBy.Equals("popularity"))
                {
                    ordering = Ordering <Post> .CreateDesc(p => p.Likes.Count);
                }
                else if (orderBy.Equals("freshness"))
                {
                    ordering = Ordering <Post> .CreateDesc(p => p.CreatedAt);
                }
            }

            if (null == ordering && selfPosts)
            {
                ordering = Ordering <Post> .CreateDesc(p => p.CreatedAt);
            }

            var posts = _unitOfWork.GetPostsForUser(userId, selfPosts, (int)pageIndex, (int)pageSize, ordering, interestId).ToList();

            if (orderBy != null && orderBy.Equals("favorite"))
            {
                posts = _unitOfWork.Posts.GetFavoritePosts(userId, (int)pageIndex, (int)pageSize).ToList();
            }

            var readPosts = _mapper.Map <List <ReadPostModel> >(posts);

            for (int i = 0; i < readPosts.Count; i++)
            {
                readPosts[i].Interests  = _mapper.Map <List <ReadInterestModel> >(posts[i].PostInterests);
                readPosts[i].LikeId     = _unitOfWork.Likes.GetLikeIdOfPostForUser(posts[i].Id, userId);
                readPosts[i].FavoriteId = _unitOfWork.Favorites.GetFavoriteIdOfPostForUser(posts[i].Id, userId);
                readPosts[i].LikesCount = _unitOfWork.Posts.GetLikesCountOfPost(posts[i].Id);
            }

            return(Ok(readPosts));
        }