private IActionResult UpsertCharacterFromPatch(Guid id, JsonPatchDocument <CharacterForUpdateDto> patchDoc)
        {
            var characterDto = new CharacterForUpdateDto();

            patchDoc.ApplyTo(characterDto, ModelState);

            ModelState.CheckModelForSameNameAndPlanet(characterDto);
            TryValidateModel(characterDto);
            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            var characterToAdd = Mapper.Map <Character>(characterDto);

            characterToAdd.Id = id;

            repository.AddCharacter(characterToAdd);

            Save(exceptionMessage: $"Upserting character {id} failed on save.");

            var characterToReturn = Mapper.Map <CharacterDto>(characterToAdd);

            return(CreatedAtRoute("GetCharacter",
                                  new { id = characterToReturn.Id },
                                  characterToReturn));
        }
        public IActionResult UpdateCharacter(Guid id,
                                             [FromBody] CharacterForUpdateDto character)
        {
            if (character == null)
            {
                return(BadRequest());
            }

            var characterFromRepo = repository.GetCharacter(id);

            if (characterFromRepo == null)
            {
                return(UpsertCharacterFromPut(id, character));
            }

            Mapper.Map(character, characterFromRepo);

            ModelState.CheckModelForSameNameAndPlanet(characterFromRepo);
            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            repository.UpdateCharacter(characterFromRepo);

            Save(exceptionMessage: $"PUT character {id} falied on save.");

            return(NoContent());
        }
示例#3
0
        public async Task UpdateCharacterAsync(CharacterForUpdateDto dto, Guid id)
        {
            var character = await _characterRepository.GetSingleAsync(id);

            character.Update(dto.Planet, dto.Episodes);

            await _characterRepository.SaveChangesAsync();
        }
示例#4
0
        public async Task <IActionResult> UpdateCharacterAsync([FromBody] CharacterForUpdateDto dto, string id)
        {
            if (!await _characterService.CheckIfCharacterExistsAsync(new Guid(id)))
            {
                return(NotFound("Character with given id does not exist"));
            }

            await _characterService.UpdateCharacterAsync(dto, new Guid(id));

            return(NoContent());
        }
        private IActionResult UpsertCharacterFromPut(Guid id, CharacterForUpdateDto character)
        {
            var characterToAdd = Mapper.Map <Character>(character);

            characterToAdd.Id = id;

            ModelState.CheckModelForSameNameAndPlanet(characterToAdd);
            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            repository.AddCharacter(characterToAdd);

            Save(exceptionMessage: $"Upserting character {characterToAdd.Id} failed on save.");

            var characterToReturn = Mapper.Map <CharacterDto>(characterToAdd);

            return(CreatedAtRoute("GetCharacter",
                                  new { id = characterToReturn.Id },
                                  characterToReturn));
        }