Exemplo n.º 1
0
        public async Task <IHttpActionResult> AddAnswerToQuestion(int testId, int questionId, AnswerModel answerModel)
        {
            if (testId <= 0)
            {
                return(BadRequest("Incorrect test id."));
            }
            if (questionId <= 0)
            {
                return(BadRequest("Incorrect question id."));
            }
            if (answerModel == null)
            {
                return(BadRequest("Answer must not be null."));
            }

            try
            {
                answerModel.QuestionId = questionId;

                AnswerDTO answerDTO = _mapper.Map <AnswerModel, AnswerDTO>(answerModel);

                AnswerDTO createdanswer = await _testService.AddAnswerToQuestion(answerDTO);

                AnswerModel returnedTest = _mapper.Map <AnswerDTO, AnswerModel>(createdanswer);

                return(Ok(returnedTest));
            }
            catch (EntityNotFoundException)
            {
                return(NotFound());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }