/// <summary> /// Record an answer for a question from an ongoing session. /// </summary> /// <returns>Answer result.</returns> /// <param name="pSessionAnswer">Session answer parameters.</param> public ResponseDTO <AnswerResultDTO> AddAnswer(SessionAnswerDTO pSessionAnswer) { if (pSessionAnswer.Session == null) { return(ResponseDTO <AnswerResultDTO> .BadRequest("Failed to add answer. Invalid session.")); } using (IUnitOfWork bUoW = _unitOfWorkFactory.GetUnitOfWork()) { var question = bUoW.QuestionRepository.Get(pSessionAnswer.Question.Id); var answers = question.Answers.Where(answer => pSessionAnswer.Answers.Select(a => a.Id).Contains(answer.Id)).ToList(); SessionAnswer sessionAnswer = new SessionAnswer { Answers = answers, Question = bUoW.QuestionRepository.Get(pSessionAnswer.Question.Id), AnswerTime = pSessionAnswer.AnswerTime }; // Get session entity from the given SessionId Session session = bUoW.SessionRepository.Get(pSessionAnswer.Session.Id) ?? throw new Exception($"Session id {pSessionAnswer.Session.Id} not found."); session.Answers.Add(sessionAnswer); AnswerResultDTO answerResult = _mapper.Map <AnswerResultDTO>(sessionAnswer); bUoW.Complete(); return(ResponseDTO <AnswerResultDTO> .Ok(answerResult.IsCorrect? "Right!" : "Wrong answer.", answerResult)); } }
public void AddAnswerShouldBeOk() { var correctAnswers = new List <AnswerDTO> { new AnswerDTO { Id = 1, Answer = "test answer 1", IsCorrect = true }, new AnswerDTO { Id = 2, Answer = "test answer 2", IsCorrect = true }, }; var sessionAnswer = new SessionAnswerDTO { Session = new SessionDTO(), Question = new QuestionDTO { Id = 1 }, Answers = correctAnswers }; _questionRepositoryMock.Setup(repo => repo.Get(It.IsAny <int>())).Returns(new Question { Id = 1, Answers = new List <Answer> { new Answer { Id = 1, IsCorrect = true }, new Answer { Id = 2, IsCorrect = true } } }); _sessionRepositoryMock.Setup(repo => repo.Get(It.IsAny <int>())).Returns(new Session { Id = 1, Answers = new List <SessionAnswer>() }); var response = _service.AddAnswer(sessionAnswer); Assert.IsTrue(response.Success); Assert.AreEqual(ResponseCode.Ok, response.Code); Assert.AreEqual("Right!", response.Message); Assert.IsTrue(response.Data.IsCorrect); }
/// <summary> /// Add an answer for the given question to the current session /// </summary> /// <returns>Answer result.</returns> /// <param name="pQuestion">Question.</param> /// <param name="pAnswers">Answers.</param> public ResponseDTO <AnswerResultDTO> AddAnswer(QuestionDTO pQuestion, IEnumerable <AnswerDTO> pAnswers) { SessionAnswerDTO sessionAnswer = new SessionAnswerDTO { Session = CurrentSession, Question = pQuestion, Answers = pAnswers.ToList(), AnswerTime = (DateTime.Now - pQuestion.ShowedMoment).Seconds }; return(_operativeService.AddAnswer(sessionAnswer)); }
/// <summary> /// Record an answer for a question from an ongoing session. /// </summary> /// <returns>Answer result.</returns> /// <param name="pSessionAnswer">Session answer parameters.</param> public ResponseDTO <AnswerResultDTO> AddAnswer(SessionAnswerDTO pSessionAnswer) { try { return(_sessionService.AddAnswer(pSessionAnswer)); } catch (Exception ex) { _logger.Error(ex, "Failed to add answer to the given session."); return(ResponseDTO <AnswerResultDTO> .InternalError( ErrorMessageHelper.FailedOperation("recording your answer"))); } }