Ejemplo n.º 1
0
        private async Task <IIpV4Client> AddIpAddressAsync(IPAddress address, CityResponse response, CancellationToken cancellationToken = default)
        {
            var continentTask = _continentsRepository.FetchAsync(new ContinentFilter(response.Continent.Name));
            var countryTask   = _countriesRepository.FetchAsync(new CountyFilter(response.Country.Name));
            var cityTask      = _citiesRepository.FetchAsync(new CityFilter(response.City.Name));

            await Task.WhenAll(continentTask, countryTask, cityTask)
            .ConfigureAwait(continueOnCapturedContext: false);

            var continent = continentTask.Result.FirstOrDefault();
            var country   = countryTask.Result.FirstOrDefault();
            var city      = cityTask.Result.FirstOrDefault();

            if (continent == null)
            {
                continent = await _continentsRepository
                            .AddAsync(new ContinentData(response.Continent.Name, response.Continent.Code))
                            .ConfigureAwait(continueOnCapturedContext: false);
            }
            if (country == null)
            {
                country = await _countriesRepository
                          .AddAsync(new CountyData(response.Country.Name, response.Country.IsoCode, continent))
                          .ConfigureAwait(continueOnCapturedContext: false);
            }
            if (city == null && !string.IsNullOrWhiteSpace(response.City.Name))
            {
                city = await _citiesRepository
                       .AddAsync(new CityData(response.City.Name, country))
                       .ConfigureAwait(continueOnCapturedContext: false);
            }
            var location = new LocationData(response.Location.AccuracyRadius, response.Location.Latitude, response.Location.Longitude, response.Location.TimeZone);

            return(await _ipV4ClientsRepository
                   .AddAsync(new IpV4ClientData(address.ToString(), city, country, continent, location))
                   .ConfigureAwait(continueOnCapturedContext: false));
        }