public IActionResult UpdateTalkForSpeaker(int speakerId,
                                                  int talkId,
                                                  TalkUpdateDto talk)
        {
            if (!_speakerRepository.SpeakerExists(speakerId))
            {
                return(NotFound());
            }

            var talkForSpeaker = _talkRepository.GetTalk(speakerId, talkId);

            if (talkForSpeaker == null)
            {
                var talkToAdd = _mapper.Map <Talk>(talk);
                talkToAdd.Id = talkId;

                _talkRepository.AddTalk(speakerId, talkToAdd);
                _talkRepository.Save();
                var talkToReturn = _mapper.Map <TalkDto>(talkToAdd);

                return(CreatedAtRoute("GetTalkForSpeaker",
                                      new { speakerId, talkId = talkToReturn.Id },
                                      talkToReturn));
            }

            _mapper.Map(talk, talkForSpeaker);
            _talkRepository.UpdateTalk(talkForSpeaker);
            _talkRepository.Save();
            return(NoContent());
        }
Пример #2
0
        public IActionResult Update(int id, [FromBody] TalkUpdateDto talkUpdateDto)
        {
            if (talkUpdateDto == null)
            {
                return(BadRequest());
            }

            Talk existingTalk = _talksRepository.GetSingle(id, User.Identity.Name);

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

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

            AutoMapper.Mapper.Map(talkUpdateDto, existingTalk);

            _talksRepository.Update(existingTalk);

            if (!_talksRepository.Save())
            {
                throw new Exception("Updating a fooditem failed on save.");
            }

            return(Ok(AutoMapper.Mapper.Map <TalkDto>(existingTalk)));
        }
        public ActionResult PartiallyUpdateTalkForSpeaker(int speakerId,
                                                          int talkId,
                                                          JsonPatchDocument <TalkUpdateDto> patchDocument)
        {
            if (!_speakerRepository.SpeakerExists(speakerId))
            {
                return(NotFound());
            }

            var talkForSpeakerFromRepo = _talkRepository.GetTalk(speakerId, talkId);

            if (talkForSpeakerFromRepo == null)
            {
                var talkDto = new TalkUpdateDto();
                patchDocument.ApplyTo(talkDto, ModelState);

                if (!TryValidateModel(talkDto))
                {
                    return(ValidationProblem(ModelState));
                }

                var talkToAdd = _mapper.Map <Talk>(talkDto);
                talkToAdd.Id = talkId;

                _talkRepository.AddTalk(speakerId, talkToAdd);
                _talkRepository.Save();

                var talkToReturn = _mapper.Map <TalkDto>(talkToAdd);

                return(CreatedAtRoute("GetTalkForSpeaker",
                                      new { speakerId, talkId = talkToReturn.Id },
                                      talkToReturn));
            }

            var talkToPatch = _mapper.Map <TalkUpdateDto>(talkForSpeakerFromRepo);

            // add validation

            patchDocument.ApplyTo(talkToPatch);
            if (!TryValidateModel(talkToPatch))
            {
                return(ValidationProblem(ModelState));
            }

            _mapper.Map(talkToPatch, talkForSpeakerFromRepo);

            _talkRepository.UpdateTalk(talkForSpeakerFromRepo);

            _talkRepository.Save();

            return(NoContent());
        }