private async Task CheckCityExist(CityRequestModel requestModel, string id = null) { var existingCity = await _cityRepository.FirstOrDefaultAsync(city => (string.IsNullOrEmpty(id) || city.Id != id) && city.CountryId == requestModel.CountryId && (city.Name == requestModel.Name || city.CityCode == requestModel.CityCode)); if (existingCity != null) { string allreadyExistingData = existingCity.Name == requestModel.Name ? requestModel.Name : requestModel.CityCode; throw new ItemAlreadyExistsException(allreadyExistingData); } }
public async Task <CityDto> CreateAsync(CityRequestModel requestModel) { Country country = await _countryRepository.FirstOrDefaultAsync(c => c.Id == requestModel.CountryId); if (country == null) { throw new NotFoundException($"Country not found for Id:{requestModel.CountryId}"); } await CheckCityExist(requestModel); var createdCity = _mapper.Map <City>(requestModel); createdCity.CountryCode = country.Iso2; return(_mapper.Map <CityDto>(await _cityRepository.AddAsync(createdCity))); }
public async Task <CityDto> UpdateAsync(string id, CityRequestModel requestModel) { if (!await _cityRepository.AnyAsync(city => city.Id == id)) { throw new ItemNotFoundException(requestModel.Name); } if (!await _countryRepository.AnyAsync(country => country.Id == requestModel.CountryId)) { throw new NotFoundException($"Country not found for Id:{requestModel.CountryId}"); } await CheckCityExist(requestModel, id); var updatedCity = await _cityRepository.UpdateAsync(id, _mapper.Map <City>(requestModel)); return(_mapper.Map <CityDto>(updatedCity)); }
public ActionResult <City> Post([FromBody] CityRequestModel city) { try { City _city = new City { GeoLocation = city.Location, Name = city.Name }; using (var scope = container.BeginLifetimeScope()) { using (var repository = scope.Resolve <ICityRepository>()) { _city = repository.Read(repository.Create(_city)); } } return(_city); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public IActionResult Put([FromBody] CityRequestModel city) { try { City _city = new City { GeoLocation = city.Location, Name = city.Name, Id = city.Id }; using (var scope = container.BeginLifetimeScope()) { using (var repository = scope.Resolve <ICityRepository>()) { repository.Update(_city); } } return(Ok()); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public virtual async Task <CityDto> UpdateAsync(string id, [FromBody] CityRequestModel request) => await _cityService.UpdateAsync(id, request);
public virtual async Task <CityDto> CreateAsync([FromBody] CityRequestModel request) => await _cityService.CreateAsync(request);
public async Task AddAsync(CityRequestModel cityModel) { await _context.AddAsync(cityModel); await _context.SaveChangesAsync(); }