private async Task <GeolocationDetails> CreateDetailsAsync(string ipOrUrl, GeolocationDetails newItem)
        {
            try
            {
                string accessKey = ipStackConfiguration.GetAccessKey();

                if (string.IsNullOrWhiteSpace(accessKey))
                {
                    throw new ApplicationException("Missing IP Stack access key setting.");
                }

                Interfaces.Data.GeolocationDetails details = await geolocationDetailsProvider.GetAsync(accessKey, ipOrUrl);

                if (details.Success == false)
                {
                    HandleUnsuccessfulDetailsRequest(details);
                }

                newItem.City        = details.City;
                newItem.CountryName = details.CountryName;
                newItem.ZipCode     = details.ZipCode;

                db.GeolocationDetails.Add(newItem);
                db.SaveChanges();

                return(newItem);
            }
            catch (Exception ex)
            {
                logger.LogError(this, ex);
                throw;
            }
        }
        public static void ImportCSV(string csvFile)
        {
            var      context  = new GeolocationContext();
            Assembly assembly = Assembly.GetExecutingAssembly();

            using (var reader = new StreamReader(csvFile, Encoding.UTF8))
            {
                CsvReader csvReader = new CsvReader(reader, CultureInfo.InvariantCulture);
                csvReader.Configuration.HasHeaderRecord = true;
                csvReader.Configuration.Delimiter       = ",";
                csvReader.Configuration.IgnoreQuotes    = true;
                csvReader.Configuration.RegisterClassMap <CSVMap>();
                var addresses = csvReader.GetRecords <Address>().ToArray();
                context.Addresses.RemoveRange(context.Addresses);
                context.AddRange(addresses);
                context.SaveChanges();
            }
        }
예제 #3
0
        public static void ImportCSV(string csvFile)
        {
            var      context  = new GeolocationContext();
            Assembly assembly = Assembly.GetExecutingAssembly();

            using (var reader = new StreamReader(csvFile, Encoding.UTF8))
            {
                CsvReader csvReader = new CsvReader(reader, CultureInfo.InvariantCulture);
                csvReader.Configuration.HasHeaderRecord = true;
                csvReader.Configuration.Delimiter       = ",";
                csvReader.Configuration.IgnoreQuotes    = true;
                csvReader.Configuration.RegisterClassMap <CSVMap>();
                var geolocations = csvReader.GetRecords <Geolocation>().ToArray();
                context.Database.ExecuteSqlCommand("Delete from \"Geolocations\"");
                context.AddRange(geolocations);
                context.SaveChanges();
            }
        }