Exemplo n.º 1
0
        public IActionResult GetCommentariesForTopic(Guid topicId, CommentariesResourceParameters commentariesResourceParameters)
        {
            var commentaryEntities = noterRepository.GetCommentariesForTopic(topicId, commentariesResourceParameters);
            var commentaryDtos     = Mapper.Map <IEnumerable <CommentaryDto> >(commentaryEntities);

            var previousPageLink = commentaryEntities.HasPrevious ?
                                   CreateCommentariesResourceUri(commentariesResourceParameters, ResourceUriType.PreviousPage)
                : null;

            var nextPageLink = commentaryEntities.HasNext ?
                               CreateCommentariesResourceUri(commentariesResourceParameters, ResourceUriType.NextPage)
                : null;

            var paginationMetadata = new
            {
                totalCount   = commentaryEntities.TotalCount,
                pageSize     = commentaryEntities.PageSize,
                currentPage  = commentaryEntities.CurrentPage,
                totalPages   = commentaryEntities.TotalPages,
                previousPage = previousPageLink,
                nextPage     = nextPageLink
            };

            Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata));

            return(Ok(commentaryDtos));
        }
Exemplo n.º 2
0
        private string CreateCommentariesResourceUri(CommentariesResourceParameters commentariesResourceParameters, ResourceUriType resourceUriType)
        {
            switch (resourceUriType)
            {
            case ResourceUriType.PreviousPage: return(urlHelper.Link("GetCommentariesForTopic",
                                                                     new
                {
                    pageNumber = commentariesResourceParameters.PageNumber - 1,
                    pageSize = commentariesResourceParameters.PageSize
                }));

            case ResourceUriType.NextPage: return(urlHelper.Link("GetCommentariesForTopic",
                                                                 new
                {
                    pageNumber = commentariesResourceParameters.PageNumber + 1,
                    pageSize = commentariesResourceParameters.PageSize
                }));

            default:
                return(urlHelper.Link("GetCommentariesForTopic",
                                      new
                {
                    pageNumber = commentariesResourceParameters.PageNumber,
                    pageSize = commentariesResourceParameters.PageSize
                }));
            }
        }
Exemplo n.º 3
0
        public PagedList <Commentary> GetCommentariesForTopic(Guid topicId, CommentariesResourceParameters commentariesResourceParameters)
        {
            var query = context.Commentaries.Where(e => e.TopicId == topicId);

            return(PagedList <Commentary> .Create(query, commentariesResourceParameters.PageNumber, commentariesResourceParameters.PageSize));
        }