public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id, [FromBody] JsonPatchDocument <UpdatePointOfInterestRequestModel> requestedPatch) { if (requestedPatch == null) { BadRequest(); } var city = CitiesDataStore.Current.Cities.SingleOrDefault(c => c.Id == cityId); if (city == null) { return(NotFound()); } var pointOfInterest = city.PointsOfInterest.FirstOrDefault(p => p.Id == id); if (pointOfInterest == null) { return(NotFound()); } var pointOfInterestToBePatched = new UpdatePointOfInterestRequestModel() { Description = pointOfInterest.Description, Name = pointOfInterest.Name }; requestedPatch.ApplyTo(pointOfInterestToBePatched, ModelState); TryValidateModel(pointOfInterestToBePatched); if (!ModelState.IsValid) { return(BadRequest()); } pointOfInterest.Name = pointOfInterestToBePatched.Name; pointOfInterest.Description = pointOfInterestToBePatched.Description; return(NoContent()); }
public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] UpdatePointOfInterestRequestModel requestModel) { if (requestModel == null) { return(BadRequest()); } if (requestModel.Description == requestModel.Name) { ModelState.AddModelError("Description", "Description should be different than name."); } if (!ModelState.IsValid) { return(BadRequest()); } var city = CitiesDataStore.Current.Cities.SingleOrDefault(c => c.Id == cityId); if (city == null) { return(NotFound()); } var pointOfInterest = city.PointsOfInterest.FirstOrDefault(p => p.Id == id); if (pointOfInterest == null) { return(NotFound()); } pointOfInterest.Name = requestModel.Name; pointOfInterest.Description = requestModel.Description; return(Ok()); }