Пример #1
0
        public async Task <IActionResult> GetCommentsForPost([FromQuery] CommentResourceParameters commentResourceParameters, Guid postId)
        {
            if (!_propertyMapping.ValidMappingExist <CommentOutputDto, Comment>(commentResourceParameters.OrderBy))
            {
                return(BadRequest());
            }
            if (!_propertyValidation.HasValidProperties <CommentOutputDto>(commentResourceParameters.Fields))
            {
                return(BadRequest());
            }

            var commentsFromRepo = _commentRepository.GetCommentsForPost(postId, commentResourceParameters);

            var previousPageLink = commentsFromRepo.HasPrevious ? CreateCommentsUri(commentResourceParameters, UriType.PreviousPage, "GetCommentsForPost") : null;
            var nextPageLink     = commentsFromRepo.HasNext ? CreateCommentsUri(commentResourceParameters, UriType.NextPage, "GetCommentsForPost") : null;

            var metaData = new
            {
                totalCount  = commentsFromRepo.TotalCount,
                pageSize    = commentsFromRepo.PageSize,
                currentPage = commentsFromRepo.CurrentPage,
                totalPages  = commentsFromRepo.TotalPages,
                previousPageLink,
                nextPageLink,
            };

            Response.Headers.Add("Pagination", JsonSerializer.Serialize(metaData));

            return(Ok(_mapper.Map <IEnumerable <CommentOutputDto> >(commentsFromRepo).ShapeData(commentResourceParameters.Fields)));
        }
Пример #2
0
        private string CreateCommentsUri(CommentResourceParameters commentResourceParameters, UriType uriType, string routeName)
        {
            switch (uriType)
            {
            case UriType.PreviousPage:
                return(Url.Link(routeName, new
                {
                    pageNumber = commentResourceParameters.PageNumber - 1,
                    commentResourceParameters.PageSize,
                    commentResourceParameters.OrderBy,
                    commentResourceParameters.Fields
                }));

            case UriType.NextPage:
                return(Url.Link(routeName, new
                {
                    pageNumber = commentResourceParameters.PageNumber + 1,
                    commentResourceParameters.PageSize,
                    commentResourceParameters.OrderBy,
                    commentResourceParameters.Fields
                }));

            default:
                return(Url.Link(routeName, new
                {
                    commentResourceParameters.PageNumber,
                    commentResourceParameters.PageSize,
                    commentResourceParameters.OrderBy,
                    commentResourceParameters.Fields
                }));
            }
        }
Пример #3
0
        public PageList <Comment> GetCommentsForPost(Guid postId, CommentResourceParameters commentResourceParameters)
        {
            if (postId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(postId));
            }


            var collection = _context.Comments as IQueryable <Comment>;

            // No SearchingQuery Needed

            collection = collection.Include(c => c.Author).Where(c => c.BelongPostId == postId);

            if (!string.IsNullOrWhiteSpace(commentResourceParameters.OrderBy))
            {
                var commentPropertyMappingDictionary = _propertyMapping.GetPropertyMapping <PostOutputDto, Post>();
                collection = collection.ApplySort(commentResourceParameters.OrderBy, commentPropertyMappingDictionary);
            }

            return(PageList <Comment> .Create(collection, commentResourceParameters.PageNumber, commentResourceParameters.PageSize));
        }