public async Task SaveCountryAsync(CountryViewModel countryViewModel) { var capitalId = await SaveCapitalAsync(countryViewModel.Capital).ConfigureAwait(false); var regionId = await SaveRegionAsync(countryViewModel.Region).ConfigureAwait(false); using (var context = new CountriesContext()) { var savedCountry = await countriesRepository.GetAll(context) .FirstOrDefaultAsync(country => country.NumericCode == countryViewModel.NumericCode) .ConfigureAwait(false); if (savedCountry == null) { await countriesRepository .AddAsync(context, MapViewModelToCountry(countryViewModel, capitalId, regionId)) .ConfigureAwait(false); } else { await countriesRepository .UpdateAsync(context, savedCountry.Id, country => MapViewModelToCountry(countryViewModel, capitalId, regionId)) .ConfigureAwait(false); } } }
/// <summary> /// Add Or Edit country /// </summary> /// <param name="countryId"></param> /// <param name="request"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task <Response> AddOrEditCountryAsync(int countryId, CreateCountryRequest request, CancellationToken cancellationToken = default) { var responseModel = new Response(); if (await CountryNameExists(request.Name)) { responseModel.AddError(ExceptionCreator.CreateBadRequestError("country", "country name does already exist")); return(responseModel); } if (countryId != 0) { var country = await _countriesRepo.FindByIdAsync(countryId); if (country != null) { country.Name = request.Name; country.Code = request.Code; country.LastModifiedBy = _httpContextAccessor.HttpContext.User.Identity.Name; country.LastModifiedOn = DateTime.UtcNow; await _countriesRepo.UpdateAsync(country); } else { responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(country), $"country of id {countryId}: not found")); return(responseModel); } } else { try { var newCountry = CreateCountry(request); await _countriesRepo.AddAsync(newCountry); } catch (Exception ex) { responseModel.AddError(ExceptionCreator.CreateInternalServerError(ex.ToString())); } } return(responseModel); }
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)); }