Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: question generator bugs #103

Merged
merged 2 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.util.List;

@RequiredArgsConstructor
@Service
//@Service
public class InsertDataUtils {

private final QuestionRepository questionRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public class Question {
@NotNull
@JoinColumn(name = "correct_answer_id")
private Answer correctAnswer;
@Column(name = "question_category")
private QuestionCategory questionCategory;
@Column(name = "answer_category")
private AnswerCategory answerCategory;
private String language;
private QuestionType type;
Expand Down
9 changes: 9 additions & 0 deletions questiongenerator/src/main/java/model/Question.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import repositories.Storable;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
Expand All @@ -23,7 +24,9 @@ public class Question implements Storable {
@ManyToOne
@JoinColumn(name = "correct_answer_id")
private Answer correctAnswer;
@Column(name = "question_category")
private QuestionCategory questionCategory;
@Column(name = "answer_category")
private AnswerCategory answerCategory;
private String language;
private QuestionType type;
Expand All @@ -38,5 +41,11 @@ public Question(String content, Answer correctAnswer, QuestionCategory questionC
this.answerCategory = answerCategory;
this.language = language;
this.type = type;
this.answers = new ArrayList<>();
this.answers.add(correctAnswer);
}

public List<Answer> getAnswers() {
return answers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import java.util.List;

/**
* Class for storing entries in the Question and Answer DB.
Expand All @@ -21,6 +22,21 @@ public void save(Storable s){
entityManager.getTransaction().commit();
entityManager.close();

Jpa.close();
}
public void saveAll(List<Storable> storableList) {
EntityManagerFactory emf = Jpa.getEntityManagerFactory();

EntityManager entityManager = emf.createEntityManager();

for (Storable s : storableList) {
entityManager.getTransaction().begin();
entityManager.persist(s);
entityManager.getTransaction().commit();
}

entityManager.close();

Jpa.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import model.AnswerCategory;
import model.Question;
import org.json.JSONObject;
import repositories.Storable;

import java.util.ArrayList;
import java.util.List;

/**
* Implementation for a question where the capital of a country is asked and all the capitals are returned.
Expand Down Expand Up @@ -38,6 +42,8 @@ protected void setQuery() {
*/
@Override
protected void processResults() {
List<Question> questions = new ArrayList<>();
List<Answer> answers = new ArrayList<>();
for (int i = 0; i < results.length(); i++) {
JSONObject result = results.getJSONObject(i);
String countryLabel = result.getJSONObject("countryLabel").getString("value");
Expand All @@ -49,16 +55,24 @@ protected void processResults() {

//Saving the answer
Answer a = new Answer(capitalLabel, AnswerCategory.CITY);
repository.save(a);
answers.add(a);

//Saving the question
String content;
if (langCode.equals("en"))
content = "What is the capital of " + countryLabel + "?";
else
content = "¿Cuál es la capital de " + countryLabel + "?";
questions.add(new Question(content, a, QuestionCategory.GEOGRAPHY, AnswerCategory.CITY, langCode, QuestionType.TEXT));
}
addRandomAnswers(answers, questions);
repository.saveAll(new ArrayList<>(answers));
repository.saveAll(new ArrayList<>(questions));
}

repository.save(new Question(content, a, QuestionCategory.GEOGRAPHY, AnswerCategory.CITY, langCode, QuestionType.TEXT));
private void addRandomAnswers(List<Answer> answers, List<Question> questions) {
for(Question q : questions) {
q.getAnswers().add(answers.get((int) (Math.random() * (answers.size()-1))));
}
}

Expand Down