예제 #1
0
        public IActionResult GetCountry(int id, [FromQuery] string fields, bool includePostalCodes = false)
        {
            if (!_typeHelperService.TypeHasProperties <CountryDto>(fields))
            {
                return(BadRequest());
            }

            var countryFromRepo = _repository.GetCountry(id, includePostalCodes);

            if (countryFromRepo == null)
            {
                return(NotFound());
            }

            if (includePostalCodes)
            {
                var countryWithPostalCodes = Mapper.Map <Models.CountryWithPostalCodesDto>(countryFromRepo);
                return(Ok(countryWithPostalCodes));
            }

            var country = Mapper.Map <Models.CountryDto>(countryFromRepo);

            var links = CreateLinksForCountry(id, fields);

            var linkedResourceToReturn = country.ShapeData(fields)
                                         as IDictionary <string, object>;

            linkedResourceToReturn.Add("links", links);

            return(Ok(linkedResourceToReturn));
        }
        public IActionResult Get(int id, bool languages = false)
        {
            try
            {
                var country = _countryInfoRepository.GetCountry(id, languages);


                if (country == null)
                {
                    return(NotFound());
                }

                if (languages)
                {
                    var countryResults = new CountryDTO()
                    {
                        Id         = country.Id,
                        Name       = country.Name,
                        Density    = country.Density,
                        LandArea   = country.LandArea,
                        Population = country.Population
                    };

                    foreach (var lang in country.OfficialLanguages)
                    {
                        countryResults.OfficialLanguages.Add(
                            new OfficialLanguagesByCountryDTO()
                        {
                            Id           = lang.Id,
                            LanguageName = lang.LanguageName
                        });
                    }
                    return(Ok(countryResults));
                }
                else
                {
                    var countryResults = new CountryDTO()
                    {
                        Id         = country.Id,
                        Name       = country.Name,
                        Density    = country.Density,
                        LandArea   = country.LandArea,
                        Population = country.Population
                    };

                    return(Ok(countryResults));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest());
            }
        }