Skip to content

Commit

Permalink
Merge pull request #233 from Arquisoft/chore/addImageQuestions
Browse files Browse the repository at this point in the history
Feat: Added image questions
  • Loading branch information
dariogmori authored Apr 16, 2024
2 parents cf6151e + b7afbe8 commit 0dbfed7
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public Question findRandomQuestion(String language, List<QuestionCategory> 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);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -12,6 +13,18 @@ public class QuestionResponseDtoMapper implements Function<Question, QuestionRes

@Override
public QuestionResponseDto apply(Question question) {
if(question.getType().equals(QuestionType.IMAGE))
return QuestionResponseDto.builder()
.id(question.getId())
.content(question.getContent().split("#\\* &%")[0])
.type(question.getType())
.answerCategory(question.getAnswerCategory())
.answers(question.getAnswers().stream().map(new AnswerResponseDtoMapper()).toList())
.language(question.getLanguage())
.questionCategory(question.getQuestionCategory())
.image(question.getContent().split("#\\* &%")[1])
.build();

return QuestionResponseDto.builder()
.id(question.getId())
.content(question.getContent())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ void setUp() {
defaultQuestion = Question.builder()
.id(1L)
.answers(new ArrayList<>())
.content("What is the capital of France?")
.questionCategory(QuestionCategory.GEOGRAPHY)
.type(QuestionType.TEXT)
.build();
Expand All @@ -60,6 +61,7 @@ void setUp() {
.text("Paris")
.category(AnswerCategory.CAPITAL_CITY)
.questions(List.of(defaultQuestion))
.language("en")
.questionsWithThisAnswer(List.of(defaultQuestion))
.build();

Expand Down Expand Up @@ -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() {
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions questiongenerator/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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");
}
Expand All @@ -35,7 +36,6 @@ public static void main(String[] args) {
new PaintingQuestion("en");
new PaintingQuestion("es");
}
*/


/*
Expand Down
1 change: 1 addition & 0 deletions questiongenerator/src/main/java/model/Question.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@Entity
@Table(name = "questions")
public class Question implements Storable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
public class GeneralRepositoryStorer {

public static final String LINKCONCAT = "#* &%";

public void saveAll(List<Storable> storableList) {
EntityManagerFactory emf = Jpa.getEntityManagerFactory();
EntityManager entityManager = emf.createEntityManager();
Expand All @@ -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();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import model.*;
import org.json.JSONObject;
import repositories.GeneralRepositoryStorer;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import model.*;
import org.json.JSONObject;
import repositories.GeneralRepositoryStorer;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 0dbfed7

Please sign in to comment.