Skip to content

Commit

Permalink
chore: move feedback logic to service
Browse files Browse the repository at this point in the history
  • Loading branch information
msmrha-cen61210 committed Dec 6, 2024
1 parent ead8310 commit 09b3761
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -25,4 +32,18 @@ public Integer addAnswer(Integer runId, Integer questionId, int[] answers) {
return quizAnswerRepository.save(quizAnswer).getId();
}

public MultipleAnswersResult getAnswerFeedback(Integer questionId, List<Integer> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -47,20 +55,10 @@ public ResponseEntity<Boolean> answerQuestion(@PathVariable Integer id, @PathVar
@Transactional
@PostMapping("/quiz-question/{id}/answer")
public ResponseEntity<MultipleAnswersResult> answerMultipleChoice(@PathVariable Integer id, @RequestBody List<Integer> 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
Expand Down

0 comments on commit 09b3761

Please sign in to comment.