コード例 #1
0
        public IActionResult Get()
        {
            var countriesEntities = _countryInfoRepository.GetCountries();

            var results = new List <CountriesWithoutOfficialLanguagesDTO>();

            foreach (var _countryEntity in countriesEntities)
            {
                results.Add(new CountriesWithoutOfficialLanguagesDTO
                {
                    Id         = _countryEntity.Id,
                    Name       = _countryEntity.Name,
                    Density    = _countryEntity.Density,
                    LandArea   = _countryEntity.LandArea,
                    Population = _countryEntity.Population
                });
            }

            return(Ok(results));
        }
コード例 #2
0
        public IActionResult GetCountries(CountriesResourceParameters countriesResourceParameters)
        {
            if (!_propertyMappingService.ValidMappingExistsFor <CountryDto, Country>
                    (countriesResourceParameters.OrderBy))
            {
                return(BadRequest());
            }

            if (!_typeHelperService.TypeHasProperties <CountryDto>
                    (countriesResourceParameters.Fields))
            {
                return(BadRequest());
            }

            var countriesFromRepo = _repository.GetCountries(countriesResourceParameters);
            var countries         = Mapper.Map <IEnumerable <CountryDto> >(countriesFromRepo);

            var previousPageLink = countriesFromRepo.HasPrevious ?
                                   CreateCountriesResourceUri(countriesResourceParameters,
                                                              ResourceUriType.PreviousPage) : null;

            var nextPageLink = countriesFromRepo.HasNext ?
                               CreateCountriesResourceUri(countriesResourceParameters,
                                                          ResourceUriType.NextPage) : null;

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

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

            var links = CreateLinksForCountries(countriesResourceParameters,
                                                countriesFromRepo.HasNext, countriesFromRepo.HasPrevious);

            var shapedCountries = countries.ShapeData(countriesResourceParameters.Fields);

            var shapedCountriesWithLinks = shapedCountries.Select(author =>
            {
                var countryAsDictionary = author as IDictionary <string, object>;
                var countryLinks        = CreateLinksForCountry(
                    (int)countryAsDictionary["Id"], countriesResourceParameters.Fields);

                countryAsDictionary.Add("links", countryLinks);

                return(countryAsDictionary);
            });

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

            return(Ok(linkedCollectionResource));
        }