예제 #1
0
        public IActionResult GetPostComments(int id, [FromQuery] int page)
        {
            var pageSize = Convert.ToInt32(_config.GetSection("Pagination")["CommentPageSize"]);

            if (page < 1)
            {
                return(BadRequest("Requsted page must be greater than 0."));
            }

            var commentCount     = _context.CommentsCount.Where(c => c.MessageId == id).FirstOrDefault();
            int numberOfComments = commentCount.CommentCount != null ? (int)commentCount.CommentCount : 0;
            var pages            = numberOfComments / pageSize;

            if (numberOfComments % pageSize != 0)
            {
                pages++;
            }

            if (page > pages)
            {
                return(NotFound("The requested page does not exist."));
            }

            var rowsToSkip = (page - 1) * pageSize;
            var comments   = _context.Comment
                             .Where(
                x => _context.Comment
                .OrderBy(y => y.Id)
                .Select(y => y.Id)
                .Skip(rowsToSkip)
                .Take(pageSize)
                .Contains(x.Id) && x.MessageId == id
                )
                             .Include("Profile")
                             .ToList();

            var commentList = new List <CommentPresentation>();

            var profiles        = new Dictionary <string, PostProfilePresentation>();
            var commentProfiles = comments.Select(c => new { c.Profile.FullName, c.Profile.UserName, c.Profile.Picture }).Distinct();

            // Store all distinct usernames in dictionary
            foreach (var c in commentProfiles)
            {
                profiles.Add(c.UserName, new PostProfilePresentation(c.FullName, c.UserName, c.Picture));
            }

            foreach (var comment in comments)
            {
                var commentPresentation = new CommentPresentation(comment.Id, comment.Message, comment.Profile);
                commentList.Add(commentPresentation);
            }

            var commentFeed = new CommentFeed(commentList, page, pages);

            return(Ok(new { commentFeed, profiles }));
        }
        public static IList<Comment> GetAllCommentsPaging(PlusService service, string _activityId, int NumberOfPages, int ItemsPerPage, string NextPageToken)
        {
            //List all of the activities in the specified collection for the current user.  
            // Documentation: https://developers.google.com/+/api/latest/activities/list
            int count = 0;
            int iterate = NumberOfPages;
            CommentsResource.ListRequest listComments = service.Comments.List(_activityId);


            listComments.MaxResults = ItemsPerPage;

            CommentFeed commentsFeed = listComments.Execute();
            count++;
            IList<Comment> Comments = new List<Comment>();



            //// Loop through until we arrive at an empty page
            while (commentsFeed.Items != null || count <= iterate)
            {
                // Prepare the next page of results
                listComments.PageToken = commentsFeed.NextPageToken;


                // Adding each item  to the list.
                foreach (Comment item in commentsFeed.Items)
                {
                    Comments.Add(item);
                }

                // We will know we are on the last page when the next page token is
                // null.
                // If this is the case, break.
                if (commentsFeed.NextPageToken == null || count >= iterate)
                {
                    break;
                }


                // Execute and process the next page request
                commentsFeed = listComments.Execute();
                count++;
            }
            Comment token = new Comment();
            token.Id = commentsFeed.NextPageToken;
            Comments.Add(token);
            return Comments;
        }
        /// <summary>
        /// List all of the comments for an activity.
        /// Documentation:  https://developers.google.com/+/api/latest/comments/list
        /// </summary>
        /// <param name="service">a Valid authenticated PlusService</param>
        /// <param name="_activityId">The ID of the activity to get comments for.</param>
        /// <returns></returns>
        public static IList <Comment> GetAllComments(PlusService service, string _activityId)
        {
            //List all of the activities in the specified collection for the current user.
            // Documentation: https://developers.google.com/+/api/latest/activities/list
            CommentsResource.ListRequest listComments = service.Comments.List(_activityId);


            listComments.MaxResults = 100;
            CommentFeed     commentsFeed = listComments.Execute();
            IList <Comment> Comments     = new List <Comment>();



            //// Loop through until we arrive at an empty page
            while (commentsFeed.Items != null)
            {
                // Adding each item  to the list.
                foreach (Comment item in commentsFeed.Items)
                {
                    Comments.Add(item);
                }

                // We will know we are on the last page when the next page token is
                // null.
                // If this is the case, break.
                if (commentsFeed.NextPageToken == null)
                {
                    break;
                }

                // Prepare the next page of results
                listComments.PageToken = commentsFeed.NextPageToken;

                // Execute and process the next page request
                commentsFeed = listComments.Execute();
            }

            return(Comments);
        }