From a45ca6298541fd1853246e4b57e23f32e1647da7 Mon Sep 17 00:00:00 2001 From: Dario Date: Sat, 24 Feb 2024 16:54:04 +0100 Subject: [PATCH 01/12] test: questions --- .../questions/QuestionControllerTest.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 api/src/test/java/lab/en2b/quizapi/questions/QuestionControllerTest.java diff --git a/api/src/test/java/lab/en2b/quizapi/questions/QuestionControllerTest.java b/api/src/test/java/lab/en2b/quizapi/questions/QuestionControllerTest.java new file mode 100644 index 00000000..c46955e9 --- /dev/null +++ b/api/src/test/java/lab/en2b/quizapi/questions/QuestionControllerTest.java @@ -0,0 +1,33 @@ +package lab.en2b.quizapi.questions; + +import lab.en2b.quizapi.auth.config.SecurityConfig; +import lab.en2b.quizapi.questions.question.QuestionController; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.context.annotation.Import; +import org.springframework.http.ResponseEntity; +import org.springframework.test.web.servlet.MockMvc; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@WebMvcTest(QuestionController.class) +@AutoConfigureMockMvc +@Import(SecurityConfig.class) +public class QuestionControllerTest { + @Autowired + MockMvc mockMvc; + @Test + void getQuestionShouldReturn200() throws Exception { + mockMvc.perform(get("/questions") + .contentType("application/json") + .with(csrf())) + .andExpect(status().isOk()); + } +} From fc1653eacf3c9bc77de6c4c1772438b8a492a93f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gonzalo=20Su=C3=A1rez=20Losada?= Date: Sat, 24 Feb 2024 17:27:59 +0100 Subject: [PATCH 02/12] feat: /questions --- .../quizapi/questions/question/Answer.java | 4 +++ .../quizapi/questions/question/Question.java | 27 +++++++++++++++++++ .../questions/question/QuestionCategory.java | 4 +++ .../question/QuestionController.java | 4 +++ .../questions/question/QuestionType.java | 4 +++ 5 files changed, 43 insertions(+) create mode 100644 api/src/main/java/lab/en2b/quizapi/questions/question/Answer.java create mode 100644 api/src/main/java/lab/en2b/quizapi/questions/question/Question.java create mode 100644 api/src/main/java/lab/en2b/quizapi/questions/question/QuestionCategory.java create mode 100644 api/src/main/java/lab/en2b/quizapi/questions/question/QuestionType.java diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/Answer.java b/api/src/main/java/lab/en2b/quizapi/questions/question/Answer.java new file mode 100644 index 00000000..95f4f29c --- /dev/null +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/Answer.java @@ -0,0 +1,4 @@ +package lab.en2b.quizapi.questions.question; + +public class Answer { +} 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 new file mode 100644 index 00000000..4a2f601e --- /dev/null +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/Question.java @@ -0,0 +1,27 @@ +package lab.en2b.quizapi.questions.question; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import lombok.AccessLevel; +import lombok.Setter; + +import java.util.List; + +@Entity +public class Question { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Setter(AccessLevel.NONE) + private Long id; + private String content; + private List answers; + private Answer correctAnswer; + private QuestionCategory category; + private String language; + private QuestionType type; + + +} diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionCategory.java b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionCategory.java new file mode 100644 index 00000000..3941acd7 --- /dev/null +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionCategory.java @@ -0,0 +1,4 @@ +package lab.en2b.quizapi.questions.question; + +public class QuestionCategory { +} diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java index 1c1e7127..6ac410c2 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java @@ -1,5 +1,6 @@ package lab.en2b.quizapi.questions.question; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -11,4 +12,7 @@ public class QuestionController { private String getDummyQuestion(){ return "Who the hell is Steve Jobs?"; } + + @GetMapping("/questions") + private ResponseEntity getQuestions() { return ResponseEntity.ok("Response ok "); } } diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionType.java b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionType.java new file mode 100644 index 00000000..3859fee0 --- /dev/null +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionType.java @@ -0,0 +1,4 @@ +package lab.en2b.quizapi.questions.question; + +public class QuestionType { +} From 7753308cc80d7e6820facdc555fa32f932d4c328 Mon Sep 17 00:00:00 2001 From: Diego Villanueva Date: Sat, 24 Feb 2024 17:49:29 +0100 Subject: [PATCH 03/12] feat: Answer modeling --- .../en2b/quizapi/questions/question/Answer.java | 16 ++++++++++++++++ .../questions/question/AnswerCategory.java | 5 +++++ .../quizapi/questions/question/Question.java | 17 +++++++++++++---- .../questions/question/QuestionCategory.java | 3 ++- .../questions/question/QuestionController.java | 8 +++++++- .../questions/question/QuestionService.java | 13 +++++++++++++ .../questions/question/QuestionType.java | 13 +++++++++++++ 7 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 api/src/main/java/lab/en2b/quizapi/questions/question/AnswerCategory.java create mode 100644 api/src/main/java/lab/en2b/quizapi/questions/question/QuestionService.java diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/Answer.java b/api/src/main/java/lab/en2b/quizapi/questions/question/Answer.java index 95f4f29c..029aca73 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/Answer.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/Answer.java @@ -1,4 +1,20 @@ package lab.en2b.quizapi.questions.question; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import lombok.AccessLevel; +import lombok.Setter; + +@Entity public class Answer { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Setter(AccessLevel.NONE) + private Long id; + + private String content; + private String answerCategory; + private String answerType; } diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/AnswerCategory.java b/api/src/main/java/lab/en2b/quizapi/questions/question/AnswerCategory.java new file mode 100644 index 00000000..849bb599 --- /dev/null +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/AnswerCategory.java @@ -0,0 +1,5 @@ +package lab.en2b.quizapi.questions.question; + +public enum AnswerCategory { + CITY, COUNTRY, PERSON, EVENT, DATE, OTHER +} 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 4a2f601e..f9b4b501 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 @@ -1,9 +1,7 @@ package lab.en2b.quizapi.questions.question; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; +import jakarta.persistence.*; +import jakarta.validation.constraints.NotNull; import lombok.AccessLevel; import lombok.Setter; @@ -17,10 +15,21 @@ public class Question { @Setter(AccessLevel.NONE) private Long id; private String content; + @NotNull + @ManyToMany(fetch = FetchType.EAGER) + @JoinTable(name="questions_answers", + joinColumns= + @JoinColumn(name="question_id", referencedColumnName="id"), + inverseJoinColumns= + @JoinColumn(name="answer_id", referencedColumnName="id") + ) private List answers; + @ManyToOne private Answer correctAnswer; + @ManyToOne private QuestionCategory category; private String language; + @ManyToOne private QuestionType type; diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionCategory.java b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionCategory.java index 3941acd7..9d5eb02f 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionCategory.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionCategory.java @@ -1,4 +1,5 @@ package lab.en2b.quizapi.questions.question; -public class QuestionCategory { +public enum QuestionCategory { + HISTORY, GEOGRAPHY, SCIENCE, MATH, LITERATURE, ART, SPORTS, MUSIC, MOVIES, TV, POLITICS, OTHER } diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java index 6ac410c2..f42bc373 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java @@ -1,18 +1,24 @@ package lab.en2b.quizapi.questions.question; +import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.List; + @RestController @RequestMapping("/questions") +@RequiredArgsConstructor public class QuestionController { + private final QuestionService questionService; + @GetMapping("/dummy") private String getDummyQuestion(){ return "Who the hell is Steve Jobs?"; } @GetMapping("/questions") - private ResponseEntity getQuestions() { return ResponseEntity.ok("Response ok "); } + private ResponseEntity> getQuestions() { return ResponseEntity.ok(questionService.getQuestions()); } } 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 new file mode 100644 index 00000000..bfcffefe --- /dev/null +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionService.java @@ -0,0 +1,13 @@ +package lab.en2b.quizapi.questions.question; + +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +public class QuestionService { + public List getQuestions() { + return new ArrayList<>(); + } +} diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionType.java b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionType.java index 3859fee0..714c8014 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionType.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionType.java @@ -1,4 +1,17 @@ package lab.en2b.quizapi.questions.question; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import lombok.AccessLevel; +import lombok.Setter; + +@Entity public class QuestionType { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Setter(AccessLevel.NONE) + private Long id; + } From c0b9dfb1c0cd9df2ae131bdb9ddd904ce20bbd4b Mon Sep 17 00:00:00 2001 From: sergioqfeg1 Date: Sat, 24 Feb 2024 18:03:09 +0100 Subject: [PATCH 04/12] feat: repository --- .../en2b/quizapi/questions/answer/Answer.java | 21 +++++++++++++++++++ .../{question => answer}/AnswerCategory.java | 2 +- .../quizapi/questions/question/Answer.java | 20 ------------------ .../quizapi/questions/question/Question.java | 8 ++++--- .../question/QuestionRepository.java | 6 ++++++ .../questions/question/QuestionService.java | 6 +++++- .../questions/question/QuestionType.java | 9 +++----- 7 files changed, 41 insertions(+), 31 deletions(-) create mode 100644 api/src/main/java/lab/en2b/quizapi/questions/answer/Answer.java rename api/src/main/java/lab/en2b/quizapi/questions/{question => answer}/AnswerCategory.java (63%) delete mode 100644 api/src/main/java/lab/en2b/quizapi/questions/question/Answer.java create mode 100644 api/src/main/java/lab/en2b/quizapi/questions/question/QuestionRepository.java diff --git a/api/src/main/java/lab/en2b/quizapi/questions/answer/Answer.java b/api/src/main/java/lab/en2b/quizapi/questions/answer/Answer.java new file mode 100644 index 00000000..5e1a3f3e --- /dev/null +++ b/api/src/main/java/lab/en2b/quizapi/questions/answer/Answer.java @@ -0,0 +1,21 @@ +package lab.en2b.quizapi.questions.answer; + +import jakarta.persistence.*; +import lab.en2b.quizapi.questions.question.Question; +import lombok.AccessLevel; +import lombok.Setter; + +import java.util.List; + +@Entity +public class Answer { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Setter(AccessLevel.NONE) + private Long id; + private String text; + private AnswerCategory category; + @OneToMany(mappedBy = "answer") + private List questions; + +} diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/AnswerCategory.java b/api/src/main/java/lab/en2b/quizapi/questions/answer/AnswerCategory.java similarity index 63% rename from api/src/main/java/lab/en2b/quizapi/questions/question/AnswerCategory.java rename to api/src/main/java/lab/en2b/quizapi/questions/answer/AnswerCategory.java index 849bb599..13412a21 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/AnswerCategory.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/answer/AnswerCategory.java @@ -1,4 +1,4 @@ -package lab.en2b.quizapi.questions.question; +package lab.en2b.quizapi.questions.answer; public enum AnswerCategory { CITY, COUNTRY, PERSON, EVENT, DATE, OTHER diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/Answer.java b/api/src/main/java/lab/en2b/quizapi/questions/question/Answer.java deleted file mode 100644 index 029aca73..00000000 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/Answer.java +++ /dev/null @@ -1,20 +0,0 @@ -package lab.en2b.quizapi.questions.question; - -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import lombok.AccessLevel; -import lombok.Setter; - -@Entity -public class Answer { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Setter(AccessLevel.NONE) - private Long id; - - private String content; - private String answerCategory; - private String answerType; -} 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 f9b4b501..67094cbd 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 @@ -2,6 +2,8 @@ import jakarta.persistence.*; import jakarta.validation.constraints.NotNull; +import lab.en2b.quizapi.questions.answer.Answer; +import lab.en2b.quizapi.questions.answer.AnswerCategory; import lombok.AccessLevel; import lombok.Setter; @@ -25,11 +27,11 @@ public class Question { ) private List answers; @ManyToOne + @JoinColumn(name = "answer_id") private Answer correctAnswer; - @ManyToOne - private QuestionCategory category; + private QuestionCategory questionCategory; + private AnswerCategory answerCategory; private String language; - @ManyToOne private QuestionType type; diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionRepository.java b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionRepository.java new file mode 100644 index 00000000..396db575 --- /dev/null +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionRepository.java @@ -0,0 +1,6 @@ +package lab.en2b.quizapi.questions.question; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface QuestionRepository extends JpaRepository { +} 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 bfcffefe..913699a0 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 @@ -1,13 +1,17 @@ package lab.en2b.quizapi.questions.question; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; @Service +@RequiredArgsConstructor public class QuestionService { + + private final QuestionRepository questionRepository; public List getQuestions() { - return new ArrayList<>(); + return questionRepository.findAll(); } } diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionType.java b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionType.java index 714c8014..6dc09fdc 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionType.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionType.java @@ -7,11 +7,8 @@ import lombok.AccessLevel; import lombok.Setter; -@Entity -public class QuestionType { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Setter(AccessLevel.NONE) - private Long id; + +public enum QuestionType { + TEXT, VIDEO, IMAGE, AUDIO } From c5a51979ab231773593d2c1cbb90761fbcfb63a0 Mon Sep 17 00:00:00 2001 From: Dario Date: Sat, 24 Feb 2024 18:19:34 +0100 Subject: [PATCH 05/12] feat: models done --- .../lab/en2b/quizapi/questions/answer/Answer.java | 13 ++++++++++--- .../en2b/quizapi/questions/question/Question.java | 10 +++++++--- .../questions/question/QuestionController.java | 2 +- .../quizapi/questions/QuestionControllerTest.java | 3 +++ 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/api/src/main/java/lab/en2b/quizapi/questions/answer/Answer.java b/api/src/main/java/lab/en2b/quizapi/questions/answer/Answer.java index 5e1a3f3e..024f77d8 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/answer/Answer.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/answer/Answer.java @@ -2,12 +2,16 @@ import jakarta.persistence.*; import lab.en2b.quizapi.questions.question.Question; -import lombok.AccessLevel; -import lombok.Setter; +import lombok.*; import java.util.List; @Entity +@Table(name = "answers") +@NoArgsConstructor +@AllArgsConstructor +@Getter +@Setter public class Answer { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -15,7 +19,10 @@ public class Answer { private Long id; private String text; private AnswerCategory category; - @OneToMany(mappedBy = "answer") + @OneToMany(mappedBy = "correctAnswer", fetch = FetchType.EAGER) private List questions; + @ManyToMany(mappedBy = "answers", fetch = FetchType.EAGER) + private List questionsWithThisAnswer; + } 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 67094cbd..3aa8c83b 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 @@ -4,12 +4,16 @@ import jakarta.validation.constraints.NotNull; import lab.en2b.quizapi.questions.answer.Answer; import lab.en2b.quizapi.questions.answer.AnswerCategory; -import lombok.AccessLevel; -import lombok.Setter; +import lombok.*; import java.util.List; @Entity +@Table(name = "questions") +@NoArgsConstructor +@AllArgsConstructor +@Getter +@Setter public class Question { @Id @@ -27,7 +31,7 @@ public class Question { ) private List answers; @ManyToOne - @JoinColumn(name = "answer_id") + @JoinColumn(name = "correct_answer_id") private Answer correctAnswer; private QuestionCategory questionCategory; private AnswerCategory answerCategory; diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java index f42bc373..b67224b3 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java @@ -19,6 +19,6 @@ private String getDummyQuestion(){ return "Who the hell is Steve Jobs?"; } - @GetMapping("/questions") + @GetMapping private ResponseEntity> getQuestions() { return ResponseEntity.ok(questionService.getQuestions()); } } diff --git a/api/src/test/java/lab/en2b/quizapi/questions/QuestionControllerTest.java b/api/src/test/java/lab/en2b/quizapi/questions/QuestionControllerTest.java index c46955e9..d61895d8 100644 --- a/api/src/test/java/lab/en2b/quizapi/questions/QuestionControllerTest.java +++ b/api/src/test/java/lab/en2b/quizapi/questions/QuestionControllerTest.java @@ -1,6 +1,7 @@ package lab.en2b.quizapi.questions; import lab.en2b.quizapi.auth.config.SecurityConfig; +import lab.en2b.quizapi.auth.jwt.JwtUtils; import lab.en2b.quizapi.questions.question.QuestionController; import org.junit.jupiter.api.Test; import org.mockito.Mock; @@ -23,6 +24,8 @@ public class QuestionControllerTest { @Autowired MockMvc mockMvc; + @Mock + JwtUtils jwtUtils; @Test void getQuestionShouldReturn200() throws Exception { mockMvc.perform(get("/questions") From 3baed9a56e2b72812c43f11977d11136bdd44855 Mon Sep 17 00:00:00 2001 From: Dario Date: Sat, 24 Feb 2024 18:37:08 +0100 Subject: [PATCH 06/12] feat: answer questions --- .../quizapi/questions/answer/dtos/AnswerDto.java | 14 ++++++++++++++ .../questions/question/QuestionController.java | 14 ++++++++++---- .../questions/question/QuestionService.java | 12 +++++++++++- 3 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 api/src/main/java/lab/en2b/quizapi/questions/answer/dtos/AnswerDto.java diff --git a/api/src/main/java/lab/en2b/quizapi/questions/answer/dtos/AnswerDto.java b/api/src/main/java/lab/en2b/quizapi/questions/answer/dtos/AnswerDto.java new file mode 100644 index 00000000..b932f0a9 --- /dev/null +++ b/api/src/main/java/lab/en2b/quizapi/questions/answer/dtos/AnswerDto.java @@ -0,0 +1,14 @@ +package lab.en2b.quizapi.questions.answer.dtos; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class AnswerDto { + @JsonProperty("answer_id") + private Long answerId; +} diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java index b67224b3..697faf9a 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java @@ -1,10 +1,9 @@ package lab.en2b.quizapi.questions.question; +import lab.en2b.quizapi.questions.answer.dtos.AnswerDto; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; @@ -20,5 +19,12 @@ private String getDummyQuestion(){ } @GetMapping - private ResponseEntity> getQuestions() { return ResponseEntity.ok(questionService.getQuestions()); } + private ResponseEntity> getQuestions() { + return ResponseEntity.ok(questionService.getQuestions()); + } + + @PostMapping("/{questionId}/answer") + private ResponseEntity answerQuestion(@RequestParam Long questionId, @RequestBody AnswerDto answerDto){ + return ResponseEntity.ok(questionService.answerQuestion(questionId,answerDto)); + } } 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 913699a0..3b890046 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 @@ -1,9 +1,9 @@ package lab.en2b.quizapi.questions.question; +import lab.en2b.quizapi.questions.answer.dtos.AnswerDto; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import java.util.ArrayList; import java.util.List; @Service @@ -14,4 +14,14 @@ public class QuestionService { public List getQuestions() { return questionRepository.findAll(); } + + public String answerQuestion(Long id, AnswerDto answerDto) { + Question question = questionRepository.findById(id).orElseThrow(); + if(question.getAnswers().stream().filter(i -> i.getId().equals(answerDto.getAnswerId())).findFirst().isPresent()){ + return "Correct!"; + } + else{ + return "Wrong!"; + } + } } From 0801a90319f0f4e6fe80e0b00d408dd0282398bb Mon Sep 17 00:00:00 2001 From: sergioqfeg1 Date: Wed, 28 Feb 2024 17:12:21 +0100 Subject: [PATCH 07/12] feat: getQuestion + responseDto --- .../answer/dtos/AnswerResponseDto.java | 11 ++++++++++ .../question/QuestionController.java | 5 +++++ .../questions/question/QuestionService.java | 4 ++++ .../question/dtos/QuestionResponseDto.java | 21 +++++++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 api/src/main/java/lab/en2b/quizapi/questions/answer/dtos/AnswerResponseDto.java create mode 100644 api/src/main/java/lab/en2b/quizapi/questions/question/dtos/QuestionResponseDto.java diff --git a/api/src/main/java/lab/en2b/quizapi/questions/answer/dtos/AnswerResponseDto.java b/api/src/main/java/lab/en2b/quizapi/questions/answer/dtos/AnswerResponseDto.java new file mode 100644 index 00000000..c13eb95d --- /dev/null +++ b/api/src/main/java/lab/en2b/quizapi/questions/answer/dtos/AnswerResponseDto.java @@ -0,0 +1,11 @@ +package lab.en2b.quizapi.questions.answer.dtos; + +import lab.en2b.quizapi.questions.answer.AnswerCategory; + +import java.util.List; + +public class AnswerResponseDto { + private Long id; + private String text; + private AnswerCategory category; +} diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java index 697faf9a..9df4881e 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java @@ -27,4 +27,9 @@ private ResponseEntity> getQuestions() { private ResponseEntity answerQuestion(@RequestParam Long questionId, @RequestBody AnswerDto answerDto){ return ResponseEntity.ok(questionService.answerQuestion(questionId,answerDto)); } + + @GetMapping("/new") + private ResponseEntity generateQuestion(){ + return ResponseEntity.ok(questionService.getQuestion()); + } } 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 3b890046..06e0384b 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 @@ -24,4 +24,8 @@ public String answerQuestion(Long id, AnswerDto answerDto) { return "Wrong!"; } } + + public Question getQuestion() { + return null; + } } 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 new file mode 100644 index 00000000..ecb495ee --- /dev/null +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/dtos/QuestionResponseDto.java @@ -0,0 +1,21 @@ +package lab.en2b.quizapi.questions.question.dtos; + +import jakarta.persistence.*; +import jakarta.validation.constraints.NotNull; +import lab.en2b.quizapi.questions.answer.Answer; +import lab.en2b.quizapi.questions.answer.AnswerCategory; +import lab.en2b.quizapi.questions.answer.dtos.AnswerResponseDto; +import lab.en2b.quizapi.questions.question.QuestionCategory; +import lab.en2b.quizapi.questions.question.QuestionType; + +import java.util.List; + +public class QuestionResponseDto { + private Long id; + private String content; + private List answers; + private QuestionCategory questionCategory; + private AnswerCategory answerCategory; + private String language; + private QuestionType type; +} From 57f576b83245021e3bb075778c83f42b61614458 Mon Sep 17 00:00:00 2001 From: sergioqfeg1 Date: Wed, 28 Feb 2024 17:32:54 +0100 Subject: [PATCH 08/12] feat: mappers --- .../mappers/AnswerResponseDtoMapper.java | 15 +++++++++++ .../question/QuestionController.java | 3 ++- .../questions/question/QuestionService.java | 7 ++++-- .../question/dtos/QuestionResponseDto.java | 9 ++++--- .../mappers/QuestionResponseDtoMapper.java | 25 +++++++++++++++++++ 5 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 api/src/main/java/lab/en2b/quizapi/questions/answer/mappers/AnswerResponseDtoMapper.java create mode 100644 api/src/main/java/lab/en2b/quizapi/questions/question/mappers/QuestionResponseDtoMapper.java diff --git a/api/src/main/java/lab/en2b/quizapi/questions/answer/mappers/AnswerResponseDtoMapper.java b/api/src/main/java/lab/en2b/quizapi/questions/answer/mappers/AnswerResponseDtoMapper.java new file mode 100644 index 00000000..0a84e756 --- /dev/null +++ b/api/src/main/java/lab/en2b/quizapi/questions/answer/mappers/AnswerResponseDtoMapper.java @@ -0,0 +1,15 @@ +package lab.en2b.quizapi.questions.answer.mappers; + +import lab.en2b.quizapi.questions.answer.Answer; +import lab.en2b.quizapi.questions.answer.dtos.AnswerResponseDto; +import org.springframework.stereotype.Service; + +import java.util.function.Function; + +@Service +public class AnswerResponseDtoMapper implements Function { + @Override + public AnswerResponseDto apply(Answer answer) { + return null; + } +} diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java index 9df4881e..5d825dbb 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java @@ -1,6 +1,7 @@ package lab.en2b.quizapi.questions.question; import lab.en2b.quizapi.questions.answer.dtos.AnswerDto; +import lab.en2b.quizapi.questions.question.dtos.QuestionResponseDto; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -19,7 +20,7 @@ private String getDummyQuestion(){ } @GetMapping - private ResponseEntity> getQuestions() { + private ResponseEntity> getQuestions() { return ResponseEntity.ok(questionService.getQuestions()); } 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 06e0384b..78d9252c 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 @@ -1,6 +1,8 @@ package lab.en2b.quizapi.questions.question; import lab.en2b.quizapi.questions.answer.dtos.AnswerDto; +import lab.en2b.quizapi.questions.question.dtos.QuestionResponseDto; +import lab.en2b.quizapi.questions.question.mappers.QuestionResponseDtoMapper; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -11,8 +13,9 @@ public class QuestionService { private final QuestionRepository questionRepository; - public List getQuestions() { - return questionRepository.findAll(); + private final QuestionResponseDtoMapper questionResponseDtoMapper; + public List getQuestions() { + return questionRepository.findAll().stream().map(questionResponseDtoMapper).toList(); } public String answerQuestion(Long id, AnswerDto answerDto) { 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 ecb495ee..b9cbc3e9 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 @@ -1,15 +1,15 @@ package lab.en2b.quizapi.questions.question.dtos; -import jakarta.persistence.*; -import jakarta.validation.constraints.NotNull; -import lab.en2b.quizapi.questions.answer.Answer; import lab.en2b.quizapi.questions.answer.AnswerCategory; import lab.en2b.quizapi.questions.answer.dtos.AnswerResponseDto; +import lab.en2b.quizapi.questions.question.Question; import lab.en2b.quizapi.questions.question.QuestionCategory; import lab.en2b.quizapi.questions.question.QuestionType; +import lombok.Builder; import java.util.List; +@Builder public class QuestionResponseDto { private Long id; private String content; @@ -18,4 +18,7 @@ public class QuestionResponseDto { private AnswerCategory answerCategory; private String language; private QuestionType type; + + + } 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 new file mode 100644 index 00000000..2752f4e3 --- /dev/null +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/mappers/QuestionResponseDtoMapper.java @@ -0,0 +1,25 @@ +package lab.en2b.quizapi.questions.question.mappers; + +import lab.en2b.quizapi.questions.answer.mappers.AnswerResponseDtoMapper; +import lab.en2b.quizapi.questions.question.Question; +import lab.en2b.quizapi.questions.question.dtos.QuestionResponseDto; +import org.springframework.stereotype.Service; + +import java.util.function.Function; + +@Service +public class QuestionResponseDtoMapper implements Function { + + @Override + public QuestionResponseDto apply(Question question) { + return QuestionResponseDto.builder() + .id(question.getId()) + .content(question.getContent()) + .type(question.getType()) + .answerCategory(question.getAnswerCategory()) + .answers(question.getAnswers().stream().map(new AnswerResponseDtoMapper()).toList()) + .language(question.getLanguage()) + .questionCategory(question.getQuestionCategory()) + .build(); + } +} From 9f8c7b0e63de8033183d8a06e0837e73d6324669 Mon Sep 17 00:00:00 2001 From: Dario Date: Wed, 28 Feb 2024 17:51:39 +0100 Subject: [PATCH 09/12] feat: get question by id --- .../question/QuestionController.java | 11 ++++++++-- .../question/QuestionRepository.java | 3 +++ .../questions/question/QuestionService.java | 22 +++++++++++++------ .../question/dtos/AnswerCheckResponseDto.java | 12 ++++++++++ 4 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 api/src/main/java/lab/en2b/quizapi/questions/question/dtos/AnswerCheckResponseDto.java diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java index 5d825dbb..77748a5a 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java @@ -1,6 +1,7 @@ package lab.en2b.quizapi.questions.question; import lab.en2b.quizapi.questions.answer.dtos.AnswerDto; +import lab.en2b.quizapi.questions.question.dtos.AnswerCheckResponseDto; import lab.en2b.quizapi.questions.question.dtos.QuestionResponseDto; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; @@ -19,18 +20,24 @@ private String getDummyQuestion(){ return "Who the hell is Steve Jobs?"; } + // TODO: REMOVE WHEN NOT USED FOR TESTING @GetMapping private ResponseEntity> getQuestions() { return ResponseEntity.ok(questionService.getQuestions()); } @PostMapping("/{questionId}/answer") - private ResponseEntity answerQuestion(@RequestParam Long questionId, @RequestBody AnswerDto answerDto){ + private ResponseEntity answerQuestion(@RequestParam Long questionId, @RequestBody AnswerDto answerDto){ return ResponseEntity.ok(questionService.answerQuestion(questionId,answerDto)); } @GetMapping("/new") - private ResponseEntity generateQuestion(){ + private ResponseEntity generateQuestion(){ return ResponseEntity.ok(questionService.getQuestion()); } + + @GetMapping("/{id}") + private ResponseEntity getQuestionById(@PathVariable Long id){ + return ResponseEntity.ok(questionService.getQuestionById(id)); + } } diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionRepository.java b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionRepository.java index 396db575..d957acb0 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionRepository.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionRepository.java @@ -1,6 +1,9 @@ package lab.en2b.quizapi.questions.question; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; public interface QuestionRepository extends JpaRepository { + @Query(value = "SELECT * FROM questions ORDER BY RAND() LIMIT 1", nativeQuery = true) + Question findRandomQuestion(); } 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 78d9252c..fa179878 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 @@ -1,6 +1,7 @@ package lab.en2b.quizapi.questions.question; import lab.en2b.quizapi.questions.answer.dtos.AnswerDto; +import lab.en2b.quizapi.questions.question.dtos.AnswerCheckResponseDto; import lab.en2b.quizapi.questions.question.dtos.QuestionResponseDto; import lab.en2b.quizapi.questions.question.mappers.QuestionResponseDtoMapper; import lombok.RequiredArgsConstructor; @@ -18,17 +19,24 @@ public List getQuestions() { return questionRepository.findAll().stream().map(questionResponseDtoMapper).toList(); } - public String answerQuestion(Long id, AnswerDto answerDto) { + public AnswerCheckResponseDto answerQuestion(Long id, AnswerDto answerDto) { Question question = questionRepository.findById(id).orElseThrow(); - if(question.getAnswers().stream().filter(i -> i.getId().equals(answerDto.getAnswerId())).findFirst().isPresent()){ - return "Correct!"; + if(question.getCorrectAnswer().getId().equals(answerDto.getAnswerId())){ + return new AnswerCheckResponseDto(true); } - else{ - return "Wrong!"; + else if(question.getAnswers().stream().noneMatch(i -> i.getId().equals(answerDto.getAnswerId()))){ + throw new IllegalArgumentException("The answer you provided is not one of the options"); } + else { + return new AnswerCheckResponseDto(false); + } + } + + public QuestionResponseDto getQuestion() { + return questionResponseDtoMapper.apply(questionRepository.findRandomQuestion()); } - public Question getQuestion() { - return null; + public QuestionResponseDto getQuestionById(Long id) { + return questionResponseDtoMapper.apply(questionRepository.findById(id).orElseThrow()); } } diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/dtos/AnswerCheckResponseDto.java b/api/src/main/java/lab/en2b/quizapi/questions/question/dtos/AnswerCheckResponseDto.java new file mode 100644 index 00000000..ea1ed4e9 --- /dev/null +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/dtos/AnswerCheckResponseDto.java @@ -0,0 +1,12 @@ +package lab.en2b.quizapi.questions.question.dtos; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@AllArgsConstructor +@Getter +public class AnswerCheckResponseDto { + private boolean wasCorrect; +} From 531427088491e71e7b41164cf8e27c014631f55e Mon Sep 17 00:00:00 2001 From: Dario Date: Wed, 28 Feb 2024 17:56:03 +0100 Subject: [PATCH 10/12] chore: cleaned up code --- .../quizapi/questions/answer/dtos/AnswerResponseDto.java | 2 -- .../quizapi/questions/question/QuestionController.java | 5 ----- .../lab/en2b/quizapi/questions/question/QuestionType.java | 8 -------- .../questions/question/dtos/QuestionResponseDto.java | 1 - .../java/lab/en2b/quizapi/QuizApiApplicationTests.java | 1 - .../en2b/quizapi/questions/QuestionControllerTest.java | 4 ---- 6 files changed, 21 deletions(-) diff --git a/api/src/main/java/lab/en2b/quizapi/questions/answer/dtos/AnswerResponseDto.java b/api/src/main/java/lab/en2b/quizapi/questions/answer/dtos/AnswerResponseDto.java index c13eb95d..2b109a32 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/answer/dtos/AnswerResponseDto.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/answer/dtos/AnswerResponseDto.java @@ -2,8 +2,6 @@ import lab.en2b.quizapi.questions.answer.AnswerCategory; -import java.util.List; - public class AnswerResponseDto { private Long id; private String text; diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java index 77748a5a..8a264956 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java @@ -15,11 +15,6 @@ public class QuestionController { private final QuestionService questionService; - @GetMapping("/dummy") - private String getDummyQuestion(){ - return "Who the hell is Steve Jobs?"; - } - // TODO: REMOVE WHEN NOT USED FOR TESTING @GetMapping private ResponseEntity> getQuestions() { diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionType.java b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionType.java index 6dc09fdc..06b28af0 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionType.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionType.java @@ -1,13 +1,5 @@ package lab.en2b.quizapi.questions.question; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import lombok.AccessLevel; -import lombok.Setter; - - public enum QuestionType { TEXT, VIDEO, IMAGE, AUDIO 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 b9cbc3e9..3cad27cb 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 @@ -2,7 +2,6 @@ import lab.en2b.quizapi.questions.answer.AnswerCategory; import lab.en2b.quizapi.questions.answer.dtos.AnswerResponseDto; -import lab.en2b.quizapi.questions.question.Question; import lab.en2b.quizapi.questions.question.QuestionCategory; import lab.en2b.quizapi.questions.question.QuestionType; import lombok.Builder; diff --git a/api/src/test/java/lab/en2b/quizapi/QuizApiApplicationTests.java b/api/src/test/java/lab/en2b/quizapi/QuizApiApplicationTests.java index 42b6c339..0d641438 100644 --- a/api/src/test/java/lab/en2b/quizapi/QuizApiApplicationTests.java +++ b/api/src/test/java/lab/en2b/quizapi/QuizApiApplicationTests.java @@ -1,6 +1,5 @@ package lab.en2b.quizapi; -import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest diff --git a/api/src/test/java/lab/en2b/quizapi/questions/QuestionControllerTest.java b/api/src/test/java/lab/en2b/quizapi/questions/QuestionControllerTest.java index d61895d8..e966df84 100644 --- a/api/src/test/java/lab/en2b/quizapi/questions/QuestionControllerTest.java +++ b/api/src/test/java/lab/en2b/quizapi/questions/QuestionControllerTest.java @@ -9,11 +9,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.context.annotation.Import; -import org.springframework.http.ResponseEntity; import org.springframework.test.web.servlet.MockMvc; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; From 4a8c1e8b80031d536c099b23e355aea2285294ca Mon Sep 17 00:00:00 2001 From: Dario Date: Wed, 28 Feb 2024 17:56:19 +0100 Subject: [PATCH 11/12] fix: path variable --- .../lab/en2b/quizapi/questions/question/QuestionController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java index 8a264956..d9434201 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionController.java @@ -22,7 +22,7 @@ private ResponseEntity> getQuestions() { } @PostMapping("/{questionId}/answer") - private ResponseEntity answerQuestion(@RequestParam Long questionId, @RequestBody AnswerDto answerDto){ + private ResponseEntity answerQuestion(@PathVariable Long questionId, @RequestBody AnswerDto answerDto){ return ResponseEntity.ok(questionService.answerQuestion(questionId,answerDto)); } From 1319697d3fc3cdac4eedecbf154e5a29a25d43e2 Mon Sep 17 00:00:00 2001 From: Dario Date: Wed, 28 Feb 2024 18:01:12 +0100 Subject: [PATCH 12/12] fix: annotations --- .../quizapi/questions/answer/dtos/AnswerResponseDto.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/api/src/main/java/lab/en2b/quizapi/questions/answer/dtos/AnswerResponseDto.java b/api/src/main/java/lab/en2b/quizapi/questions/answer/dtos/AnswerResponseDto.java index 2b109a32..6915567a 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/answer/dtos/AnswerResponseDto.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/answer/dtos/AnswerResponseDto.java @@ -1,7 +1,13 @@ package lab.en2b.quizapi.questions.answer.dtos; import lab.en2b.quizapi.questions.answer.AnswerCategory; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +@AllArgsConstructor +@NoArgsConstructor +@Getter public class AnswerResponseDto { private Long id; private String text;