Ejemplo n.º 1
0
        /// <summary>
        /// Get characters PagedList from repository.
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public PagedList <Character> GetCharacters(CharacterResourceParameters parameters)
        {
            var collectionBeforePaging = context.Characters
                                         .ApplySort(parameters.OrderBy,
                                                    propertyMappingService.GetPropertyMapping <CharacterDto, Character>());

            if (!string.IsNullOrEmpty(parameters.Planet))
            {
                var planetQuery = StringHelpers.TrimToLowerInvariant(parameters.Planet);

                collectionBeforePaging = collectionBeforePaging
                                         .Where(c => StringHelpers.TrimToLowerInvariant(c.Planet) == planetQuery);
            }

            if (!string.IsNullOrEmpty(parameters.SearchQuery))
            {
                var searchQuery = StringHelpers.TrimToLowerInvariant(parameters.SearchQuery);

                collectionBeforePaging = collectionBeforePaging
                                         .Where(c =>
                                                StringHelpers.TrimToLowerInvariant(c.Name).Contains(searchQuery) ||
                                                StringHelpers.TrimToLowerInvariant(c.Planet).Contains(searchQuery));
            }

            return(PagedList <Character> .Create(collectionBeforePaging,
                                                 parameters.PageNumber,
                                                 parameters.PageSize));
        }
Ejemplo n.º 2
0
        public IActionResult GetCharacters(CharacterResourceParameters parameters)

        {
            if (!propertyMappingService.ValidMappingExistsFor <CharacterDto, Character>
                    (parameters.OrderBy))
            {
                return(BadRequest());
            }

            if (!typeHelperService.TypeHasProperties <CharacterDto>(parameters.Fields))
            {
                return(BadRequest());
            }

            var charactersFromRepo = repository.GetCharacters(parameters);

            var previousPageLink = charactersFromRepo.HasPrevious
            ? CreateCharactersResourcesUri(parameters, ResourceUriType.PreviousPage)
            : null;

            var nextPageLink = charactersFromRepo.HasNext
            ? CreateCharactersResourcesUri(parameters, ResourceUriType.NextPage)
            : null;

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

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

            var charactersToReturn = Mapper.Map <IEnumerable <CharacterDto> >(charactersFromRepo);

            return(Ok(charactersToReturn.ShapeData(parameters.Fields)));
        }
Ejemplo n.º 3
0
        private string CreateCharactersResourcesUri(
            CharacterResourceParameters parameters,
            ResourceUriType type)
        {
            switch (type)
            {
            case ResourceUriType.PreviousPage:
                return(Url.Link("GetCharacters", new
                {
                    orderBy = parameters.OrderBy,
                    fields = parameters.Fields,
                    planet = parameters.Planet,
                    searchQuery = parameters.SearchQuery,
                    pageNumber = parameters.PageNumber - 1,
                    pageSize = parameters.PageSize
                }));

            case ResourceUriType.NextPage:
                return(Url.Link("GetCharacters", new
                {
                    orderBy = parameters.OrderBy,
                    fields = parameters.Fields,
                    planet = parameters.Planet,
                    searchQuery = parameters.SearchQuery,
                    pageNumber = parameters.PageNumber + 1,
                    pageSize = parameters.PageSize
                }));

            default:
                return(Url.Link("GetCharacters", new
                {
                    orderBy = parameters.OrderBy,
                    fields = parameters.Fields,
                    planet = parameters.Planet,
                    searchQuery = parameters.SearchQuery,
                    pageNumber = parameters.PageNumber,
                    pageSize = parameters.PageSize
                }));
            }
        }