Exemplo n.º 1
0
        public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForUpdateDto pointOfInterestForUpdateDto)
        {
            if (pointOfInterestForUpdateDto == null)
            {
                return(BadRequest());
            }

            if (pointOfInterestForUpdateDto.Name == pointOfInterestForUpdateDto.Description)
            {
                ModelState.AddModelError("Description", "Name and description can not be same.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var IsPOIExist = _cityInfoRepository.IsPointOfInterestForCityExist(cityId, id); //CitiesDataStore.Current.Cities.SelectMany(x => x.PointsOfInterest).Any(x => x.Id == id);

            if (!IsPOIExist)
            {
                return(NotFound("POI with given id not exist"));
            }

            _cityInfoRepository.UpdatePointOfInterestForCity(cityId, id, pointOfInterestForUpdateDto);

            if (!_cityInfoRepository.SaveChanges())
            {
                return(StatusCode(500, "POI not updated."));
            }

            return(NoContent());
        }
        public IActionResult UpdatePointOfInterest(int cityId, int id,
                                                   [FromBody] PointOfInterestForUpdateDto 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 pointOfInterestEntity = _cityInfoRepository.GetPointOfInterestForCity(cityId, id);

            if (pointOfInterestEntity == null)
            {
                return(NotFound());
            }

            Mapper.Map(pointOfInterest, pointOfInterestEntity);

            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "A problem happend while handeling your request."));
            }

            // Means that the request completed successfully but there is nothing to return
            return(NoContent());
        }
Exemplo n.º 3
0
        public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "Enter a name value different from that of 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, "Sorry,error ecountered while handling your request."));
            }
            return(NoContent());
        }
Exemplo n.º 4
0
        public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            if (!_cityInfoRepository.CityExists(cityId))
            {
                return(NotFound());
            }

            var pointOfInterestEntity = _cityInfoRepository.GetPointOfInterest(cityId, id);

            if (pointOfInterestEntity == null)
            {
                return(NotFound());
            }

            Mapper.Map(pointOfInterest, pointOfInterestEntity);

            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "Error while handling the request"));
            }

            return(NoContent());
        }
        public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForUpdateDto newPointOfInterest)
        {
            if (newPointOfInterest == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_cityInfoRepository.CityExists(cityId))
            {
                return(NotFound());
            }

            var pointOfInterest = _cityInfoRepository.GetPointOfInterest(cityId, id);

            if (pointOfInterest == null)
            {
                return(NotFound());
            }

            AutoMapper.Mapper.Map(newPointOfInterest, pointOfInterest);

            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "A problem happend while processing your request!"));
            }

            return(NoContent());
        }
Exemplo n.º 6
0
        public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForUpdateDto 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.PointOfInterest.FirstOrDefault(p => p.Id == id);

            if (pointOfInterestFromStore == null)
            {
                return(NotFound());
            }

            pointOfInterestFromStore.Name        = pointOfInterest.Name;
            pointOfInterestFromStore.Description = pointOfInterest.Description;

            return(NoContent());
        }
Exemplo n.º 7
0
        public IActionResult UpdatePointOfInterest(int cityId, int id,
                                                   [FromBody] PointOfInterestForUpdateDto pointOfInterestForUpdate)
        {
            if (pointOfInterestForUpdate == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_repository.CityExists(cityId))
            {
                return(NotFound());
            }

            var pointOfInterest = _repository.GetPointOfInterest(cityId, id);

            if (pointOfInterest == null)
            {
                return(BadRequest(new { ErrorMessage = "This point of interest does not exist." }));
            }

            Mapper.Map(pointOfInterestForUpdate, pointOfInterest);

            if (!_repository.Save())
            {
                _logger.LogCritical($"Error when updating point of Interest for CityID {cityId}, PointOfInterest {id},{pointOfInterestForUpdate.Name}");
                return(StatusCode(500, "A problem happen when updating the point of interest. Please try again"));
            }

            return(NoContent());
        }
Exemplo n.º 8
0
        public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "The provided description should b different from the name.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

            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 UpdatePointOfInterest(int cityId, int id,
                                                   [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            if (!ModelState.IsValid || pointOfInterest == null)
            {
                return(BadRequest(ModelState));
            }

            var city = GetCity(cityId);

            if (city == null)
            {
                logInformation(cityId, "city");
                return(NotFound());
            }

            var pointOfInterestFromStore = GetPointOfInterest(city, id);

            if (pointOfInterestFromStore == null)
            {
                logInformation(cityId, "point of interest");
                return(NotFound());
            }

            pointOfInterestFromStore.Name        = pointOfInterest.Name;
            pointOfInterestFromStore.Description = pointOfInterest.Description;

            return(NoContent());
        }
        public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForUpdateDto point)
        {
            if (point.Description == point.Name)
            {
                ModelState.AddModelError("Description", "Description should be different than Name");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_repository.CityExists(cityId))
            {
                return(NotFound());
            }

            var pointOfInterestInRepo = _repository.GetPointOfInterestForCity(cityId, id);

            if (pointOfInterestInRepo == null)
            {
                return(NotFound());
            }

            _mapper.Map(point, pointOfInterestInRepo);

            _repository.Save();

            return(NoContent());
        }
        public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForUpdateDto model)
        {
            if (model.Name == model.Description)
            {
                ModelState.AddModelError(
                    "Description",
                    "The provided description should be different from the name."
                    );
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!cityInfoRepository.CityExists(cityId))
            {
                return(NotFound($"No city matching id {cityId}."));
            }

            var pointOfInterest = cityInfoRepository.GetPointOfInterestForCity(cityId, id);

            if (!cityInfoRepository.PointOfInterestExists(id))
            {
                return(NotFound($"No point of interest matching id {id}."));
            }

            mapper.Map(model, pointOfInterest);

            cityInfoRepository.UpdatePointOfInterestForCity(cityId, pointOfInterest);

            cityInfoRepository.Save();

            return(NoContent());
        }
Exemplo n.º 12
0
        public IActionResult UpdatePointOfIntrest(int cityId, int poiId, [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if (pointOfInterest.Description.Equals(pointOfInterest.Name))
            {
                ModelState.AddModelError("Description", "Description should be different from the name.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var foundCity = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

            if (foundCity == null)
            {
                NotFound();
            }
            var poi = foundCity.PointsOfInterest.FirstOrDefault(p => p.Id == poiId);

            if (poi == null)
            {
                NotFound();
            }

            poi.Name        = pointOfInterest.Name;
            poi.Description = pointOfInterest.Description;

            return(NoContent()); //204 => can also return 200OK and pass updated resource;
        }
        public IActionResult UpdatePointsOfInterest(
            int cityId,
            int id,
            [FromBody] PointOfInterestForUpdateDto pointOfInterest
            )
        {
            if (!ValidatePointOfInterest(pointOfInterest))
            {
                return(BadRequest(this.ModelState)); // Passing the ModelState to return the list of error to the client
            }
            var city = this.FindCity(cityId);

            if (city == null)
            {
                return(NotFound());
            }

            var pointOfInterestFromStore = this.FindPointOfInterest(city, id);

            if (pointOfInterestFromStore == null)
            {
                return(NotFound());
            }

            // All the fields of the resource should be in the pointOfInterest
            // object passed in the body according the http standard (REST).
            // This is a full update of the resource.
            // For a partial update see HTTP method `PATCH` (JSON PATCH (RFC 6902 https://tools.ietf.org/html/rfc6902)).
            pointOfInterestFromStore.Name        = pointOfInterest.Name;
            pointOfInterestFromStore.Description = pointOfInterest.Description;
            // Http put must return http code 204 - Ok with No Content to return
            return(NoContent());
        }
Exemplo n.º 14
0
        public IActionResult UpdatePointOfInterest(int cityId, int id,
                                                   [FromBody] PointOfInterestForUpdateDto 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.GetPointOfInterestFotCity(cityId, id);

            if (pointOfInterestEntity == null)
            {
                return(NotFound());
            }

            _mapper.Map(pointOfInterest, pointOfInterestEntity);

            _cityInfoRepository.UpdatePointOfInterestForCity(cityId, pointOfInterestEntity);

            _cityInfoRepository.Save();

            return(NoContent());
        }
        public async Task <IActionResult> UpdatePointOfInterest(int cityId, int id,
                                                                [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!await _cityInfoRepository.CityExistsAsync(cityId))
            {
                _logger.LogInformation($"city with id {cityId} not found");
                return(NotFound());
            }

            var existingPointOfInterest = await _cityInfoRepository.GetPointOfInterestForCityAsync(cityId, id);

            if (existingPointOfInterest == null)
            {
                return(NotFound());
            }

            Mapper.Map(pointOfInterest, existingPointOfInterest);
            if (!await _cityInfoRepository.SaveAsync())
            {
                return(StatusCode(500, $"Something went wrong while updating the point of interest with id {id}"));
            }
            return(NoContent());
        }
Exemplo n.º 16
0
        public IActionResult PartialliyUpdatePointOfInterest(int cityId, int id,
                                                             [FromBody] JsonPatchDocument <PointOfInterestForUpdateDto> pointOfInterestPatchDoc)
        {
            var currentCity = CitiesDataStore.Current.Cities.FirstOrDefault(city => city.Id == cityId);
            var currentPoi  = currentCity.PointsOfInterest.FirstOrDefault(poi => poi.Id == id);

            PointOfInterestForUpdateDto poiUpdate = new PointOfInterestForUpdateDto
            {
                Description = currentPoi.Description,
                Name        = currentPoi.Name
            };

            pointOfInterestPatchDoc.ApplyTo(poiUpdate, ModelState);
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var isUpdaveValid = TryValidateModel(poiUpdate);

            if (!isUpdaveValid)
            {
                return(BadRequest(ModelState));
            }

            currentPoi.Name        = poiUpdate.Name;
            currentPoi.Description = poiUpdate.Description;



            return(NoContent());
        }
        public IActionResult UpdatePointOfInterest(int cityId, int interestId,
                                                   [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            IActionResult actionResult;

            if (!IsValidationSuccessfull(cityId, interestId, pointOfInterest, out actionResult))
            {
                return(actionResult);
            }

            var city = _repository.GetCity(cityId, true);

            if (city == null)
            {
                return(NotFound(nameof(city)));
            }

            var entityFromDataBase = city.PointsOfInterest.FirstOrDefault(p => p.Id == interestId);

            if (entityFromDataBase == null)
            {
                return(NotFound(nameof(interestId)));
            }

            AutoMapper.Mapper.Map(pointOfInterest, entityFromDataBase);

            if (!_repository.Save())
            {
                return(StatusCode(500, "Some issue has been occurred while updating pointOfInterest."));
            }

            return(NoContent());
        }
Exemplo n.º 18
0
        public IActionResult UpdatePointOfInterest(int cityId, int id,
                                                   [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }
            if (pointOfInterest.Name == pointOfInterest.Description)
            {
                ModelState.AddModelError("Description", "Description should be different from Name.");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (!_cityInfoRepository.CityExists(cityId))
            {
                return(NotFound());
            }
            var pointOfInterestEntity = _cityInfoRepository.GetPointOfInterestForCity(id, cityId);

            if (pointOfInterestEntity == null)
            {
                return(NotFound());
            }
            Mapper.Map(pointOfInterest, pointOfInterestEntity);
            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "A problem happened while handling request."));
            }
            return(NoContent());
        }
Exemplo n.º 19
0
        public IActionResult UpdatePointOfInterest(int id, int idPoi, [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "Description cannot be the same as Name");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_cityInfoRepo.CityExists(id))
            {
                return(NotFound());
            }

            var poiEntity = _cityInfoRepo.GetPOIforCity(id, idPoi);

            if (poiEntity == null)
            {
                return(NotFound());
            }

            _mapper.Map(pointOfInterest, poiEntity); //source is Dto we received, destination is the entity we will persist
            _cityInfoRepo.UpdatePoiForCity(id, poiEntity);
            _cityInfoRepo.Save();

            return(NoContent());
        }
Exemplo n.º 20
0
        public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id,
                                                            [FromBody] JsonPatchDocument <PointOfInterestForUpdateDto> patchDoc)
        {
            if (patchDoc == null)
            {
                return(BadRequest());
            }


            // Does the city id exist?
            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

            if (city == null)
            {
                return(NotFound());
            }

            // Does the point of interest id exist?
            var pointOfInterestFromStore = city.PointsOfInterest.FirstOrDefault(p =>
                                                                                p.Id == id);

            if (pointOfInterestFromStore == null)
            {
                return(NotFound());
            }

            var pointOfInterestToPatch =
                new PointOfInterestForUpdateDto()
            {
                Name        = pointOfInterestFromStore.Name,
                Description = pointOfInterestFromStore.Description
            };

            patchDoc.ApplyTo(pointOfInterestToPatch, ModelState);

            // Is the PointOfInterest request coming in valid?
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (pointOfInterestToPatch.Name == pointOfInterestToPatch.Description)
            {
                ModelState.AddModelError("Description", "The Description should be different than the Name");
            }

            TryValidateModel(pointOfInterestToPatch);

            // Is PointOfInterestForUpdateDto still valid after PatchDocument request has been appled?
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            pointOfInterestFromStore.Name        = pointOfInterestToPatch.Name;
            pointOfInterestFromStore.Description = pointOfInterestToPatch.Description;

            return(NoContent());
        }
Exemplo n.º 21
0
        public IActionResult UpdatePointOfInterest(int cityId, int id,
                                                   [FromBody] PointOfInterestForUpdateDto 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));
            }

            #region Using in memory data store

            // Using in memory data store
            //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();

            #endregion


            // Using persistent data store
            if (!_repository.CityExists(cityId))
            {
                return(NotFound());
            }

            var pointOfInterestEntity = _repository.GetPointOfInterestForCity(cityId, id);
            if (pointOfInterestEntity == null)
            {
                return(NotFound());
            }

            Mapper.Map(pointOfInterest, pointOfInterestEntity);

            if (!_repository.Save())
            {
                return(StatusCode(500, "A problem happened while handling your request."));
            }

            return(NoContent());
        }
Exemplo n.º 22
0
        public IActionResult UpdatePointOfInterest(int cityId, int id,
                                                   [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if (pointOfInterest.Name == pointOfInterest.Description)
            {
                ModelState.AddModelError("Description", "Name ve Description alanı aynı içeriğe sahip olamaz.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //var city = CitiesDataStore.Current.Cities.FirstOrDefault(p => p.Id == cityId);

            //if (city == null)
            //    return NotFound();
            //yukarıdaki kodlar yerine aşağıdaki kodlar kullanılıyor.

            if (!_cityInfoRepository.CityExists(cityId))
            {
                return(NotFound());
            }


            //var pointOfInterestFromStore = city.PointsOfInterest.FirstOrDefault(p => p.Id == id);

            //if (pointOfInterestFromStore == null)
            //    return NotFound();
            //yukarıdaki kodlar yerine aşağıdaki kodlar çalışıyor.

            var pointOfInterestEntity = _cityInfoRepository.GetPointOfInterestForCity(cityId, id);

            if (pointOfInterestEntity == null)
            {
                return(NotFound());
            }

            //pointOfInterestFromStore.Name = pointOfInterest.Name;
            //pointOfInterestFromStore.Description = pointOfInterest.Description;
            //yukarıdaki kodlar yerine aşağıdaki kod çalışıyor.

            Mapper.Map(pointOfInterest, pointOfInterestEntity);

            //değişiklikleri kaydediyoruz.
            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "a problem happened while handling your request."));
            }


            return(NoContent());
        }
Exemplo n.º 23
0
        public IActionResult PartiallyUpdatePointOfInterestIM(int cityId, int id, [FromBody] JsonPatchDocument <PointOfInterestForUpdateDto> patchDoc)
        {
            // If we can't deserialise the patch document from the body, just return BadRequest.
            if (patchDoc == null)
            {
                return(BadRequest());
            }

            // If we can't find the city, return NotFound, as before.
            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());
            }

            // Need to transform the pointOfInterestDto we got from the store to a pointOfInterestForUpdateDto that we got from the PATCH request
            var pointOfInterestToPatch = new PointOfInterestForUpdateDto()
            {
                Name        = pointOfInterestFromStore.Name,
                Description = pointOfInterestFromStore.Description
            };

            // We can pass in the ModelState to get the validation that has been set up on the model.
            patchDoc.ApplyTo(pointOfInterestToPatch, ModelState);

            // And then check it's valid
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (pointOfInterestToPatch.Description == pointOfInterestToPatch.Name)
            {
                ModelState.AddModelError("Description", "The provided description should be different from the name.");
            }

            TryValidateModel(pointOfInterestToPatch);

            // And then check it's valid
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            pointOfInterestFromStore.Name        = pointOfInterestToPatch.Name;
            pointOfInterestFromStore.Description = pointOfInterestToPatch.Description;

            return(NoContent());
        }
Exemplo n.º 24
0
        public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id,
                                                            [FromBody] JsonPatchDocument <PointOfInterestForUpdateDto> patchDocument)
        {
            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());
            }

            // Since the patch is going to work on POIForUpdateDto (we specified above) you'll need to transform the POI from the
            // DataStore to POIForUpdateDto format (which transfers all the existing data to new variable we can then apply patch to)
            var pointOfInterestToPatch =
                new PointOfInterestForUpdateDto()
            {
                Name        = pointOfInterestFromStore.Name,
                Description = pointOfInterestFromStore.Description
            };

            // PATCH newly created POI instance along with ModelState to check our model for validation requirements
            patchDocument.ApplyTo(pointOfInterestToPatch, ModelState);
            // If validation fails return 400 level response to client
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            // ModelState is being checked in this case on the inputted model. Which in this case is not one of our DTO models
            // It's a JsonPatchDocument .. which means it could pass simple modelstate validation but in fact still be malformed
            // So we need to explicity check the DTO model after applying the patch document
            // First adding our custom rule we've made for name != description
            if (pointOfInterestToPatch.Name == pointOfInterestToPatch.Description)
            {
                ModelState.AddModelError(
                    "Description",
                    "The provided description should be different from the name");
            }
            // Trigger validation on POIToPatch. If this resolves to false, modelstate will be invalid
            if (!TryValidateModel(pointOfInterestToPatch))
            {
                return(BadRequest(ModelState));
            }
            // Remaining code works exactly the same at PUT update
            // Here you assign the new request body values to the POI object residing in DataStore memory in this demo case
            pointOfInterestFromStore.Name        = pointOfInterestToPatch.Name;
            pointOfInterestFromStore.Description = pointOfInterestToPatch.Description;

            // Return succesful 204 response code
            return(NoContent());
        }
Exemplo n.º 25
0
        public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id,
                                                            [FromBody] JsonPatchDocument <PointOfInterestForUpdateDto> 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 PointOfInterestForUpdateDto()
            {
                Name        = pointOfInterestFromStore.Name,
                Description = pointOfInterestFromStore.Description
            };

            patchDocument.ApplyTo(pointOfInterestToPatch, ModelState);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //TryValidateModel(pointOfInterestToPatch);  // TODO: research

            pointOfInterestFromStore.Name        = pointOfInterestToPatch.Name;
            pointOfInterestFromStore.Description = pointOfInterestToPatch.Description;

            return(NoContent());

            // use this in the body in PostMan
            //[
            // {
            //      "op": "replace",
            //      "path": "/name",
            //      "value": "new name value"
            // },
            // {
            //      "op": "replace",
            //      "path": "/description",
            //      "value": "new description value"
            // },
            //]
        }
 private void CheckPointOfinterestNameAndDescriptionAreNotTheSame(PointOfInterestForUpdateDto pointOfInterest)
 {
     // Custom validation rule that will update the ModelState
     // For more validation see FluentValidation library by Jemery Skinner
     if (pointOfInterest.Name == pointOfInterest.Description)
     {
         ModelState.AddModelError("description", "The description field cannot be equal to the name");
     }
 }
Exemplo n.º 27
0
        public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id,
                                                            [FromBody] JsonPatchDocument <PointOfInterestForUpdateDto> patchDoc)
        {
            if (patchDoc == null)
            {
                return(BadRequest());
            }

            var city = CitiesDataStore.Current.Citites.FirstOrDefault(c => c.Id == cityId);

            if (city == null)
            {
                return(NotFound());
            }


            var poi = city.PointsOfInterest.FirstOrDefault(c => c.Id == id);

            if (poi == null)
            {
                return(NotFound());
            }


            var poiToPatch = new PointOfInterestForUpdateDto()
            {
                Name        = poi.Name,
                Description = poi.Description,
            };

            patchDoc.ApplyTo(poiToPatch, ModelState);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (poiToPatch.Description == poiToPatch.Name)
            {
                ModelState.AddModelError("Description", "The provided description should be different from the name.");
            }

            TryValidateModel(poiToPatch);


            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            poi.Name        = poiToPatch.Name;
            poi.Description = poiToPatch.Description;


            return(NoContent()); // or return OK(poi);
        }
Exemplo n.º 28
0
        public IActionResult PartiallyUpdatePointOfInterest(int cityId, int pId, [FromBody] JsonPatchDocument <PointOfInterestForUpdateDto> 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 == pId);

            if (pointOfInterestFromStore == null)
            {
                return(NotFound());
            }

            var pointOfInterestToPatch = new PointOfInterestForUpdateDto()
            {
                Name        = pointOfInterestFromStore.Name,
                Description = pointOfInterestFromStore.Description
            };

            patchDoc.ApplyTo(pointOfInterestToPatch, ModelState);

            // this check against input model
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // business error
            if (pointOfInterestToPatch.Name == pointOfInterestToPatch.Description)
            {
                ModelState.AddModelError("Description", "The provided ...");
            }

            // + model error
            TryValidateModel(pointOfInterestToPatch);

            // check against update model dto.
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // update
            pointOfInterestFromStore.Name        = pointOfInterestToPatch.Name;
            pointOfInterestFromStore.Description = pointOfInterestToPatch.Description;

            return(NoContent());
        }
        public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id,
                                                            [FromBody] JsonPatchDocument <PointOfInterestForUpdateDto> 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());
            }

            // check if patch document was correctly deserialized to a object of type JsonPatchDocument<PointOfInterestForUpdateDto>
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var pointOfInterestToPatch = new PointOfInterestForUpdateDto
            {
                Name        = pointOfInterestFromStore.Name,
                Description = pointOfInterestFromStore.Description
            };

            // apply the patch update
            // adds any errors to ModelState (if not supplied will throw exception and return code 500)
            patchDocument.ApplyTo(pointOfInterestToPatch, ModelState);

            //now validate PointOfInterestForUpdateDto itself
            if (pointOfInterestToPatch.Name == pointOfInterestToPatch.Description)
            {
                ModelState.AddModelError("Description", "Name and Description cannot be the same");
            }

            TryValidateModel(pointOfInterestToPatch);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            pointOfInterestFromStore.Name        = pointOfInterestToPatch.Name;
            pointOfInterestFromStore.Description = pointOfInterestToPatch.Description;

            return(NoContent());
        }
        public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id,
                                                            [FromBody] JsonPatchDocument <PointOfInterestForUpdateDto> 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 PointOfInterestForUpdateDto
            {
                Name        = pointOfInterestFromStore.Name,
                Description = pointOfInterestFromStore.Description
            };

            //apply pointOfInterestToPatch to patchDoc
            patchDoc.ApplyTo(pointOfInterestToPatch, ModelState);

            //check if ModelState is valid and return bad request if not
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            pointOfInterestFromStore.Name        = pointOfInterestToPatch.Name;
            pointOfInterestFromStore.Description = pointOfInterestToPatch.Description;

            if (pointOfInterestToPatch.Name == pointOfInterestToPatch.Description)
            {
                ModelState.AddModelError("Description", "Description must be different than the name!");
            }

            TryValidateModel(pointOfInterestToPatch);

            //check again if ModelState is valid and return bad request if not
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            return(NoContent());
        }