public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointsOfInterestUpdateDto pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } if (!ModelState.IsValid) { 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()); } pointOfInterestFromStore.Name = pointOfInterest.Name; pointOfInterestFromStore.Description = pointOfInterest.Description; return(NoContent()); }
public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id, [FromBody] JsonPatchDocument <PointsOfInterestUpdateDto> patchDoc) { if (patchDoc == 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 pointOfInterstToPatch = new PointsOfInterestUpdateDto() { Name = pointOfInterestFromStore.Name, Description = pointOfInterestFromStore.Description }; patchDoc.ApplyTo(pointOfInterstToPatch, ModelState); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } pointOfInterestFromStore.Name = pointOfInterstToPatch.Name; pointOfInterestFromStore.Description = pointOfInterstToPatch.Description; return(NoContent()); }
public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointsOfInterestUpdateDto pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } 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); if (!_cityInfoRepository.Save()) { return(StatusCode(500, "A problem happened while handling your request.")); } return(NoContent()); }
public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointsOfInterestUpdateDto pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } 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 finalPointOfInterest = Mapper.Map <Entities.PointOfInterest>(pointOfInterest); _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest); if (!_cityInfoRepository.Save()) { return(StatusCode(500, "A problem happened while handling your request.")); } var createdPointOfInterestToReturn = Mapper.Map <Models.PointOfInterestDto>(finalPointOfInterest); return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = createdPointOfInterestToReturn.Id }, createdPointOfInterestToReturn)); }