public Helpers.PagedList <Author> GetAuthors(Helpers.AuthorsResourceParameters parameters)
        {
            var collectionBeforePaging = _context.Authors.ApplySort(parameters.OrderBy,
                                                                    _propertyMappingService.GetPropertyMapping <Models.AuthorDto, Entities.Author>());

            if (!string.IsNullOrWhiteSpace(parameters.Genre))
            {
                // trim & ignore casing
                var genreForWhereClause = parameters.Genre
                                          .Trim().ToLowerInvariant();
                collectionBeforePaging = collectionBeforePaging
                                         .Where(a => a.Genre.ToLowerInvariant() == genreForWhereClause);
            }

            if (!string.IsNullOrEmpty(parameters.SearchQuery))
            {
                // trim & ignore casing
                var searchQueryForWhereClause = parameters.SearchQuery
                                                .Trim().ToLowerInvariant();

                collectionBeforePaging = collectionBeforePaging
                                         .Where(a => a.Genre.ToLowerInvariant().Contains(searchQueryForWhereClause) ||
                                                a.FirstName.ToLowerInvariant().Contains(searchQueryForWhereClause) ||
                                                a.LastName.ToLowerInvariant().Contains(searchQueryForWhereClause));
            }

            return(Helpers.PagedList <Author> .Create(collectionBeforePaging, parameters.PageNumber, parameters.PageSize));
        }
        public Helpers.PagedList <Author> GetAuthors(Helpers.AuthorsResourceParameters authorsResourceParameters)
        {
            //var collectionBeforePaging = context.Authors
            //    .OrderBy(a => a.FirstName)
            //    .ThenBy(a => a.LastName).AsQueryable();

            var collectionBeforePaging =
                context.Authors.ApplySort(authorsResourceParameters.OrderBy,
                                          propertyMappingService.GetPropertyMapping <Models.Author, Entities.Author>());

            if (!string.IsNullOrEmpty(authorsResourceParameters.Genre))
            {
                var genreForWhereClause = authorsResourceParameters.Genre.Trim().ToLowerInvariant();
                collectionBeforePaging = collectionBeforePaging.Where(a => a.Genre.ToLowerInvariant() == genreForWhereClause);
            }

            if (!string.IsNullOrEmpty(authorsResourceParameters.SearchQuery))
            {
                var searchQueryForWhereClause = authorsResourceParameters.SearchQuery.Trim().ToLowerInvariant();
                collectionBeforePaging = collectionBeforePaging.Where(a => a.Genre.ToLowerInvariant().Contains(searchQueryForWhereClause) ||
                                                                      a.FirstName.ToLowerInvariant().Contains(searchQueryForWhereClause) ||
                                                                      a.LastName.ToLowerInvariant().Contains(searchQueryForWhereClause));
            }

            return(Helpers.PagedList <Author> .Create(collectionBeforePaging, authorsResourceParameters.PageNumber, authorsResourceParameters.PageSize));
        }
        string CreateAuthorsResourceUri(
            Helpers.AuthorsResourceParameters authorsResourceParameters,
            Helpers.ResourceUriType type)
        {
            switch (type)
            {
            case Helpers.ResourceUriType.PreviousPage:
                return(_urlHelper.Link("GetAuthors",
                                       new
                {
                    fields = authorsResourceParameters.Fields,
                    orderBy = authorsResourceParameters.OrderBy,
                    genre = authorsResourceParameters.Genre,
                    searchQuery = authorsResourceParameters.SearchQuery,
                    pageNumber = authorsResourceParameters.PageNumber - 1,
                    pageSize = authorsResourceParameters.PageSize
                }));

            case Helpers.ResourceUriType.NextPage:
                return(_urlHelper.Link("GetAuthors",
                                       new
                {
                    fields = authorsResourceParameters.Fields,
                    orderBy = authorsResourceParameters.OrderBy,
                    genre = authorsResourceParameters.Genre,
                    searchQuery = authorsResourceParameters.SearchQuery,
                    pageNumber = authorsResourceParameters.PageNumber + 1,
                    pageSize = authorsResourceParameters.PageSize
                }));

            default:
                return(_urlHelper.Link("GetAuthors",
                                       new
                {
                    fields = authorsResourceParameters.Fields,
                    orderBy = authorsResourceParameters.OrderBy,
                    genre = authorsResourceParameters.Genre,
                    searchQuery = authorsResourceParameters.SearchQuery,
                    pageNumber = authorsResourceParameters.PageNumber,
                    pageSize = authorsResourceParameters.PageSize
                }));
            }
        }
Esempio n. 4
0
        public IActionResult GetAuthors(Helpers.AuthorsResourceParameters authorsResourceParameters)
        {
            if (!propertyMappingService.ValidMappingExistsFor <Models.Author, Entities.Author>(authorsResourceParameters.OrderBy))
            {
                return(BadRequest());
            }

            if (!typeHelperServices.TypeHasProperties <Models.Author>(authorsResourceParameters.Fields))
            {
                return(BadRequest());
            }

            var authorsFromRepo = libraryRepository.GetAuthors(authorsResourceParameters);

            var paginationMetadata = Helpers.URICreator.CreateURI(authorsResourceParameters, authorsFromRepo, urlHelper, "GetAuthors");

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

            var authors = Mapper.Map <IEnumerable <Models.Author> >(authorsFromRepo);

            return(Ok(authors.ShapeData(authorsResourceParameters.Fields)));
        }
        public IActionResult GetAuthors([FromQuery] Helpers.AuthorsResourceParameters parameters)
        {
            if (!_propertyMappingService.ValidMappingExistsFor <Models.AuthorDto, Entities.Author>(parameters.OrderBy))
            {
                return(BadRequest());
            }

            if (!_typeHelperService.TypeHasProperties <AuthorDto>(parameters.Fields))
            {
                return(BadRequest());
            }

            IEnumerable <AuthorDto> authors;
            var authorsFromRepo = _libraryRepository.GetAuthors(parameters);

            string previousPageLink = authorsFromRepo.HasPrevious ?
                                      CreateAuthorsResourceUri(parameters, Helpers.ResourceUriType.PreviousPage) : null;

            string nextPageLink = authorsFromRepo.HasNext ?
                                  CreateAuthorsResourceUri(parameters, Helpers.ResourceUriType.NextPage) : null;

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

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

            authors = AutoMapper.Mapper.Map <IEnumerable <AuthorDto> >(authorsFromRepo);

            return(Ok(authors.ShapeData(parameters.Fields)));
        }