diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionService.java b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionService.java index 47a8e549..e0c420e4 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionService.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionService.java @@ -64,7 +64,9 @@ public Question findRandomQuestion(String language, List quest } public QuestionResponseDto getQuestionById(Long id) { - return questionResponseDtoMapper.apply(questionRepository.findById(id).orElseThrow()); + Question q = questionRepository.findById(id).orElseThrow(); + loadAnswers(q); + return questionResponseDtoMapper.apply(q); } diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/dtos/QuestionResponseDto.java b/api/src/main/java/lab/en2b/quizapi/questions/question/dtos/QuestionResponseDto.java index 263b36fd..2c8c00c9 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/dtos/QuestionResponseDto.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/dtos/QuestionResponseDto.java @@ -39,4 +39,7 @@ public class QuestionResponseDto { @Schema(description = "Type of the question",example = "MULTIPLE_CHOICE") private QuestionType type; + + @Schema(description = "Image for the question",example = "https://www.example.com/image.jpg") + private String image; } diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/mappers/QuestionResponseDtoMapper.java b/api/src/main/java/lab/en2b/quizapi/questions/question/mappers/QuestionResponseDtoMapper.java index 2752f4e3..39be8d87 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/mappers/QuestionResponseDtoMapper.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/mappers/QuestionResponseDtoMapper.java @@ -2,6 +2,7 @@ import lab.en2b.quizapi.questions.answer.mappers.AnswerResponseDtoMapper; import lab.en2b.quizapi.questions.question.Question; +import lab.en2b.quizapi.questions.question.QuestionType; import lab.en2b.quizapi.questions.question.dtos.QuestionResponseDto; import org.springframework.stereotype.Service; @@ -12,6 +13,18 @@ public class QuestionResponseDtoMapper implements Function()) + .content("What is the capital of France?") .questionCategory(QuestionCategory.GEOGRAPHY) .type(QuestionType.TEXT) .build(); @@ -60,6 +61,7 @@ void setUp() { .text("Paris") .category(AnswerCategory.CAPITAL_CITY) .questions(List.of(defaultQuestion)) + .language("en") .questionsWithThisAnswer(List.of(defaultQuestion)) .build(); @@ -104,13 +106,29 @@ void testGetRandomQuestion() { assertEquals(response.getId(), defaultResponseDto.getId()); } + + @Test + void testGetRandomQuestionImageType() { + defaultQuestion.setType(QuestionType.IMAGE); + defaultQuestion.setContent("What is the capital of France?#* &%https://www.example.com/image.jpg"); + when(questionRepository.findRandomQuestion(any(),any())).thenReturn(defaultQuestion); + QuestionResponseDto response = questionService.getRandomQuestion("en"); + defaultResponseDto.setType(QuestionType.IMAGE); + defaultResponseDto.setImage("https://www.example.com/image.jpg"); + assertEquals(response, defaultResponseDto); + } + @Test void testGetRandomQuestionAnswersNotYetLoaded() { when(questionRepository.findRandomQuestion(any(),any())).thenReturn(defaultQuestion); defaultQuestion.setAnswers(List.of()); - QuestionResponseDto response = questionService.getRandomQuestion(""); - - assertEquals(response.getId(), defaultResponseDto.getId()); + QuestionResponseDto response = questionService.getRandomQuestion("en"); + defaultResponseDto.setAnswers(List.of(AnswerResponseDto.builder() + .id(1L) + .category(AnswerCategory.CAPITAL_CITY) + .text("Paris") + .build())); + assertEquals(response, defaultResponseDto); } @Test void testGetRandomQuestionNoQuestionsFound() { @@ -122,7 +140,7 @@ void testGetQuestionById(){ when(questionRepository.findById(any())).thenReturn(Optional.of(defaultQuestion)); QuestionResponseDto response = questionService.getQuestionById(1L); - assertEquals(response.getId(), defaultResponseDto.getId()); + assertEquals(response, defaultResponseDto); } @Test diff --git a/questiongenerator/src/main/java/Main.java b/questiongenerator/src/main/java/Main.java index 8c0b5755..6d0a7b54 100644 --- a/questiongenerator/src/main/java/Main.java +++ b/questiongenerator/src/main/java/Main.java @@ -1,12 +1,15 @@ import model.AnswerCategory; import repositories.GeneralRepositoryStorer; -import templates.BallonDOrQuestion; -import templates.CountryCapitalQuestion; -import templates.VideogamesPublisherQuestion; +import templates.*; public class Main { + + + public static void main(String[] args) { + GeneralRepositoryStorer.editConstraints(); + // TEXT if(GeneralRepositoryStorer.doesntExist(AnswerCategory.CAPITAL_CITY)) { new CountryCapitalQuestion("en"); @@ -23,10 +26,8 @@ public static void main(String[] args) { } - /* // IMAGES - - if(GeneralRepositoryStorer.doesntExist(AnswerCategory.STADIUM.toString())) { + if(GeneralRepositoryStorer.doesntExist(AnswerCategory.STADIUM)) { new StadiumQuestion("en"); new StadiumQuestion("es"); } @@ -35,7 +36,6 @@ public static void main(String[] args) { new PaintingQuestion("en"); new PaintingQuestion("es"); } - */ /* diff --git a/questiongenerator/src/main/java/model/Question.java b/questiongenerator/src/main/java/model/Question.java index dc9c3ae8..eef4ed7c 100644 --- a/questiongenerator/src/main/java/model/Question.java +++ b/questiongenerator/src/main/java/model/Question.java @@ -9,6 +9,7 @@ @Entity @Table(name = "questions") public class Question implements Storable { + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; diff --git a/questiongenerator/src/main/java/repositories/GeneralRepositoryStorer.java b/questiongenerator/src/main/java/repositories/GeneralRepositoryStorer.java index 2d130468..1ef56146 100644 --- a/questiongenerator/src/main/java/repositories/GeneralRepositoryStorer.java +++ b/questiongenerator/src/main/java/repositories/GeneralRepositoryStorer.java @@ -14,6 +14,8 @@ */ public class GeneralRepositoryStorer { + public static final String LINKCONCAT = "#* &%"; + public void saveAll(List storableList) { EntityManagerFactory emf = Jpa.getEntityManagerFactory(); EntityManager entityManager = emf.createEntityManager(); @@ -40,7 +42,26 @@ public static boolean doesntExist(AnswerCategory category) { Jpa.close(); return count == 0; + } + + public static void editConstraints() { + EntityManagerFactory emf = Jpa.getEntityManagerFactory(); + EntityManager entityManager = emf.createEntityManager(); + + entityManager.getTransaction().begin(); + + // Drop constraint "answers_category_check" from table "answers" if exists + entityManager.createNativeQuery("ALTER TABLE answers DROP CONSTRAINT IF EXISTS answers_category_check").executeUpdate(); + // Drop constraint "questions_question_category_check" from table "questions" if exists + entityManager.createNativeQuery("ALTER TABLE questions DROP CONSTRAINT IF EXISTS questions_question_category_check").executeUpdate(); + + entityManager.getTransaction().commit(); + + entityManager.close(); + Jpa.close(); } + + } diff --git a/questiongenerator/src/main/java/templates/PaintingQuestion.java b/questiongenerator/src/main/java/templates/PaintingQuestion.java index 5281284a..eafac578 100644 --- a/questiongenerator/src/main/java/templates/PaintingQuestion.java +++ b/questiongenerator/src/main/java/templates/PaintingQuestion.java @@ -2,6 +2,7 @@ import model.*; import org.json.JSONObject; +import repositories.GeneralRepositoryStorer; import java.util.ArrayList; import java.util.List; @@ -62,9 +63,9 @@ public void processResults() { answers.add(a); if (langCode.equals("es")) - questions.add(new Question(a, "¿Cuál es este cuadro? " + imageLink, QuestionCategory.ART, QuestionType.IMAGE)); + questions.add(new Question(a, "¿Cuál es este cuadro?" + GeneralRepositoryStorer.LINKCONCAT + imageLink, QuestionCategory.ART, QuestionType.IMAGE)); else - questions.add(new Question(a, "Which painting is this? " + imageLink, QuestionCategory.ART, QuestionType.IMAGE)); + questions.add(new Question(a, "Which painting is this?" + GeneralRepositoryStorer.LINKCONCAT + imageLink, QuestionCategory.ART, QuestionType.IMAGE)); } repository.saveAll(new ArrayList<>(answers)); diff --git a/questiongenerator/src/main/java/templates/StadiumQuestion.java b/questiongenerator/src/main/java/templates/StadiumQuestion.java index 9b7b83dd..2d473e4c 100644 --- a/questiongenerator/src/main/java/templates/StadiumQuestion.java +++ b/questiongenerator/src/main/java/templates/StadiumQuestion.java @@ -2,6 +2,7 @@ import model.*; import org.json.JSONObject; +import repositories.GeneralRepositoryStorer; import java.util.ArrayList; import java.util.List; @@ -55,9 +56,9 @@ public void processResults() { answers.add(a); if (langCode.equals("es")) - questions.add(new Question(a, "¿Cuál es este estadio? " + imageLink, QuestionCategory.SPORTS, QuestionType.IMAGE)); + questions.add(new Question(a, "¿Cuál es este estadio?" + GeneralRepositoryStorer.LINKCONCAT + imageLink, QuestionCategory.SPORTS, QuestionType.IMAGE)); else - questions.add(new Question(a, "Which stadium is this? " + imageLink, QuestionCategory.SPORTS, QuestionType.IMAGE)); + questions.add(new Question(a, "Which stadium is this?" + GeneralRepositoryStorer.LINKCONCAT + imageLink, QuestionCategory.SPORTS, QuestionType.IMAGE)); } repository.saveAll(new ArrayList<>(answers));