Beispiel #1
0
        public ActionResult <CountryInfoDto> CountryInfo([FromQuery] string countryCode = null)
        {
            if (string.IsNullOrEmpty(countryCode))
            {
                countryCode = this.AutoDetectCountryCode();
            }

            var country = new Country.CountryProvider().GetCountry(countryCode);

            if (country == null)
            {
                return(StatusCode(StatusCodes.Status404NotFound));
            }

            var countryInfo = new CountryInfoDto
            {
                CommonName           = country.CommonName,
                OfficialName         = country.OfficialName,
                CountryCode          = countryCode,
                AlternativeSpellings = country.Translations.Select(o => o.Name).ToArray(),
                Region  = country.Region.ToString(),
                Borders = this.GetCountryInfos(country.BorderCountrys)
            };

            return(StatusCode(StatusCodes.Status200OK, countryInfo));
        }
Beispiel #2
0
        public ActionResult <CountryInfoDto> CountryInfo([FromQuery] string countryCode = null)
        {
            #region Auto detect country

            if (string.IsNullOrEmpty(countryCode))
            {
                try
                {
                    var acceptLanguages = HttpContext.Request.GetTypedHeaders().AcceptLanguage;
                    var firstLanguage   = acceptLanguages.FirstOrDefault().ToString();

                    var cultureInfo = CultureInfo.GetCultureInfo(firstLanguage);
                    if (cultureInfo.IsNeutralCulture)
                    {
                        var region = new RegionInfo(firstLanguage);
                        countryCode = region.TwoLetterISORegionName;
                    }
                    else
                    {
                        var region = new RegionInfo(cultureInfo.CompareInfo.LCID);
                        countryCode = region.TwoLetterISORegionName;
                    }
                }
                catch
                {
                    //Fallback
                    countryCode = "US";
                }
            }

            #endregion

            var country = new Country.CountryProvider().GetCountry(countryCode);
            if (country == null)
            {
                return(StatusCode(StatusCodes.Status404NotFound));
            }

            var countryInfo = new CountryInfoDto
            {
                CommonName           = country.CommonName,
                OfficialName         = country.OfficialName,
                CountryCode          = countryCode,
                AlternativeSpellings = country.Translations.Select(o => o.Name).ToArray(),
                Region  = country.Region.ToString(),
                Borders = this.GetCountryInfos(country.BorderCountrys)
            };

            return(StatusCode(StatusCodes.Status200OK, countryInfo));
        }
        public async Task <ActionResult <CountryInfoDto> > PostCountry(CountryInfoDto countryInfoDto)
        {
            try
            {
                var mappedEntity = _mapper.Map <CountryInfoModel>(countryInfoDto);
                _countryInfoRepo.Add(mappedEntity);

                if (await _countryInfoRepo.Save())
                {
                    return(Created($"/api/v1.0/countryinfo/{mappedEntity.CountryInfoId}", _mapper.Map <CountryModel>(mappedEntity)));
                }
            }
            catch (Exception e)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, $"Database Failure: {e.Message}"));
            }

            return(BadRequest());
        }
Beispiel #4
0
        private static CountryInfoDto[] GetBorderCountryInfos(Alpha2Code[] countryCodes)
        {
            var countryProvider = new Country.CountryProvider();
            var items           = new List <CountryInfoDto>();

            foreach (var countryCode in countryCodes)
            {
                var country = countryProvider.GetCountry(countryCode);

                var countryInfo = new CountryInfoDto
                {
                    CommonName   = country.CommonName,
                    OfficialName = country.OfficialName,
                    CountryCode  = country.Alpha2Code.ToString(),
                    Region       = country.Region.ToString()
                };

                items.Add(countryInfo);
            }

            return(items.ToArray());
        }
Beispiel #5
0
        private CountryInfoDto[] GetCountryInfos(Country.Alpha2Code[] countryCodes)
        {
            var countryProvider = new Country.CountryProvider();
            var items           = new List <CountryInfoDto>();

            foreach (var countryCode in countryCodes)
            {
                var country = countryProvider.GetCountry(countryCode);

                var countryInfo = new CountryInfoDto
                {
                    CommonName           = country.CommonName,
                    OfficialName         = country.OfficialName,
                    CountryCode          = country.Alpha2Code.ToString(),
                    AlternativeSpellings = country.Translations.Select(o => o.Name).ToArray(),
                    Region = country.Region.ToString()
                };

                items.Add(countryInfo);
            }

            return(items.ToArray());
        }
        public async Task <ActionResult <CountryInfoDto> > ChangeCountryInfoByID(int id, [FromBody] CountryInfoDto countryInfoDto)
        {
            try
            {
                var oldCountryInfo = await _countryInfoRepo.GetCountryInfoByID(id);

                if (oldCountryInfo == null)
                {
                    return(NotFound($"Couldn't find any country with id: {id}"));
                }

                var newCountry = _mapper.Map(countryInfoDto, oldCountryInfo);
                _countryInfoRepo.Update(newCountry);

                if (await _countryInfoRepo.Save())
                {
                    return(NoContent());
                }
            }
            catch (Exception e)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, $"Database Failure: {e.Message}"));
            }
            return(BadRequest());
        }