Exemplo n.º 1
0
        public Question UpdateQuestion(int questionId, UpdateQuestionDto updateQuestionDto)
        {
            var question = _context.Questions
                           .Include(q => q.Answers)
                           .FirstOrDefault(q => q.Id == questionId);

            if (question != null)
            {
                question.Text            = updateQuestionDto.Text;
                question.SecondsToAnswer = updateQuestionDto.SecondsToAnswer;
                question.Points          = updateQuestionDto.Points;
                question.Position        = updateQuestionDto.Position;
                foreach (var questionAnswer in question.Answers)
                {
                    var answer = updateQuestionDto.Answers
                                 .Find(a => a.Id == questionAnswer.Id);
                    if (answer != null)
                    {
                        questionAnswer.Text    = answer.Text;
                        questionAnswer.Correct = answer.Correct;
                    }
                }
                _context.SaveChanges();
            }
            return(GetQuestion(question.Id));
        }
Exemplo n.º 2
0
 public IActionResult Edit(Guid id, UpdateQuestionDto questionDto)
 {
     if (ModelState.IsValid && id == questionDto.Id)
     {
         var question = mapper.Map <Question>(questionDto);
         repository.Update(question);
         return(StatusCode(200, questionDto));
     }
     return(BadRequest());
 }
Exemplo n.º 3
0
        public async Task <ActionResult <QuestionDto> > UpdateQuestion(Guid questionId, UpdateQuestionDto question)
        {
            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            var questionFromRepo = _repository.GetById(questionId);

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

            _mapper.Map(question, questionFromRepo);
            _repository.Update(questionFromRepo);

            if (!await _repository.SaveChangesAsync())
            {
                throw new Exception("An error occured while trying to updating question");
            }

            return(Ok(_mapper.Map <QuestionDto>(questionFromRepo)));
        }
Exemplo n.º 4
0
 public ActionResult <Question> UpdateQuestion(int questionId, UpdateQuestionDto updateQuestionDto)
 {
     return(questionService.UpdateQuestion(questionId, updateQuestionDto));
 }