public async Task <IActionResult> PartiallyUpdateTour(Guid tourId,
                                                              [FromBody] JsonPatchDocument <TourForUpdate> jsonPatchDocument)
        {
            if (jsonPatchDocument == null)
            {
                return(BadRequest());
            }

            var tourFromRepo = await _tourManagementRepository.GetTour(tourId);

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

            var tourToPatch = Mapper.Map <TourForUpdate>(tourFromRepo);

            jsonPatchDocument.ApplyTo(tourToPatch);

            Mapper.Map(tourToPatch, tourFromRepo);

            await _tourManagementRepository.UpdateTour(tourFromRepo);

            if (!await _tourManagementRepository.SaveAsync())
            {
                throw new Exception("Updating a tour failed on save.");
            }

            return(NoContent());
        }
Exemplo n.º 2
0
        public async Task <IActionResult> PartiallyUpdateTour(Guid tourId,
                                                              [FromBody] JsonPatchDocument <TourForUpdate> jsonPatchDocument)
        {
            if (jsonPatchDocument == null)
            {
                return(BadRequest());
            }

            var tourFromRepo = await _tourManagementRepository.GetTour(tourId);

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

            // We are creating a fresh DTO from EF info
            var tourToPatch = Mapper.Map <TourForUpdate>(tourFromRepo);

            // Then we apply the patch document received from the client to the above DTO
            // Any errors duing the patching will be written to the ModelState
            jsonPatchDocument.ApplyTo(tourToPatch, ModelState);

            // Make sure there were no errors during Patching
            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            // Make sure that the DTO is valid ... I guess there must be data validation attributes on the DTO type "TourForUpdate"
            if (!TryValidateModel(tourToPatch))
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            // Now map the DTO to EF in preparation for the record to be updated
            Mapper.Map(tourToPatch, tourFromRepo);

            await _tourManagementRepository.UpdateTour(tourFromRepo);

            if (!await _tourManagementRepository.SaveAsync())
            {
                throw new Exception("Updating a tour failed on save.");
            }

            return(NoContent());
        }
        public async Task <IActionResult> PartiallyUpdateTour(Guid tourId,
                                                              [FromBody] JsonPatchDocument <TourForUpdate> jsonPatchDocument)
        {
            if (jsonPatchDocument == null)
            {
                return(BadRequest());
            }

            var tourFromRepo = await _tourManagementRepository.GetTour(tourId);

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

            var tourToPatch = Mapper.Map <TourForUpdate>(tourFromRepo);

            //To validate is patch request is well form
            jsonPatchDocument.ApplyTo(tourToPatch, ModelState);
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            //Validate DTO
            if (!TryValidateModel(tourToPatch))
            {
                return(new Helpers.UnprocessableEntityObjectResult(ModelState));
            }

            Mapper.Map(tourToPatch, tourFromRepo);

            await _tourManagementRepository.UpdateTour(tourFromRepo);

            if (!await _tourManagementRepository.SaveAsync())
            {
                throw new Exception("Updating a tour failed on save.");
            }

            return(NoContent());
        }
Exemplo n.º 4
0
        private async Task <IActionResult> UpdateSpecificTour <T> (Guid tourId, JsonPatchDocument <T> tourForUpdatePatch, bool includeShows = false) where T : class
        {
            if (tourForUpdatePatch == null)
            {
                return(BadRequest());
            }

            var tourFromRepo = await _tourManagementRepository.GetTour(tourId, includeShows);

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

            var tourForUpdate = Mapper.Map <T>(tourFromRepo);

            tourForUpdatePatch.ApplyTo(tourForUpdate, ModelState);

            if (!ModelState.IsValid)
            {
                return(UnprocessableEntity(new CustomizedValidationResult(ModelState)));
            }

            if (!TryValidateModel(tourForUpdate))
            {
                return(UnprocessableEntity(new CustomizedValidationResult(ModelState)));
            }

            Mapper.Map(tourForUpdate, tourFromRepo);

            await _tourManagementRepository.UpdateTour(tourFromRepo);

            if (await _tourManagementRepository.SaveAsync())
            {
                return(NoContent());
            }
            else
            {
                return(InternalServerError());
            }
        }
Exemplo n.º 5
0
        public async Task <IActionResult> PartiallyUpdateTour(Guid tourId, [FromBody] JsonPatchDocument <TourForUpdate> jsonPatchDocument)
        {
            if (jsonPatchDocument == null)
            {
                return(BadRequest());
            }

            var tourFromRepo = await _tourManagementRepository.GetTour(tourId);

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

            var tourToPatch = Mapper.Map <TourForUpdate>(tourFromRepo);

            jsonPatchDocument.ApplyTo(tourToPatch, ModelState);

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

            if (!TryValidateModel(tourFromRepo))
            {
                return(BadRequest());
            }

            Mapper.Map(tourToPatch, tourFromRepo);
            await _tourManagementRepository.UpdateTour(tourFromRepo);

            if (!await _tourManagementRepository.SaveAsync())
            {
                throw new Exception("Err");
            }
            return(NoContent());
        }