public IActionResult PatchPointOfInterest(int cityId, int poiId, [FromBody] JsonPatchDocument <PointOfInterestToUpdateDto> poiPatch) { if (poiPatch == null) { return(BadRequest()); } // get the specified city var foundCity = CitiesDataStore.Current.Cities.Find(city => city.Id == cityId); // if the city is not found, send not found if (foundCity == null) { return(NotFound()); } var pointOfInterestFromStore = foundCity.PointsOfInterest.Find(poi => poi.Id == poiId); if (pointOfInterestFromStore == null) { return(NotFound()); } var poiDto = new PointOfInterestToUpdateDto { Name = pointOfInterestFromStore.Name, Description = pointOfInterestFromStore.Description }; poiPatch.ApplyTo(poiDto, ModelState); // add a validation check to the model state to check that the description is not the same as the name. if (poiDto.Description?.Equals(poiDto.Name, System.StringComparison.InvariantCultureIgnoreCase) == true) { ModelState.AddModelError("Description", "The description cannot be the same as the name."); } TryValidateModel(poiDto); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } pointOfInterestFromStore.Name = poiDto.Name; pointOfInterestFromStore.Description = poiDto.Description; return(NoContent()); }
public IActionResult UpdatePointOfInterest(int cityId, int poiId, [FromBody] PointOfInterestToUpdateDto pointOfInterest) { // if body is null, send bad request if (pointOfInterest == null) { return(BadRequest(ModelState)); } // add a validation check to the model state to check that the description is not the same as the name. if (pointOfInterest.Description?.Equals(pointOfInterest.Name, System.StringComparison.InvariantCultureIgnoreCase) == true) { ModelState.AddModelError("Description", "The description cannot be the same as the name."); } // if the model is not valid (decorated by attributes), send bad request if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // get the specified city var foundCity = CitiesDataStore.Current.Cities.Find(city => city.Id == cityId); // if the city is not found, send not found if (foundCity == null) { return(NotFound()); } var pointOfInterestToUpdate = foundCity.PointsOfInterest.Find(poi => poi.Id == poiId); if (pointOfInterestToUpdate == null) { return(NotFound()); } pointOfInterestToUpdate.Name = pointOfInterest.Name; pointOfInterestToUpdate.Description = pointOfInterest.Description; return(NoContent()); }