예제 #1
0
        public IActionResult Get([FromQuery] AuthorRequestDto authorRequestDto)
        {
            var authorsFromRepo    = _restApiService.GetAuthors(authorRequestDto);
            var paginationMetadata = new
            {
                totalCount  = authorsFromRepo.TotalCount,
                pageSize    = authorsFromRepo.PageSize,
                currentPage = authorsFromRepo.CurrentPage,
                totalPages  = authorsFromRepo.TotalPages,
            };

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

            var links          = CreateLinksForAuthors(authorRequestDto, authorsFromRepo.HasNext, authorsFromRepo.HasPrevious);
            var authorToReturn = _mapper.Map <IEnumerable <AuthorDto> >(authorsFromRepo).ShapeData();

            var shapedAuthorsWithLinks = authorToReturn.Select(author =>
            {
                var authorAsDictionary = author as IDictionary <string, object>;
                var authorLinks        = CreateLinksForAuthor((Guid)authorAsDictionary["Id"]);
                authorAsDictionary.Add("links", authorLinks);
                return(authorAsDictionary);
            });

            var linkedCollectionResource = new
            {
                value = shapedAuthorsWithLinks,
                links
            };


            return(Ok(linkedCollectionResource));
        }