Beispiel #1
0
        public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForUpdateDTO updatePoi)
        {
            if (updatePoi.Name == updatePoi.Description)
            {
                ModelState.AddModelError(
                    "Description",
                    "The provided description should be different than the name"
                    );
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

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

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

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

            poi.Name        = updatePoi.Name;
            poi.Description = updatePoi.Description;

            return(NoContent());
        }
Beispiel #2
0
        public IActionResult PartialUpdatePointOfInterest(int idCity, int id, [FromBody] JsonPatchDocument <PointOfInterestForUpdateDTO> patchDoc)
        {
            if (patchDoc == null)
            {
                return(BadRequest());
            }
            var city = (CitiesDataStore.Current.Cities.FirstOrDefault(m => m.ID.Equals(idCity)));

            if (city == null)
            {
                return(NotFound());
            }
            var point = city.PointOfInterest.FirstOrDefault(p => p.ID.Equals(id));

            if (point == null)
            {
                return(BadRequest());
            }
            var updatePoint = new PointOfInterestForUpdateDTO()
            {
                Name        = point.Name,
                Description = point.Description
            };

            patchDoc.ApplyTo(updatePoint, ModelState);
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }


            point.Name        = updatePoint.Name;
            point.Description = updatePoint.Description;
            return(NoContent());
        }
Beispiel #3
0
        public IActionResult UpdatePointOfInterest(int idCity, int id, [FromBody] PointOfInterestForUpdateDTO pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            var city = (CitiesDataStore.Current.Cities.FirstOrDefault(m => m.ID.Equals(idCity)));

            if (city == null)
            {
                return(NotFound());
            }
            var point = city.PointOfInterest.FirstOrDefault(p => p.ID.Equals(id));

            if (point == null)
            {
                return(BadRequest());
            }

            point.Name        = pointOfInterest.Name;
            point.Description = pointOfInterest.Description;
            return(NoContent());
        }
Beispiel #4
0
        public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id,
                                                            [FromBody] JsonPatchDocument <PointOfInterestForUpdateDTO> patchDoc)
        {
            // patchDoc is null
            if (patchDoc == null)
            {
                return(BadRequest("patchDoc cannot be null."));
            }
            // city not found
            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

            if (city == null)
            {
                return(NotFound("City not found."));
            }

            // poi not found
            var poiFromStore = city.PointsOfInterest.FirstOrDefault(p => p.Id == id);

            if (poiFromStore == null)
            {
                return(NotFound("Point of interest not found."));
            }

            // using a poi that has an id and just not 'passing it' when instantiating the other DTO type.
            var pointOfInterestToPatch =
                new PointOfInterestForUpdateDTO()
            {
                Name        = poiFromStore.Name,
                Description = poiFromStore.Description
            };

            // if request was not valid, model state will be updated
            patchDoc.ApplyTo(pointOfInterestToPatch, ModelState);
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Adding a customer error and customized validation checking
            if (pointOfInterestToPatch.Description == pointOfInterestToPatch.Name)
            {
                ModelState.AddModelError("Description", "The provided description cannot be the same as the name.");
            }

            // run validation - trigger it manually...
            TryValidateModel(pointOfInterestToPatch);
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Update ...
            poiFromStore.Name        = pointOfInterestToPatch.Name;
            poiFromStore.Description = pointOfInterestToPatch.Description;
            return(NoContent());
        }
Beispiel #5
0
        public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id,
                                                            [FromBody] JsonPatchDocument <PointOfInterestForUpdateDTO> patchDoc)
        {
            var city = CityDataStore.Current.Cities
                       .FirstOrDefault(city => city.Id == cityId);

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

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

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

            var pointOfInterestToPatch = new PointOfInterestForUpdateDTO()
            {
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.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 from the name."
                    );
            }

            var valid = TryValidateModel(pointOfInterestToPatch);

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


            pointOfInterest.Name        = pointOfInterestToPatch.Name;
            pointOfInterest.Description = pointOfInterestToPatch.Description;

            return(NoContent());
        }
Beispiel #6
0
        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(c => c.Id == id);

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

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

            patchDoc.ApplyTo(pointOfInterestToPatch, ModelState);

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

            if (pointOfInterestToPatch.Description == pointOfInterestToPatch.Name)
            {
                ModelState.AddModelError("Description", "blablabla");
            }

            TryValidateModel(pointOfInterestToPatch);

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

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

            return(NoContent());
        }
Beispiel #7
0
        public IActionResult UpdatePointOfInterest(int cityId, int pointOfInterestId,
                                                   [FromBody] PointOfInterestForUpdateDTO pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if (pointOfInterest.Name == pointOfInterest.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());
            }

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

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

            _mapper.Map(pointOfInterest, pointOfInterestEntity); //overwrites ponitofinterestentity with values of pointofinterest

            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "A problem happened while handling your request!"));
            }
            //var pointOfInterestFromStore = city.PointsOfInterest.FirstOrDefault(p =>
            //    p.Id == pointOfInterestId);

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

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

            return(NoContent());
        }
Beispiel #8
0
        [HttpPut("{cityId}/pointsofinterest/{id}")]         // Acc. to HTTP standard, PUT should fully update the resource
        public IActionResult UpdatePointOfInterest(int cityId, int id,
                                                   [FromBody] PointOfInterestForUpdateDTO pointOfInterest)
        {
            // null poi
            if (pointOfInterest == null)
            {
                return(BadRequest("Point of Interest not defined."));
            }

            // Adding a customer error and customized validation checking
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "The provided description cannot be the same as the name.");
            }

            // ModelState is a dictionary containing various data
            // If model validation fails, isvalid will be false.
            if (!ModelState.IsValid)
            {
                // will pass along any error messages in modelstate to the response
                return(BadRequest(ModelState));
            }

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

            if (city == null)
            {
                return(NotFound("City not found."));
            }

            // poi not found
            var poiFromStore = city.PointsOfInterest.FirstOrDefault(p => p.Id == id);

            if (poiFromStore == null)
            {
                return(NotFound("Point of interest not found."));
            }

            // Update ...
            poiFromStore.Name        = pointOfInterest.Name;
            poiFromStore.Description = pointOfInterest.Description;

            // succesful operation but no return content.
            return(NoContent());
        }
Beispiel #9
0
        public IActionResult UpdatePointOfInterest(int cityId, int id,
                                                   [FromBody] PointOfInterestForUpdateDTO pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if (pointOfInterest.Name == pointOfInterest.Description)
            {
                ModelState.AddModelError("Description", "The description must not be equal to the name.");
            }

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

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

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

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

            AutoMapper.Mapper.Map(pointOfInterest, pointOfInterestToUpdate);

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

            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", "Descrição não pode ser igual ao nome.");
            }

            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());
        }
Beispiel #11
0
        public IActionResult UpdatePointOfInterest(int cityId, int id,
                                                   [FromBody] PointOfInterestForUpdateDTO poiData)
        {
            if (poiData.Description == poiData.Name)
            {
                ModelState.AddModelError(
                    "Description",
                    "The provided description should not match the name."
                    );
            }

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

            var city = CityDataStore.Current.Cities
                       .FirstOrDefault(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        = poiData.Name;
            pointOfInterest.Description = poiData.Name;

            return(NoContent());
        }
Beispiel #12
0
        public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForUpdateDTO pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "fshsfjdd");
            }

            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());
        }