예제 #1
0
 public IActionResult Update(CityUpdateVM city)
 {
     if (ModelState.IsValid)
     {
         var model = _mapper.Map <City>(city);
         _unitOfWork.City.Update(model);
     }
     return(RedirectToAction("Index"));
 }
예제 #2
0
        public async Task <IActionResult> UpdateCityForCountry(int countryId, int cityId, [FromBody] CityUpdateVM cityUpdate)
        {
            if (cityUpdate == null)
            {
                return(BadRequest());
            }
            if (!await _countryRepository.CountryExistsAsync(countryId))
            {
                return(NotFound());
            }
            var city = await _cityRepository.GetCityForCountryAsync(countryId, cityId);

            if (city == null)
            {
                return(NotFound());
            }
            // 把 cityUpdate 的属性都映射给 city
            _mapper.Map(cityUpdate, city);
            _cityRepository.UpdateCityForCountry(city);
            if (!await _unitOfWork.SaveAsync())
            {
                return(StatusCode(500, $"更新 country:{countryId} 下的 city:{cityId}失败"));
            }
            return(NoContent());
        }