示例#1
0
        public PagedList <Author> GetAuthors(AuthorResourceParams authorResourceParams)
        {
            //var collectionBeforePaging = context.Authors
            //    .OrderBy(a => a.FirstName)
            //    .ThenBy(a => a.LastName).AsQueryable();

            var collectionBeforePaging = context.Authors.ApplySort(authorResourceParams.OrderBy, propertyMappingService.GetPropertyMapping <AuthorVM, Author>());

            if (!string.IsNullOrEmpty(authorResourceParams.Genre))
            {
                var genreWhereClause = authorResourceParams.Genre.Trim().ToLowerInvariant();
                collectionBeforePaging = collectionBeforePaging.Where(a => a.Genre.ToLowerInvariant() == genreWhereClause);
            }
            if (!string.IsNullOrEmpty(authorResourceParams.SearchQuery))
            {
                // trim & ignore casing
                var searchQueryWhereClause = authorResourceParams.SearchQuery
                                             .Trim().ToLowerInvariant();

                collectionBeforePaging = collectionBeforePaging
                                         .Where(a => a.Genre.ToLowerInvariant().Contains(searchQueryWhereClause) ||
                                                a.FirstName.ToLowerInvariant().Contains(searchQueryWhereClause) ||
                                                a.LastName.ToLowerInvariant().Contains(searchQueryWhereClause));
            }
            return(PagedList <Author> .Create(collectionBeforePaging,
                                              authorResourceParams.PageNumber,
                                              authorResourceParams.PageSize));
        }
示例#2
0
        private IEnumerable <LinkVM> CreateAuthorsLinks(AuthorResourceParams authorResourceParams, bool hasNext, bool hasPrevious)
        {
            var links = new List <LinkVM>();

            links.Add(new LinkVM(CreateAuthorResourceUri(authorResourceParams, ResourceUriType.Current), "self", "GET"));
            if (hasNext)
            {
                links.Add(new LinkVM(CreateAuthorResourceUri(authorResourceParams, ResourceUriType.NextPage), "nextPage", "GET"));
            }
            if (hasPrevious)
            {
                links.Add(new LinkVM(CreateAuthorResourceUri(authorResourceParams, ResourceUriType.PreviousPage), "previousPage", "GET"));
            }
            return(links);
        }
示例#3
0
        private string CreateAuthorResourceUri(AuthorResourceParams authorResourceParams, ResourceUriType type)
        {
            switch (type)
            {
            case ResourceUriType.PreviousPage:
                return(urlHelper.Link("GetAuthors",
                                      new
                {
                    fields = authorResourceParams.Fields,
                    orderBy = authorResourceParams.OrderBy,
                    searchQuery = authorResourceParams.SearchQuery,
                    genre = authorResourceParams.Genre,
                    pageNumber = authorResourceParams.PageNumber - 1,
                    pageSize = authorResourceParams.PageSize
                }));

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

            case ResourceUriType.Current:
            default:
                return(urlHelper.Link("GetAuthors",
                                      new
                {
                    fields = authorResourceParams.Fields,
                    orderBy = authorResourceParams.OrderBy,
                    searchQuery = authorResourceParams.SearchQuery,
                    genre = authorResourceParams.Genre,
                    pageNumber = authorResourceParams.PageNumber,
                    pageSize = authorResourceParams.PageSize
                }));
            }
        }
示例#4
0
        public IActionResult GetAuthors(AuthorResourceParams authorResourceParams, [FromHeader(Name = "Accept")] string mediaType)
        {
            if (!propertyMappingService.ValidMappingExistsFor <AuthorVM, Author>(authorResourceParams.OrderBy))
            {
                return(BadRequest());
            }
            if (!typeHelperService.TypeHasProperties <AuthorVM>(authorResourceParams.Fields))
            {
                return(BadRequest());
            }
            var authors   = libraryRepository.GetAuthors(authorResourceParams);
            var authorsVM = Mapper.Map <IEnumerable <AuthorVM> >(authors);

            if (mediaType == "application/vnd.marvin.hateoas+json")
            {
                var paginationMetadata = new
                {
                    totalCount  = authors.TotalCount,
                    pageSize    = authors.PageSize,
                    currentPage = authors.CurrentPage,
                    totalPages  = authors.TotalPages
                };

                Response.Headers.Add("X-Pagination",
                                     Newtonsoft.Json.JsonConvert.SerializeObject(paginationMetadata));
                var links                  = CreateAuthorsLinks(authorResourceParams, authors.HasNext, authors.HasPrevious);
                var shapedAuthors          = authorsVM.ShapeData(authorResourceParams.Fields);
                var shapedAuthorsWithLinks = shapedAuthors.Select(a =>
                {
                    var authorAsDictionary = a as IDictionary <string, object>;
                    var authorLinks        = CreateAuthorLinks((Guid)authorAsDictionary["Id"], authorResourceParams.Fields);
                    authorAsDictionary.Add("links", authorLinks);
                    return(authorAsDictionary);
                });

                var linkedCollectionResource = new
                {
                    value = shapedAuthorsWithLinks,
                    links = links
                };
                return(Ok(linkedCollectionResource));
            }
            else
            {
                var previousPageLink = authors.HasPrevious ?
                                       CreateAuthorResourceUri(authorResourceParams,
                                                               ResourceUriType.PreviousPage) : null;

                var nextPageLink = authors.HasNext ?
                                   CreateAuthorResourceUri(authorResourceParams,
                                                           ResourceUriType.NextPage) : null;

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

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

                return(Ok(authorsVM.ShapeData(authorResourceParams.Fields)));
            }
        }