[HttpPut("{cityId}/pointsofinterest/{id}")] //fully update a point of interest public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForUpdatingDto pointOfInterest) { if (pointOfInterest == null || !ModelState.IsValid) //repeaded calidation here, fluent validation should be looked at. { return(BadRequest(ModelState)); } if (pointOfInterest.Name == pointOfInterest.Description) { ModelState.AddModelError("Description", "The provided description should be different from the name"); return(BadRequest(ModelState)); } var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId); if (city == null) { return(NotFound()); } var pointOfInterestToUpdate = city.PointsOfInterest.FirstOrDefault(p => p.Id == id); if (pointOfInterestToUpdate == null) { return(NotFound()); } //when using the put attribute we must update every attribute in the resource, if no value is provided for an attribute it should be set to its default value. pointOfInterestToUpdate.Name = pointOfInterest.Name; pointOfInterestToUpdate.Description = pointOfInterest.Description; return(NoContent()); //for updates no content is the standard. }
public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForUpdatingDto pointOfInterest) { if (pointOfInterest.Description == pointOfInterest.Name) { ModelState.AddModelError( "Description", "The provided description should be different from the name."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!_cityInfoRepository.CityExists(cityId)) { return(NotFound()); } var pointOfInterestEntity = _cityInfoRepository. GetPointOfInterestForCity(cityId, id); if (pointOfInterestEntity == null) { return(NotFound()); } _mapper.Map(pointOfInterest, pointOfInterestEntity); _cityInfoRepository.UpdatePointOfInterestForCity(cityId, pointOfInterestEntity); _cityInfoRepository.Save(); return(NoContent()); }
[HttpPatch("{cityId}/pointsofinterest/{id}")] //partially update a point of interest public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id, [FromBody] JsonPatchDocument <PointOfInterestForUpdatingDto> jsonDoc) { if (jsonDoc == null || !ModelState.IsValid) //repeaded calidation here, fluent validation should be looked at. { return(BadRequest(ModelState)); } var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId); if (city == null) { return(NotFound()); } var pointOfInterestFromStore = city.PointsOfInterest.FirstOrDefault(p => p.Id == id); if (pointOfInterestFromStore == null) { return(NotFound()); } var pointOfInterestToPatch = new PointOfInterestForUpdatingDto() { Name = pointOfInterestFromStore.Name, Description = pointOfInterestFromStore.Description }; jsonDoc.ApplyTo(pointOfInterestToPatch, ModelState); if (!ModelState.IsValid) //this will be valid as the model state is currently based on the Patch document { return(BadRequest(ModelState)); } if (pointOfInterestToPatch.Name == pointOfInterestToPatch.Description) { ModelState.AddModelError("Description", "The provided description should be different from the name"); return(BadRequest(ModelState)); } TryValidateModel(pointOfInterestToPatch); //trys to validate the model and adds it to the model state if (!ModelState.IsValid) //if the dto to patch is invalied based on annotation this will not be false. Where as above it was true { return(BadRequest(ModelState)); } pointOfInterestFromStore.Name = pointOfInterestToPatch.Name; pointOfInterestFromStore.Description = pointOfInterestToPatch.Description; return(NoContent()); //for updates no content or ok is the standard. }
public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id, [FromBody] JsonPatchDocument <PointOfInterestForUpdatingDto> patchDocument) { if (patchDocument == null) { return(BadRequest()); } var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId); if (city == null) { return(NotFound()); } var pointOfInterestFromStore = city.PointsOfInterest.FirstOrDefault(p => p.Id == id); if (pointOfInterestFromStore == null) { return(NotFound()); } var pointOfInterestToPatch = new PointOfInterestForUpdatingDto() { Name = pointOfInterestFromStore.Name, Description = pointOfInterestFromStore.Description }; patchDocument.ApplyTo(pointOfInterestToPatch, ModelState); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (pointOfInterestToPatch.Description == pointOfInterestToPatch.Name) { ModelState.AddModelError("Description", "The provided description cannot be the same as the name"); } TryValidateModel(pointOfInterestToPatch); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } pointOfInterestFromStore.Name = pointOfInterestToPatch.Name; pointOfInterestFromStore.Description = pointOfInterestToPatch.Description; return(NoContent()); }
public IActionResult PartiallyUpdatePointsOfInterest(int cityId, int id, [FromBody] JsonPatchDocument <PointOfInterestForUpdatingDto> patchdoc) { if (patchdoc == null) { _logger.LogInformation($"The request body sent is not valid"); return(BadRequest()); } var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId); if (city == null) { _logger.LogInformation($"City wiht id {cityId} wasn't found when accessing points of interest"); return(NotFound()); } var poi = city.PointsOfInterest.FirstOrDefault(p => p.Id == id); if (poi == null) { _logger.LogInformation($"Point of Interest wiht id {id} wasn't found when accessing points of interest with city id {cityId}"); return(NotFound()); } var pointOfInterestToPatch = new PointOfInterestForUpdatingDto() { Name = poi.Name, Description = poi.Description }; patchdoc.ApplyTo(pointOfInterestToPatch, ModelState); TryValidateModel(pointOfInterestToPatch); if (!ModelState.IsValid) { _logger.LogInformation($"The request model sent is not valid"); return(BadRequest()); } poi.Name = pointOfInterestToPatch.Name; poi.Description = pointOfInterestToPatch.Description; return(NoContent()); }
public IActionResult UpdatePointOfInterest(int cityId, int pId, [FromBody] PointOfInterestForUpdatingDto pointOfInterestForUpdating) { if (pointOfInterestForUpdating == null) { _logger.LogInformation($"The request body sent is not valid"); return(BadRequest()); } if (!ModelState.IsValid) { _logger.LogInformation($"The Model State is not valid"); return(BadRequest(ModelState)); } var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId); if (city == null) { _logger.LogInformation($"City wiht id {cityId} wasn't found when accessing points of interest"); return(NotFound(new { cityId })); } var pointOfInterest = city.PointsOfInterest .FirstOrDefault(p => p.Id == pId); if (pointOfInterest == null) { _logger.LogInformation($"Point of Interest wiht id {pId} wasn't found when accessing points of interest with city id {cityId}"); return(NotFound(new { pId })); } pointOfInterest.Name = pointOfInterestForUpdating.Name; pointOfInterest.Description = pointOfInterestForUpdating.Description; return(NoContent()); }