public async Task <City> CreateCityAsync(UpdateCityRequest city) { var duplicateCityDb = await _cityCntx.Cities.Where(c => c.CountryId == city.CountryId && String.CompareOrdinal(c.CityName, city.CityName) == 0).ToArrayAsync(); if (duplicateCityDb.Length > 0) { throw new DuplicateFoundException("Current city already exists"); } var added = _cityCntx.Cities.Add(_mapper.Map <CityDTO>(city)); var result = await _cityCntx.SaveChangesAsync(); if (result == 0) { throw new DuplicateFoundException("Can not add city with requested parameters"); } return(_mapper.Map <City>(added)); }
public async Task <City> UpdateCityByIdAsync(int cityId, UpdateCityRequest city) { var citiesDb = await _cityCntx.Cities.Where(c => c.CityId == cityId).ToArrayAsync(); if (citiesDb?.Length < 1) { throw new IdNotFoundException("No city found"); } var mapped = _mapper.Map <UpdateCityRequest, CityDTO>(city, citiesDb[0]); mapped.CityId = citiesDb[0].CityId; var updated = _cityCntx.Cities.Update(mapped); var result = await _cityCntx.SaveChangesAsync(); if (result == 0) { throw new DuplicateFoundException("Can not update city with such requested parameters"); } return(_mapper.Map <City>(updated)); }