public IActionResult UpdateCity(int id, [FromBody] CityForUpdateDTO cityUpdate) { var cityModel = CityDataStore.Current.Cities.FirstOrDefault(c => c.Id == id); if (cityModel == null) { return(NotFound()); } cityModel.Name = cityUpdate.Name; cityModel.Description = cityUpdate.Description; return(NoContent()); }
public IActionResult UpdateCity(int Id, [FromBody] CityForUpdateDTO city) { // for repeated validations use Fluent Validation Debug.WriteLine("update city initiated"); //deserialise the frombody data and use to create city if (city == null) { return(BadRequest()); } //[maxlength] in DTOs these are data annotation attributes which are contained in ModelState dictionary //these are used for data validation, if not valid they will make ModelState invalid //in that case body can not be deserialized if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!_cityInfoRepository.CityExists(Id)) { return(NotFound()); } var cityToUpdate = _cityInfoRepository.GetCity(Id); if (cityToUpdate == null) { return(NotFound()); } //this overload Map method of Mapper copies the source object content (1st param) into the destination object (2nd param) //which is what is required to update the content entity AutoMapper.Mapper.Map(city, cityToUpdate); //On saving Context, the reference object cityUpdate is automatically populated if (!_cityInfoRepository.Save()) { return(StatusCode(500, "Error updating city")); } return(NoContent()); }