Skip to content

Commit

Permalink
Merge pull request #140 from Arquisoft/fix-question-generation
Browse files Browse the repository at this point in the history
Empty answer fix.
  • Loading branch information
Pelayori authored Mar 11, 2024
2 parents e17b52c + 944551c commit f7e4a98
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 f7e4a98

Please sign in to comment.