From 09b3761f566f266bbf359780240c006a6a7e0452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0mrha=20Michal?= Date: Fri, 6 Dec 2024 13:22:48 +0100 Subject: [PATCH] chore: move feedback logic to service --- .../quizmaster/quiz/QuizAnswerService.java | 21 ++++++++++++++++ .../quiz/QuizQuestionController.java | 24 +++++++++---------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/backend/src/main/java/cz/scrumdojo/quizmaster/quiz/QuizAnswerService.java b/backend/src/main/java/cz/scrumdojo/quizmaster/quiz/QuizAnswerService.java index acd0c4b..3c03b1b 100644 --- a/backend/src/main/java/cz/scrumdojo/quizmaster/quiz/QuizAnswerService.java +++ b/backend/src/main/java/cz/scrumdojo/quizmaster/quiz/QuizAnswerService.java @@ -2,16 +2,23 @@ import cz.scrumdojo.quizmaster.quiz.validation.PostQuizAnswerValidator; import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.RequestBody; + +import java.util.List; +import java.util.Optional; @Service public class QuizAnswerService { private final QuizAnswerRepository quizAnswerRepository; + private final QuizQuestionRepository quizQuestionRepository; private final PostQuizAnswerValidator postQuizAnswerValidator; public QuizAnswerService(QuizAnswerRepository quizAnswerRepository, + QuizQuestionRepository quizQuestionRepository, PostQuizAnswerValidator postQuizAnswerValidator) { this.quizAnswerRepository = quizAnswerRepository; + this.quizQuestionRepository = quizQuestionRepository; this.postQuizAnswerValidator = postQuizAnswerValidator; } @@ -25,4 +32,18 @@ public Integer addAnswer(Integer runId, Integer questionId, int[] answers) { return quizAnswerRepository.save(quizAnswer).getId(); } + public MultipleAnswersResult getAnswerFeedback(Integer questionId, List selectedAnswers) { + + int[] answersArray = selectedAnswers.stream().mapToInt(Integer::intValue).toArray(); + + var wrongAnswersIndexes = quizQuestionRepository.findById(questionId) + .map(QuizQuestion.getWrongAnswersIndexes(answersArray)) + .orElse(List.of()); + + var isAnsweredCorrectly = wrongAnswersIndexes.isEmpty(); + + return new MultipleAnswersResult( + isAnsweredCorrectly, + wrongAnswersIndexes); + } } diff --git a/backend/src/main/java/cz/scrumdojo/quizmaster/quiz/QuizQuestionController.java b/backend/src/main/java/cz/scrumdojo/quizmaster/quiz/QuizQuestionController.java index 0234cda..84c0706 100644 --- a/backend/src/main/java/cz/scrumdojo/quizmaster/quiz/QuizQuestionController.java +++ b/backend/src/main/java/cz/scrumdojo/quizmaster/quiz/QuizQuestionController.java @@ -16,10 +16,18 @@ public class QuizQuestionController { private final QuizQuestionService quizQuestionService; + private final QuizAnswerService quizAnswerService; + @Autowired - public QuizQuestionController(QuizQuestionRepository quizQuestionRepository, QuizQuestionService quizQuestionService) { + public QuizQuestionController( + QuizQuestionRepository quizQuestionRepository, + QuizQuestionService quizQuestionService, + QuizAnswerService quizAnswerService) { + this.quizQuestionRepository = quizQuestionRepository; this.quizQuestionService = quizQuestionService; + this.quizAnswerService = quizAnswerService; + } @Transactional @@ -47,20 +55,10 @@ public ResponseEntity answerQuestion(@PathVariable Integer id, @PathVar @Transactional @PostMapping("/quiz-question/{id}/answer") public ResponseEntity answerMultipleChoice(@PathVariable Integer id, @RequestBody List answers) { - int[] answersArray = answers.stream().mapToInt(Integer::intValue).toArray(); - - var wrongAnswersIndexes = findQuestion(id).map(QuizQuestion.getWrongAnswersIndexes(answersArray)).orElse(List.of()); - var isAnsweredCorrectly = wrongAnswersIndexes.isEmpty(); + var result = quizAnswerService.getAnswerFeedback(id, answers); - return response( - Optional.of( - new MultipleAnswersResult( - isAnsweredCorrectly, - wrongAnswersIndexes - ) - ) - ); + return response(Optional.of(result)); } @Transactional