예제 #1
0
        public IActionResult GetAuthors(AuthorsResourceParameters resourceParameters)
        {
            var authors = repository.GetAuthors(resourceParameters);
            IEnumerable <AuthorDto> authorsDto = Mapper.Map <IEnumerable <AuthorDto> >(authors);

            if (!this.typeHelperService.TypeHasProperties <AuthorDto>(resourceParameters.Fields))
            {
                return(BadRequest());
            }

            var previousPageLink = authors.HasPrevious ?
                                   CreateAuthorsResourceUri(resourceParameters,
                                                            ResourceUriType.PreviousPage) : null;

            var nextPageLink = authors.HasNext ?
                               CreateAuthorsResourceUri(resourceParameters,
                                                        ResourceUriType.NextPage) : null;

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

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

            return(Ok(authorsDto.ShapeData(resourceParameters.Fields)));
        }
        public IActionResult GetAuthorCollection([ModelBinder(BinderType = typeof(ArrayModelBinder))] IEnumerable <int> ids)
        {
            if (ids == null)
            {
                return(BadRequest());
            }

            var authorEntities = repository.GetAuthors(ids);

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

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

            return(Ok(authorsToReturn));
        }