public async Task <CityDto> CreateCityAsync(CityCreateDto cityCreateDto) { var existingCity = await _citiesRepository.FindByNameAsync(cityCreateDto.Name); if (existingCity != null) { throw new ObjectConflictException("City allready exists"); } var newCity = _mapper.Map <City>(cityCreateDto); await _citiesRepository.AddAsync(newCity); return(_mapper.Map <CityDto>(newCity)); }
/// <summary> /// Add or Edit an existing city /// </summary> /// <param name="cityId"></param> /// <param name="request"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task <Response> AddOrEditCityAsync(int cityId, CreateCityRequest request, CancellationToken cancellationToken = default) { var responseModel = new Response(); if (await _citiesRepo.CityNameExists(request.CountryId, request.Name)) { responseModel.AddError(ExceptionCreator.CreateBadRequestError("city", "city name does already exist")); return(responseModel); } if (cityId != 0) { var city = await _dbContext.Cities.FirstOrDefaultAsync(c => c.Id == cityId); try { if (city != null) { city.CountryId = request.CountryId; city.Name = request.Name; city.LastModifiedBy = _httpContextAccessor.HttpContext.User.Identity.Name; city.LastModifiedOn = DateTime.UtcNow; await _citiesRepo.UpdateAsync(city); } else { responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(city), $"city of Id: { cityId} not found")); return(responseModel); } } catch (Exception ex) { responseModel.AddError(ExceptionCreator.CreateInternalServerError(ex.ToString())); } } else { try { await _citiesRepo.AddAsync(CreateCity(request)); } 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)); }
public async Task <Result <int> > Create(City city) { return(await _citiesRepository.AddAsync(city)); }