Skip to content

Commit

Permalink
Fixed the empty answer bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
uo287545 committed Mar 11, 2024
1 parent e17b52c commit 944551c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 19 deletions.
7 changes: 0 additions & 7 deletions src/main/java/com/uniovi/services/QuestionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ public interface QuestionService {
*/
Optional<Question> getQuestion(Long id);

/**
* Get a random question
*
* @return The question selected
*/
Optional<Question> getRandomQuestion();

/**
* Get a random question from any category
*
Expand Down
15 changes: 3 additions & 12 deletions src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,16 @@ public Optional<Question> getQuestion(Long id) {
return questionRepository.findById(id);
}

@Override
public Optional<Question> getRandomQuestion() {
List<Question> allQuestions = questionRepository.findAll().stream()
.filter(question -> question.getLanguage().equals(LocaleContextHolder.getLocale().getLanguage())).toList();
int idx = (int) (Math.random() * allQuestions.size());
Question q = allQuestions.get(idx);
while (q.hasEmptyOptions()){
return getRandomQuestion();
}
return Optional.ofNullable(q);
}

@Override
public List<Question> getRandomQuestions(int num) {
List<Question> allQuestions = questionRepository.findAll().stream()
.filter(question -> question.getLanguage().equals(LocaleContextHolder.getLocale().getLanguage())).toList();
List<Question> res = new ArrayList<>();
for (int i = 0; i < num; i++) {
int idx = (int) (Math.random() * allQuestions.size());
while (allQuestions.get(idx).hasEmptyOptions()){
idx = (int) (Math.random() * allQuestions.size());
}
res.add(allQuestions.get(idx));
}
return res;
Expand Down

0 comments on commit 944551c

Please sign in to comment.