public IActionResult UpdatePointofInterest(int cityId, int id, [FromBody] PointOfInterestUpdateDto pointofInterestUpdate) { if (pointofInterestUpdate == null) { return(NotFound()); } if (!ModelState.IsValid) { return(BadRequest()); } var city = CitiesDataStore.current.Cities.FirstOrDefault(c => c.Id == cityId); if (city == null) { return(NotFound()); } var pointOfinterestToStore = city.Pointsofinterest.FirstOrDefault(p => p.Id == id); if (pointOfinterestToStore == null) { return(NotFound()); } pointOfinterestToStore.Name = pointofInterestUpdate.Name; pointOfinterestToStore.Description = pointofInterestUpdate.Description; return(NoContent()); }
public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestUpdateDto 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()); }
public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestUpdateDto pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } if (pointOfInterest.Description == pointOfInterest.Name) { ModelState.AddModelError("Description", "Name and Description cannot be the same."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!_cityInfoRepository.CityExists(cityId)) { return(NotFound()); } var poiFromDb = _cityInfoRepository.GetPointOfInterest(cityId, id); if (poiFromDb == null) { return(NotFound()); } Mapper.Map(pointOfInterest, poiFromDb); //stores new values if (!_cityInfoRepository.Save()) { return(StatusCode(500, "A problem occurred handling your request.")); } return(NoContent()); }
public IActionResult UpdatePointOfInterest(int cityid, int id, [FromBody] PointOfInterestUpdateDto pointOfInterestUpdate) { if (pointOfInterestUpdate == null) { return(BadRequest()); } if (!_cityInfoRepository.CityExists(cityid)) { return(NotFound()); } var pointOfInterest = _cityInfoRepository.GetPointOfInterestForCity(cityid, id); if (pointOfInterest == null) { return(NotFound()); } Mapper.Map(pointOfInterestUpdate, pointOfInterest); if (!_cityInfoRepository.Save()) { return(StatusCode(500, "A problem happened while handling your request")); } return(NoContent()); }
public IActionResult PartialUpdatePoi(int cityId, int id, [FromBody] JsonPatchDocument <PointOfInterestUpdateDto> patchDocument) { if (patchDocument == null) { return(BadRequest()); } //The ModelState does NOT work for JsonPatchDocument //if (!ModelState.IsValid) //{ // return BadRequest(ModelState); //} var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId); if (city == null) { return(NotFound()); } var poi = city.Pois.FirstOrDefault(p => p.Id == id); if (poi == null) { return(NotFound()); } var updateDto = new PointOfInterestUpdateDto() { Name = poi.Name, Description = poi.Description }; //Throws ArgumentNullException if not valid JsonPatchDocument patchDocument.ApplyTo(updateDto, ModelState); //this ApplyTo Only works for JsonPatchDocuemnt<T> //ModelState now contains error of <T> type if (updateDto.Name == updateDto.Description) { ModelState.AddModelError("Description", "Name can not be the same with Description"); } TryValidateModel(updateDto); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } poi.Name = updateDto.Name; poi.Description = updateDto.Description; _mailService.Send("poi updated", "poi updated"); return(NoContent()); }
public IActionResult CreatePointOfInterest(int templeId, [FromBody] PointOfInterestUpdateDto 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)); } // var temple = templesDataStore.Current.temples.FirstOrDefault(c => c.Id == templeId); // var temple = _templeInfoRepository.Gettemple(templeId, true); if (!_templeInfoRepository.TempleExists(templeId)) { return(NotFound()); } //for demo only //var maxPointsId = templesDataStore.Current.temples.SelectMany(c => c.PointsOfInterest).Max(m => m.Id); //var maxPointsId = temple.PointsOfInterest.Max(m => m.Id); //var finalPointOfInterest = new PointOfInterest() //{ // Id = ++maxPointsId, // Name = pointOfInterest.Name, // Description = pointOfInterest.Description //}; var finalPointOfInterest = Mapper.Map <Entities.PointOfInterest>(pointOfInterest); _templeInfoRepository.AddPointOfInterest(templeId, finalPointOfInterest); if (!_templeInfoRepository.Save()) { return(StatusCode(500, "A problem happened while processing your request")); } // temple.PointsOfInterest.Add(finalPointOfInterest); return(CreatedAtRoute("GetPointOfInterest", new { templeId = templeId, pId = finalPointOfInterest.Id }, finalPointOfInterest)); }
public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id, [FromBody] JsonPatchDocument <PointOfInterestUpdateDto> 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 pointOfInterestToPatch = new PointOfInterestUpdateDto() { Name = pointOfInterestFromStore.Name, Description = pointOfInterestFromStore.Description }; patchDoc.ApplyTo(pointOfInterestToPatch, ModelState); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (pointOfInterestToPatch.Description == pointOfInterestToPatch.Name) { ModelState.AddModelError("Description", "The provided description should be different to the name."); } TryValidateModel(pointOfInterestToPatch); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } pointOfInterestFromStore.Name = pointOfInterestToPatch.Name; pointOfInterestFromStore.Description = pointOfInterestToPatch.Description; return(NoContent()); }
public IActionResult PartillyUpdatePointOfInterest(int cityId, int id, [FromBody] JsonPatchDocument <PointOfInterestUpdateDto> patchDoc) { if (patchDoc == null) { return(BadRequest()); } //var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId); var interest = _cityInfoRepository.GetPointOfInterestForCity(cityId, id); if (_cityInfoRepository.CityExist(cityId) || interest == null) { return(NotFound()); } var pointofInterestToPatch = new PointOfInterestUpdateDto() { Name = interest.Name, Description = interest.Description }; patchDoc.ApplyTo(pointofInterestToPatch, ModelState); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (pointofInterestToPatch.Description == pointofInterestToPatch.Name) { ModelState.AddModelError("Decription", "Description must be different from name!"); } TryValidateModel(pointofInterestToPatch); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } interest.Name = pointofInterestToPatch.Name; interest.Description = pointofInterestToPatch.Description; return(NoContent()); }
public IActionResult PartialUpdatePointsofInterst(int cityId, int id, [FromBody] JsonPatchDocument <PointOfInterestUpdateDto> patchdoc) { if (patchdoc == null) { return(BadRequest()); } var city = CitiesDataStore.current.Cities.FirstOrDefault(c => c.Id == cityId); if (city == null) { return(NotFound()); } var pointofInterstStore = city.Pointsofinterest.FirstOrDefault(c => c.Id == id); if (pointofInterstStore == null) { return(NotFound()); } var pointofinterestPatch = new PointOfInterestUpdateDto() { Name = pointofInterstStore.Name, Description = pointofInterstStore.Description }; patchdoc.ApplyTo(pointofinterestPatch, ModelState); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } TryValidateModel(pointofinterestPatch); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } pointofInterstStore.Name = pointofinterestPatch.Name; pointofInterstStore.Description = pointofinterestPatch.Description; return(NoContent()); }
public IActionResult patcher(int cityId, int id, [FromBody] JsonPatchDocument <PointOfInterestUpdateDto> updatedPointOfInterest) { var city = CitiesStore.Current.Cities.FirstOrDefault(c => c.Id == cityId); if (city == null) { return(NotFound()); } var pointInterestFromStore = city.PointOfInterest.FirstOrDefault(c => c.Id == id); if (pointInterestFromStore == null) { return(NotFound()); } var pointOfInterestToPatch = new PointOfInterestUpdateDto() { Name = pointInterestFromStore.Name, Comment = pointInterestFromStore.Comment }; updatedPointOfInterest.ApplyTo(pointOfInterestToPatch, ModelState); // modelstate validates the json i.e if its properties match that of the dto. //however it doesn't handle data anotation since its json to do that, validate after //it has been applied to by the dto object. if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!TryValidateModel(pointOfInterestToPatch)) { return(BadRequest(ModelState)); } //to validate data anotation pointInterestFromStore.Name = pointOfInterestToPatch.Name; pointInterestFromStore.Comment = pointOfInterestToPatch.Comment; return(NoContent()); }
public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestUpdateDto pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (pointOfInterest.Description == pointOfInterest.Name) { ModelState.AddModelError("Description", "Description should be different form name"); } var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId); if (city == null) { return(NotFound()); } var pointOfInterestFromStore = city.PointsOfInterest.FirstOrDefault(p => p.Id == cityId); if (city == null) { return(NotFound()); } if (pointOfInterestFromStore == null) { return(NotFound()); } pointOfInterestFromStore.Name = pointOfInterest.Name; pointOfInterestFromStore.Description = pointOfInterest.Description; return(NoContent()); }
public IActionResult UpdatePointOfInterest(int templeId, int pId, [FromBody] PointOfInterestUpdateDto pointOfInterestUpdate) { if (pointOfInterestUpdate == null) { return(BadRequest()); } if (pointOfInterestUpdate.Description == pointOfInterestUpdate.Name) { ModelState.AddModelError("Description", "The provided description should be different from the name."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //first try to find the temple TempleDTO temple = Gettemple(templeId); if (temple == null) { _logger.LogInformation($"temple with templeID {templeId} was not found"); return(NotFound()); } //next find the point of interest PointOfInterestDto pointOfInterestStore = GetPointOfInterest(pId, temple); if (pointOfInterestStore == null) { return(NotFound()); } pointOfInterestStore.Name = pointOfInterestUpdate.Name; pointOfInterestStore.Description = pointOfInterestUpdate.Description; return(NoContent()); }
public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestUpdateDto pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } if (pointOfInterest.Description == pointOfInterest.Name) { ModelState.AddModelError("Decription", "Description must be different from name!"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId); //var interest = city.PointsOfInterests.FirstOrDefault(i => i.Id == id); var interest = _cityInfoRepository.GetPointOfInterestForCity(cityId, id); if (!_cityInfoRepository.CityExist(cityId) || interest == null) { return(NotFound()); } Mapper.Map(pointOfInterest, interest); if (!_cityInfoRepository.Save()) { return(StatusCode(500, "Something went wrog")); } //interest.Name = pointOfInterest.Name; //interest.Description = pointOfInterest.Description; return(NoContent()); }
public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestUpdateDto pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } if (pointOfInterest.Description == pointOfInterest.Name) { ModelState.AddModelError("Description", "Description should be deffrent 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 occured while handling your request")); } return(NoContent()); }
public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestUpdateDto pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } if (pointOfInterest.Description == pointOfInterest.Name) { ModelState.AddModelError("Description", "The provided description should not be the same as name"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId); if (city == null) { return(NotFound($"city with id: {cityId}")); } var pointOfInterestFromStore = city.PointsOfInterest.FirstOrDefault(p => p.Id == id); if (pointOfInterestFromStore == null) { return(NotFound($"point of interest with id {id} in city with id: {cityId}")); } pointOfInterestFromStore.Name = pointOfInterest.Name; pointOfInterestFromStore.Description = pointOfInterest.Description; return(NoContent()); }
public IActionResult UpdatePoi(int cityId, int id, [FromBody] PointOfInterestUpdateDto updateDto) { if (updateDto == null) { return(BadRequest()); } if (updateDto.Name.Equals(updateDto.Description)) { ModelState.AddModelError("Description", "Name can not be the same with Description"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId); if (city == null) { return(NotFound()); } var poi = city.Pois.FirstOrDefault(p => p.Id == id); if (poi == null) { return(NotFound()); } poi.Name = updateDto.Name; poi.Description = updateDto.Description; return(NoContent()); }
public IActionResult UpdatePointOfInterest(int cityId, int pointId, [FromBody] PointOfInterestUpdateDto pointOfInterest) { if (pointOfInterest == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!_repo.CityExists(cityId)) { return(NotFound()); } var pointOfInterestEntity = _repo.GetPointOfInterest(cityId, pointId); if (pointOfInterestEntity == null) { return(NotFound()); } AutoMapper.Mapper.Map(pointOfInterest, pointOfInterestEntity); if (!_repo.Save()) { return(StatusCode(500, "A problem happened while tracking your request")); } return(NoContent()); }
public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id, [FromBody] JsonPatchDocument <PointOfInterestUpdateDto> patchDoc) // we use PointOfInterestUpdateDto instead of PointOfInterestDto, because PointOfInterestDto has an id. And we don't want users to change id's. { 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(c => c.Id == id); if (pointOfInterestFromStore == null) { return(NotFound()); } var pointOfInterestToPatch = new PointOfInterestUpdateDto() { Name = pointOfInterestFromStore.Name, Description = pointOfInterestFromStore.Description }; // transform pointOfInterestToPatch from point of interest recieved from data store, to type PointOfInterestUpdateDto patchDoc.ApplyTo(pointOfInterestToPatch, ModelState); // we pass in ModelState because it knows what values are in PointOfInterestUpdateDto. So if user passes in something other than // Name/Description, it will cause ModelState to be invalid. // After doing patchDoc, it may find some of the validation rules are being broken, or bad field is being sent. So make sure it is correct here. if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Manually apply our custom error handling rule, name cannot equal description if (pointOfInterestToPatch.Description == pointOfInterestToPatch.Name) { ModelState.AddModelError("Description", "The provided description should be different from the name."); } // Make sure the model being patched is still valid (e.g. user cannot remove name, so if user tries to patch remove it, it should throw an error) // e.g. tryValidateModel triggers validation of PointOfInterestUpdateDto TryValidateModel(pointOfInterestToPatch); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Update all fields pointOfInterestFromStore.Name = pointOfInterestToPatch.Name; pointOfInterestFromStore.Description = pointOfInterestToPatch.Description; // respond with 204 (no content) on success, 200 is also fine return(NoContent()); }