예제 #1
0
        public async Task <ActionResult <CityDto> > PostCity(CityMutationDto city)
        {
            var cityForCreation = _mapper.Map <City>(city);

            if (_countryService.GetCountryById(cityForCreation.CountryId) == null)
            {
                return(BadRequest("Country with given id doesn't exist"));
            }
            try
            {
                _cityService.InsertCity(cityForCreation);
                return(Created("!api/Cities", cityForCreation));
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Error on the server"));
            }
        }
예제 #2
0
        public async Task <IActionResult> PutCity(Guid id, CityMutationDto newCity)
        {
            var cityToUpdate = _cityService.GetCityById(id);

            if (cityToUpdate == null)
            {
                return(BadRequest("City with given id doesnt exist"));
            }

            if (_countryService.GetCountryById(newCity.CountryId) == null)
            {
                return(BadRequest("Country with given id doesn't exist"));
            }
            try
            {
                _cityService.UpdateCity(cityToUpdate, _mapper.Map <City>(newCity));
                return(NoContent());
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Error on the server"));
            }
        }