Пример #1
0
        public IActionResult UpdateCity(int id, [FromBody] CityForUpdateDto city)
        {
            if (city == null)
            {
                return(BadRequest());
            }

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

            var cityFromStore = _dataStorage.Cities.Get(id);

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

            Mapper.Map(city, cityFromStore);
            _dataStorage.Cities.Update(cityFromStore);
            _dataStorage.SaveChanges();

            return(NoContent());
        }
Пример #2
0
        public IActionResult UpdateCity(int id, [FromBody] CityForUpdateDto city)
        {
            if (city.Description == city.Name)
            {
                ModelState.AddModelError(
                    "Description",
                    "The provided description should be different from the name.");
            }

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

            var cityFromRepo = _repositoryWrapper.CityInfoRepositoryWrapper.FindOne(id);

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

            _mapper.Map(city, cityFromRepo);

            _repositoryWrapper.CityInfoRepositoryWrapper.Update(cityFromRepo);
            //_repositoryWrapper.CityInfoRepositoryWrapper.Save();

            return(NoContent());
        }
Пример #3
0
        public async Task <IActionResult> UpdateCity(int cityId, CityForUpdateDto city)
        {
            var cityFromRepo = await _cityRepository.GetCityAsync(cityId);

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

            var validationResults = new CityForUpdateDtoValidator().Validate(city);

            validationResults.AddToModelState(ModelState, null);

            if (!ModelState.IsValid)
            {
                return(BadRequest(new ValidationProblemDetails(ModelState)));
                //return ValidationProblem();
            }

            _mapper.Map(city, cityFromRepo);
            _cityRepository.UpdateCity(cityFromRepo);

            await _cityRepository.SaveAsync();

            return(NoContent());
        }
Пример #4
0
        public async Task <IActionResult> UpdateCity(int userCityId, CityForUpdateDto cityForUpdateDto)
        {
            var cityToUpdate = await _repo.GetUserCity(userCityId);

            _mapper.Map <CityForUpdateDto, UserCity>(cityForUpdateDto, cityToUpdate);
            if (await _repo.SaveAll())
            {
                return(NoContent());
            }
            return(BadRequest("فشل في التعديل"));
        }
Пример #5
0
        public IActionResult UpdateCity(int id, [FromBody] CityForUpdateDto city)
        {
            //PUT updates all properties.  Properties not passed in will use their default values (strings will be null, etc)

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

            //add custom validation before checking data annotation rules
            if (city.Name == city.Description)
            {
                ModelState.AddModelError("Description", "The provided description must be different than the name.");
            }

            //validate data annotations on the model, returning error messages in the response
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //begin hardcoded data
            ////find the city
            //var cityFromStore = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == id);
            //if (cityFromStore == null)
            //    return NotFound();

            ////update the city
            //cityFromStore.Name = city.Name;
            //cityFromStore.Description = city.Description;
            //end hardcoded data

            //get entity to update
            var cityEntity = _cityInfoRepository.GetCity(id);

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

            //update the entity
            cityEntity.Name        = city.Name;
            cityEntity.Description = city.Description;

            //save the updated entity
            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "A problem happened while handling your request."));
            }

            //return a 204
            return(NoContent());
        }
Пример #6
0
        public async Task <IActionResult> UpdateCity(int cityId,
                                                     [FromBody] CityForUpdateDto cityForUpdateDto)
        {
            var cityFromRepo = await _unitOfWork.CityRepository.GetCity(cityId);

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

            _mapper.Map(cityForUpdateDto, cityFromRepo);
            await _unitOfWork.CompleteAsync();

            return(NoContent());
        }
Пример #7
0
        public IActionResult PartiallyUpdateCity(int id,
                                                 [FromBody] JsonPatchDocument <CityForUpdateDto> patchDocument)
        {
            if (patchDocument == null)
            {
                return(BadRequest());
            }

            var city = GetCityById(id);

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

            var cityToPatch = new CityForUpdateDto()
            {
                Description = city.Description,
                Name        = city.Name
            };

            patchDocument.ApplyTo(cityToPatch, ModelState);

            // Check whether ModelState is valid in patchDocument
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            //Check if after applyTo operation the ModelState in the city is valid
            TryValidateModel(cityToPatch);

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

            city.Name        = cityToPatch.Name;
            city.Description = cityToPatch.Description;

            return(NoContent());
        }
Пример #8
0
        public async Task <IActionResult> EditCity([FromBody] CityForUpdateDto city, int id, int cityId)
        {
            if (city == null)
            {
                return(BadRequest());
            }
            var cityToUpdate = await _repository.GetCityForCountry(id, cityId);

            if (cityToUpdate == null)
            {
                return(NotFound());
            }
            Mapper.Map(city, cityToUpdate);
            if (!await _repository.Save())
            {
                throw new Exception("Failed");
            }
            return(NoContent());
        }
Пример #9
0
        public IActionResult updateCity(int id,
                                        [FromBody] CityForUpdateDto cityForUpdate)
        {
            if (cityForUpdate == null || !ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var city = GetCityById(id);

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

            city.Name        = cityForUpdate.Name;
            city.Description = cityForUpdate.Description;

            return(NoContent());
        }
Пример #10
0
        public IActionResult UpdateCity(int id,
                                        [FromBody] CityForUpdateDto city)
        {
            if (city.Description == city.Name)
            {
                ModelState.AddModelError(
                    "Description",
                    "The provided description should be different from the name.");
            }

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

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

            var cityEntity = _cityInfoRepository
                             .GetCityAdvanced(id);

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

            _mapper.Map(city, cityEntity);

            _cityInfoRepository.UpdateCity(cityEntity);
            // LTPE
            // Linjen herover er egentlig ikke nødvendig med den nuværende implementation af
            // vores program, Da Enity Framework Core holder automatisk styr på vores
            // ændringer. Men kode linjen er god at have med for en sikkerheds skyld.
            // Metoden UpdateCity er for nærværende bare en tom metode !!!
            _cityInfoRepository.Save();

            return(NoContent());
        }
Пример #11
0
        public IActionResult PartiallyUpdateCity(int id, [FromBody] JsonPatchDocument <CityForUpdateDto> patchDoc)
        {
            //PATCH modifies properties as specified in the patchDoc.  Properties not in the patchDoc will not be changed.
            //Reuse the UpdateDto to include the same validation rules used for PUT

            //JSON PATCH SPEC https://tools.ietf.org/html/rfc6902

            /*
             * [
             *  {
             *      "op": "replace",
             *      "path": "/name",
             *      "value": "new name"
             *  }
             * ]
             */

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

            //begin hardcoded data
            ////find the city
            //var cityFromStore = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == id);
            //if (cityFromStore == null)
            //    return NotFound();

            ////create a new object (copy the existing city)
            //var cityToPatch = new CityForUpdateDto
            //{
            //    Name = cityFromStore.Name,
            //    Description = cityFromStore.Description
            //};

            ////apply the patch document passed in to the new object.  Pass in ModelState to capture any errors during patching.
            //patchDoc.ApplyTo(cityToPatch, ModelState);

            ////check for errors when applying the patch.  This ModelState is looking at the patchDoc, not the CityForUpdateDto
            //if (!ModelState.IsValid)
            //    return BadRequest(ModelState);

            ////validate the CityForUpdateDto is valid
            //if (cityToPatch.Name == cityToPatch.Description)
            //    ModelState.AddModelError("Description", "The provided description must be different than the name.");

            ////run the model validation rules
            //TryValidateModel(cityToPatch);

            //if (!ModelState.IsValid)
            //    return BadRequest(ModelState);

            ////update the city with the new data
            //cityFromStore.Name = cityToPatch.Name;
            //cityFromStore.Description = cityToPatch.Description;
            //end hardcoded data

            //find the city
            var cityEntity = _cityInfoRepository.GetCity(id);

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

            //create a new dto to apply the patch to (copy the existing city entity)
            var cityToPatch = new CityForUpdateDto
            {
                Name        = cityEntity.Name,
                Description = cityEntity.Description
            };

            //apply the patch document passed in to the new object.  Pass in ModelState to capture any errors during patching.
            patchDoc.ApplyTo(cityToPatch, ModelState);

            //check for errors when applying the patch.  This ModelState is looking at the patchDoc, not the CityForUpdateDto
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //validate the CityForUpdateDto is valid
            if (cityToPatch.Name == cityToPatch.Description)
            {
                ModelState.AddModelError("Description", "The provided description must be different than the name.");
            }

            //run the model validation rules
            TryValidateModel(cityToPatch);

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

            //update the city with the new data
            cityEntity.Name        = cityToPatch.Name;
            cityEntity.Description = cityToPatch.Description;

            //save the updated entity
            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "A problem happened while handling your request."));
            }

            //return a 204
            return(NoContent());
        }