Пример #1
0
        public ActionResult ListCountries()
        {
            var all    = new List <CountryJson>();
            var places = GeoCountryInfo.All;

            foreach (var place in places.OrderBy(x => x.CountryID))
            {
                var one = new CountryJson();
                one.key       = place.ISO2;
                one.id        = place.CountryID;
                one.continent = Convert.ToInt32(place.Continent);
                one.name      = place.Name;
                one.lat       = place.Center.Latitude;
                one.lng       = place.Center.Longitude;

                one.latMin = place.Bounds.SouthEast.Latitude;
                one.latMax = place.Bounds.NorthWest.Latitude;

                one.lngMin = place.Bounds.NorthWest.Longitude;
                one.lngMax = place.Bounds.SouthEast.Longitude;

                all.Add(one);
            }

            return(Json(all, JsonRequestBehavior.AllowGet));
        }
Пример #2
0
        public async Task <IActionResult> StoreCountries()
        {
            string apiBase    = _apiConfig.Value.apiBase;
            string apiCountry = _apiConfig.Value.apiCountries;

            ApiCaller apiCall  = new ApiCaller();
            var       response = await apiCall.CallApi(apiBase, apiCountry);

            if (!string.IsNullOrEmpty(response))
            {
                //Deserializing the response recieved from web api and storing
                CountryJson countryJson = JsonConvert.DeserializeObject <CountryJson>(response);
                if (countryJson.results.Any())
                {
                    using (_context)
                    {
                        try
                        {
                            foreach (CountryResponse result in countryJson.results)
                            {
                                if (await _context.Country.FirstOrDefaultAsync(c => c.Code == result.code) == null)
                                {
                                    await _context.Country.AddAsync(new Country
                                    {
                                        Code      = result.code,
                                        Name      = result.name,
                                        Cities    = result.cities,
                                        Count     = result.count,
                                        Locations = result.locations
                                    });
                                }
                            }

                            await _context.SaveChangesAsync();
                        }
                        catch (Exception e)
                        {//log exception
                            return(BadRequest(e.Message));
                        }
                    }
                }
            }

            return(Ok(true));
        }