private void validateModelForUpdate(PointsOfInterestForUpdateDTO pointOfInterest)
        {
            if (String.IsNullOrEmpty(pointOfInterest.Name))
            {
                ModelState.AddModelError(
                    "Nome",
                    "Nome inválido");

                return;
            }

            if (pointOfInterest.Name.Length > 50)
            {
                ModelState.AddModelError(
                    "Tamanho do campo nome",
                    "O campo nome não pode ser maior que 50 caracteres");
            }

            if (!String.IsNullOrEmpty(pointOfInterest.Description) && pointOfInterest.Description.Length > 255)
            {
                ModelState.AddModelError(
                    "Tamanho do campo descrição",
                    "O campo descrição não pode ser maior que 255 caracteres");
            }
        }
        public IActionResult UpdatePointOfInterest(int cityId, int id,
                                                   [FromBody] PointsOfInterestForUpdateDTO pointsOfInterestForUpdateDTO)
        {
            validateModelForUpdate(pointsOfInterestForUpdateDTO);

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

            if (!_cityInfoRepository.CityExists(cityId))
            {
                return(NotFound("City not Found"));
            }

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

            if (poiEntity == null)
            {
                return(NotFound("Point of Interest not Found"));
            }

            _mapper.Map(pointsOfInterestForUpdateDTO, poiEntity);

            _cityInfoRepository.UpdatePointOfInterestForCity(cityId, poiEntity);

            _cityInfoRepository.Save();

            return(NoContent());
        }
Example #3
0
        public IActionResult UpdatePointOfInterest(int cityId, int pointOfInterestId,
                                                   [FromBody] PointsOfInterestForUpdateDTO pointOfInterest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var city = CitiesDataStore.Current.Cities.SingleOrDefault(x => x.Id == cityId);

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

            var pointOfInterestFromDataSore = city.PointsOfInterests.SingleOrDefault(x =>
                                                                                     x.Id == pointOfInterestId);

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

            pointOfInterestFromDataSore.Name        = pointOfInterest.Name;
            pointOfInterestFromDataSore.Description = pointOfInterest.Description;

            return(NoContent());
        }
Example #4
0
        public IActionResult PartiallyUpdatePointOfInterest(int cityId, int pointOfInterestId,
                                                            [FromBody] JsonPatchDocument <PointsOfInterestForUpdateDTO> patchDoc)
        {
            if (patchDoc == null)
            {
                return(BadRequest(ModelState));
            }

            var city = CitiesDataStore.Current.Cities.SingleOrDefault(x => x.Id == cityId);

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

            var pointOfInterest = city.PointsOfInterests.SingleOrDefault(x => x.Id == pointOfInterestId);

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

            var pointOfInterestToUpdate = new PointsOfInterestForUpdateDTO()
            {
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.Description
            };

            patchDoc.ApplyTo(pointOfInterestToUpdate, ModelState);

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

            TryValidateModel(pointOfInterestToUpdate);

            pointOfInterest.Name        = pointOfInterestToUpdate.Name;
            pointOfInterest.Description = pointOfInterest.Description;

            return(NoContent());
        }
        public IActionResult UpdatePointOfInterest(PointsOfInterestForUpdateDTO pointsOfInterest, int cityId, int id)
        {
            if (pointsOfInterest.Name == pointsOfInterest.Description)
            {
                ModelState.AddModelError("Description", "Name and Description cannot be the Same");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            var city = CitiesDataStore.Current.Cities.FirstOrDefault(x => x.Id == cityId);
            var poi  = city.PointsOfInterest.FirstOrDefault(x => x.Id == id);

            if (city == null || poi == null)
            {
                return(NotFound());
            }
            poi.Name        = pointsOfInterest.Name;
            poi.Description = pointsOfInterest.Description;
            return(NoContent());
        }