From 41240703a4a6bd95ebbc476fd901be1d44cb6c17 Mon Sep 17 00:00:00 2001 From: Dario Date: Sat, 9 Mar 2024 18:01:11 +0100 Subject: [PATCH 1/2] fix: questions now do not generate duplicate column --- .../main/java/lab/en2b/quizapi/questions/question/Question.java | 2 ++ questiongenerator/src/main/java/model/Question.java | 2 ++ 2 files changed, 4 insertions(+) diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/Question.java b/api/src/main/java/lab/en2b/quizapi/questions/question/Question.java index ef9cae58..09484751 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/Question.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/Question.java @@ -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; diff --git a/questiongenerator/src/main/java/model/Question.java b/questiongenerator/src/main/java/model/Question.java index 94e4832d..b40cd660 100644 --- a/questiongenerator/src/main/java/model/Question.java +++ b/questiongenerator/src/main/java/model/Question.java @@ -23,7 +23,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; From 9b2ac011fccffb9742a47bd5398a5a42f1f14902 Mon Sep 17 00:00:00 2001 From: Dario Date: Sat, 9 Mar 2024 19:35:59 +0100 Subject: [PATCH 2/2] fix: questions add answers now --- .../quizapi/commons/utils/InsertDataUtils.java | 2 +- .../src/main/java/model/Question.java | 7 +++++++ .../repositories/GeneralRepositoryStorer.java | 16 ++++++++++++++++ .../java/templates/CountryCapitalQuestion.java | 18 ++++++++++++++++-- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/api/src/main/java/lab/en2b/quizapi/commons/utils/InsertDataUtils.java b/api/src/main/java/lab/en2b/quizapi/commons/utils/InsertDataUtils.java index 0fa9db7a..94bab5fb 100644 --- a/api/src/main/java/lab/en2b/quizapi/commons/utils/InsertDataUtils.java +++ b/api/src/main/java/lab/en2b/quizapi/commons/utils/InsertDataUtils.java @@ -16,7 +16,7 @@ import java.util.List; @RequiredArgsConstructor -@Service +//@Service public class InsertDataUtils { private final QuestionRepository questionRepository; diff --git a/questiongenerator/src/main/java/model/Question.java b/questiongenerator/src/main/java/model/Question.java index b40cd660..a627b4f2 100644 --- a/questiongenerator/src/main/java/model/Question.java +++ b/questiongenerator/src/main/java/model/Question.java @@ -3,6 +3,7 @@ import repositories.Storable; import javax.persistence.*; +import java.util.ArrayList; import java.util.List; @Entity @@ -40,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 getAnswers() { + return answers; } } diff --git a/questiongenerator/src/main/java/repositories/GeneralRepositoryStorer.java b/questiongenerator/src/main/java/repositories/GeneralRepositoryStorer.java index bc6bb2dd..995ffc2e 100644 --- a/questiongenerator/src/main/java/repositories/GeneralRepositoryStorer.java +++ b/questiongenerator/src/main/java/repositories/GeneralRepositoryStorer.java @@ -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. @@ -21,6 +22,21 @@ public void save(Storable s){ entityManager.getTransaction().commit(); entityManager.close(); + Jpa.close(); + } + public void saveAll(List 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(); } } diff --git a/questiongenerator/src/main/java/templates/CountryCapitalQuestion.java b/questiongenerator/src/main/java/templates/CountryCapitalQuestion.java index 8588e7ab..0ea0b97b 100644 --- a/questiongenerator/src/main/java/templates/CountryCapitalQuestion.java +++ b/questiongenerator/src/main/java/templates/CountryCapitalQuestion.java @@ -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. @@ -38,6 +42,8 @@ protected void setQuery() { */ @Override protected void processResults() { + List questions = new ArrayList<>(); + List answers = new ArrayList<>(); for (int i = 0; i < results.length(); i++) { JSONObject result = results.getJSONObject(i); String countryLabel = result.getJSONObject("countryLabel").getString("value"); @@ -49,7 +55,7 @@ protected void processResults() { //Saving the answer Answer a = new Answer(capitalLabel, AnswerCategory.CITY); - repository.save(a); + answers.add(a); //Saving the question String content; @@ -57,8 +63,16 @@ protected void processResults() { 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 answers, List questions) { + for(Question q : questions) { + q.getAnswers().add(answers.get((int) (Math.random() * (answers.size()-1)))); } }