public async Task <IActionResult> PartiallyUpdateCityForCountry(int countryId, int cityId, [FromBody] JsonPatchDocument <CityUpdateResource> patchDoc) { if (patchDoc == null) { return(BadRequest()); } if (!await _countryRepository.CountryExistAsync(countryId)) { return(NotFound()); } var city = await _cityRepository.GetCityForCountryAsync(countryId, cityId); if (city == null) { var cityUpdate = new CityUpdateResource(); patchDoc.ApplyTo(cityUpdate); var cityToAdd = _mapper.Map <City>(cityUpdate); cityToAdd.Id = cityId; // 只适用于Id不是自增的情况 _cityRepository.AddCityForCountry(countryId, cityToAdd); if (!await _unitOfWork.SaveAsync()) { return(StatusCode(500, $"P city {cityId} for country {countryId} failed when inserting")); } var cityResource = Mapper.Map <CityResource>(cityToAdd); return(CreatedAtRoute("GetCityForCountry", new { countryId, cityId }, cityResource)); //return NotFound(); } //把EFCore的City映射成CityUpdateResource,这样这个CityUpdateResource就有了该City在数据库里最新的属性值 var cityToPatch = _mapper.Map <CityUpdateResource>(city); //patchDoc里面有任何验证错误都会在ModelState里面体现出来 //patchDoc.ApplyTo(cityToPatch, ModelState); patchDoc.ApplyTo(cityToPatch); TryValidateModel(cityToPatch); if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } _mapper.Map(cityToPatch, city); _cityRepository.UpdateCityForCountry(city); if (!await _unitOfWork.SaveAsync()) { throw new Exception($"Patching city {cityId} for country {countryId} failed when saving."); } return(NoContent()); }
public async Task <IActionResult> UpdateCityForCountry(int countryId, int cityId, [FromBody] CityUpdateResource cityUpdate) { if (cityUpdate == null) { return(BadRequest()); } if (cityUpdate.Name == "中国") { ModelState.AddModelError(nameof(cityUpdate.Name), "城市的名称不可以叫中国"); } if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } if (!await _countryRepository.CountryExistAsync(countryId)) { return(NotFound()); } var city = await _cityRepository.GetCityForCountryAsync(countryId, cityId); if (city == null) { var cityToAdd = _mapper.Map <City>(cityUpdate); cityToAdd.Id = cityId; // 如果Id不是自增的话 _cityRepository.AddCityForCountry(countryId, cityToAdd); if (!await _unitOfWork.SaveAsync()) { return(StatusCode(500, $"Upserting city {cityId} for country {countryId} failed when inserting")); } var cityResource = Mapper.Map <CityResource>(cityToAdd); return(CreatedAtRoute("GetCityForCountry", new { countryId, cityId }, cityResource)); //return NotFound(); } // 把cityUpdate的属性值都映射给city 把第一个参数对象的属性映射到第二个参数对象上 _mapper.Map(cityUpdate, city); _cityRepository.UpdateCityForCountry(city); if (!await _unitOfWork.SaveAsync()) { throw new Exception($"Updating city {cityId} for country {countryId} failed when saving."); } //Ok和NoContent都是可以的,如果在Action的方法里某些属性的值是在这里改变的,那么可以使用Ok把最新的对象传递回去;但是如果在Action方法里没有再修改其它属性的值,也就是说更新之后和传递进来的对象的属性值是一样的,那就没有必要再把最新的对象传递回去了,这时就应该使用NoContent。 return(NoContent()); }
public async Task <IActionResult> UpdateCityForCountry(int countryId, int cityId, [FromBody] CityUpdateResource cityUpdate) { if (cityUpdate == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } if (!await _countryRepository.CountryExistAsync(countryId)) { return(NotFound()); } var city = await _cityRepository.GetCityForCountryAsync(countryId, cityId); if (city == null) { //var cityToAdd = _mapper.Map<City>(cityUpdate); //cityToAdd.Id = cityId; // 如果Id不是自增的话 //_cityRepository.AddCityForCountry(countryId, cityToAdd); //if (!await _unitOfWork.SaveAsync()) //{ // return StatusCode(500, $"Upserting city {cityId} for country {countryId} failed when inserting"); //} //var cityResource = Mapper.Map<CityResource>(cityToAdd); //return CreatedAtRoute("GetCityForCountry", new { countryId, cityId }, cityResource); return(NotFound()); } // 把cityUpdate的属性值都映射给city _mapper.Map(cityUpdate, city); _cityRepository.UpdateCityForCountry(city); if (!await _unitOfWork.SaveAsync()) { throw new Exception($"Updating city {cityId} for country {countryId} failed when saving."); } return(NoContent()); }
public async Task <IActionResult> UpdateCityForCountry(int countryId, int cityId, [FromBody] CityUpdateResource cityUpdate) { if (cityUpdate == null) { return(BadRequest()); } if (!await _countryRepository.CountryExistAsync(countryId)) { return(NotFound()); } var city = await _cityRepository.GetCityForCountryAsync(countryId, cityId); if (city == null) { return(NotFound()); } _mapper.Map(cityUpdate, city); _cityRepository.UpdateCityForCountry(city); if (!await _unitOfWork.SaveAsync()) { return(StatusCode(500, $"更新Country表中Id为{countryId}的子表City,Id为{cityId}的数据时出错!")); } return(NoContent()); }