...

/

Implementing QuizSession

Implementing QuizSession

Let's finish making our second GenServer.

Answering a question

Now, a user can start a quiz with a start_link and a call to :select_question. What remains is to answer a question. We’ll enter them in lib/mastery/boundary/quiz_session.ex:

Press + to interact
def handle_call({:answer_question, answer}, _from, {quiz, email}) do
quiz
|> Quiz.answer_question(Response.new(quiz, email, answer))
|> Quiz.select_question
|> maybe_finish(email)
end
defp maybe_finish(nil, _email), do: {:stop, :normal, :finished, nil}
defp maybe_finish(quiz, email) do
{
:reply,
{quiz.current_question.asked, quiz.last_response.correct},
{quiz, email}
}
end
  • This function calls answer_question to answer the question and then advance the quiz.

  • It returns the presentation data that we will need later to show to the user the ...