Exemplo n.º 1
0
        public IActionResult GetAuthors(
            [FromQuery] AuthorsResourceParameters authorsResourceParameters)
        {
            if (!_propertyMappingService.ValidMappingExistingFor <AuthorDto, Author>
                    (authorsResourceParameters.OrderBy))
            {
                return(BadRequest());
            }

            if (!_propertycheckerService.TypeHasProperties <AuthorDto>
                    (authorsResourceParameters.Fields))
            {
                return(BadRequest());
            }

            var authorsFromRep = _bookLibraryRepository.
                                 GetAuthors(authorsResourceParameters);



            var paginationMetadata = new
            {
                totalCount  = authorsFromRep.TotalCount,
                pageSize    = authorsFromRep.PageSize,
                currentPage = authorsFromRep.CurrentPage,
                totalPages  = authorsFromRep.TotalPages
            };

            Response.Headers.Add("X-Pagination",
                                 JsonSerializer.Serialize(paginationMetadata));

            var links = CreateLinksForAuthors(authorsResourceParameters,
                                              authorsFromRep.HasNext, authorsFromRep.HasPrevious);

            var shapedAuthors = _mapper.Map <IEnumerable <AuthorDto> >(authorsFromRep)
                                .ShapeData(authorsResourceParameters.Fields);

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

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

            return(Ok(linkedCollectionResource));
        }
        public ActionResult <IEnumerable <AuthorDto> > GetAuthorCollection(
            [FromRoute]
            [ModelBinder(binderType: typeof(ArrayModelBinder))] IEnumerable <Guid> ids)
        {
            if (ids == null)
            {
                return(BadRequest());
            }

            var authorEntities = _bookLibraryRepository.GetAuthors(ids);

            if (authorEntities.Count() != ids.Count())
            {
                return(NotFound());
            }

            var authorsToReturn = _mapper.Map <IEnumerable <AuthorDto> >(authorEntities);

            return(Ok(authorsToReturn));
        }