From 65d43acb91c4330b2f185c9c951f99fe5670ccf4 Mon Sep 17 00:00:00 2001 From: kgc9007 Date: Mon, 22 Jul 2024 13:06:51 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Quiz=20=ED=80=B4=EC=A6=88=20=EB=93=B1?= =?UTF-8?q?=EB=A1=9D,=20=EC=A1=B0=ED=9A=8C=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../quiz/controller/QuizController.java | 25 +++++++++++++------ .../quiz/entity/MutipleQuizCreateRequest.java | 20 --------------- .../edufocus/edufocus/quiz/entity/Quiz.java | 9 +++++-- .../quiz/entity/QuizCreateRequest.java | 12 ++++++--- .../edufocus/quiz/entity/QuizSet.java | 4 +++ .../quiz/repository/QuizRepository.java | 2 ++ .../edufocus/quiz/service/QuizService.java | 3 ++- .../quiz/service/QuizServiceImpl.java | 25 ++++++++++++------- .../edufocus/quiz/service/QuizSetService.java | 1 + .../quiz/service/QuizSetServiceImpl.java | 10 ++++++-- 10 files changed, 67 insertions(+), 44 deletions(-) delete mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/entity/MutipleQuizCreateRequest.java diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/controller/QuizController.java b/backend/src/main/java/com/edufocus/edufocus/quiz/controller/QuizController.java index 3c18199..57e22e0 100644 --- a/backend/src/main/java/com/edufocus/edufocus/quiz/controller/QuizController.java +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/controller/QuizController.java @@ -1,17 +1,19 @@ package com.edufocus.edufocus.quiz.controller; import com.edufocus.edufocus.quiz.entity.*; +import com.edufocus.edufocus.quiz.repository.QuizRepository; import com.edufocus.edufocus.quiz.service.QuizService; import com.edufocus.edufocus.quiz.service.QuizSetService; import com.edufocus.edufocus.user.model.service.UserService; +import com.edufocus.edufocus.user.util.JWTUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; @RestController @RequestMapping("/quiz") @@ -25,10 +27,12 @@ public class QuizController { private final UserService userService; - @PostMapping - public ResponseEntity createQuizSet(@RequestBody QuizSetCreateRequest quizSetCreateRequest) { + private final JWTUtil jwtUtil; + private final QuizRepository quizRepository; - Long userId = 1L; + @PostMapping + public ResponseEntity createQuizSet(@RequestHeader("Authorization") String accessToken, @RequestBody QuizSetCreateRequest quizSetCreateRequest) { + Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); String title = quizSetCreateRequest.getTitle(); String image = quizSetCreateRequest.getImage(); @@ -43,4 +47,11 @@ public class QuizController { return new ResponseEntity<>(HttpStatus.CREATED); } + + @GetMapping("/{quizsetId}") + public ResponseEntity getQuizzes(@PathVariable Long quizsetId) { + QuizSet quizSet = quizSetService.findQuizSet(quizsetId); + + return new ResponseEntity<>(quizSet, HttpStatus.OK); + } } diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/MutipleQuizCreateRequest.java b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/MutipleQuizCreateRequest.java deleted file mode 100644 index 20dbf54..0000000 --- a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/MutipleQuizCreateRequest.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.edufocus.edufocus.quiz.entity; - -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.ToString; - -@Getter -@NoArgsConstructor -@AllArgsConstructor -public class MutipleQuizCreateRequest extends QuizCreateRequest { - - private String choice1; - - private String choice2; - - private String choice3; - - private String choice4; -} diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/Quiz.java b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/Quiz.java index 4ea9b43..a113b12 100644 --- a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/Quiz.java +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/Quiz.java @@ -1,12 +1,16 @@ package com.edufocus.edufocus.quiz.entity; +import com.fasterxml.jackson.annotation.JsonBackReference; +import com.fasterxml.jackson.annotation.JsonManagedReference; import jakarta.persistence.*; -import lombok.Getter; -import lombok.Setter; +import lombok.*; @Entity @Getter @Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor public class Quiz { @Id @@ -15,6 +19,7 @@ public class Quiz { @ManyToOne @JoinColumn(name = "quizset_id") + @JsonBackReference private QuizSet quizSet; @Column diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizCreateRequest.java b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizCreateRequest.java index d5c38da..eb58041 100644 --- a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizCreateRequest.java +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizCreateRequest.java @@ -3,7 +3,6 @@ package com.edufocus.edufocus.quiz.entity; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; -import lombok.ToString; @Getter @NoArgsConstructor @@ -14,10 +13,17 @@ public class QuizCreateRequest { private String description; - private boolean isSingle; - private String answer; + private boolean isSingle; + private String image; + private String choice1; + + private String choice2; + + private String choice3; + + private String choice4; } diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizSet.java b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizSet.java index b067277..89a2b4d 100644 --- a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizSet.java +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizSet.java @@ -1,6 +1,8 @@ package com.edufocus.edufocus.quiz.entity; import com.edufocus.edufocus.user.model.entity.User; +import com.fasterxml.jackson.annotation.JsonBackReference; +import com.fasterxml.jackson.annotation.JsonManagedReference; import jakarta.persistence.*; import lombok.Getter; import lombok.Setter; @@ -19,6 +21,7 @@ public class QuizSet { @ManyToOne @JoinColumn(name = "user_id") + @JsonBackReference private User user; @Column @@ -28,6 +31,7 @@ public class QuizSet { private String image; @OneToMany(mappedBy = "quizSet") + @JsonManagedReference private List quizzes = new ArrayList(); public void addQuiz(Quiz quiz) { diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/repository/QuizRepository.java b/backend/src/main/java/com/edufocus/edufocus/quiz/repository/QuizRepository.java index 7428689..9e7d345 100644 --- a/backend/src/main/java/com/edufocus/edufocus/quiz/repository/QuizRepository.java +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/repository/QuizRepository.java @@ -3,5 +3,7 @@ package com.edufocus.edufocus.quiz.repository; import com.edufocus.edufocus.quiz.entity.Quiz; import org.springframework.data.jpa.repository.JpaRepository; + public interface QuizRepository extends JpaRepository { + } diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizService.java b/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizService.java index 3174e89..8b0725f 100644 --- a/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizService.java +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizService.java @@ -1,9 +1,10 @@ package com.edufocus.edufocus.quiz.service; -import com.edufocus.edufocus.quiz.entity.MutipleQuizCreateRequest; import com.edufocus.edufocus.quiz.entity.QuizCreateRequest; import org.springframework.stereotype.Service; +import java.util.List; + @Service public interface QuizService { diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizServiceImpl.java index 5f34010..b4e0ac9 100644 --- a/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizServiceImpl.java +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizServiceImpl.java @@ -1,6 +1,5 @@ package com.edufocus.edufocus.quiz.service; -import com.edufocus.edufocus.quiz.entity.MutipleQuizCreateRequest; import com.edufocus.edufocus.quiz.entity.Quiz; import com.edufocus.edufocus.quiz.entity.QuizCreateRequest; import com.edufocus.edufocus.quiz.entity.QuizSet; @@ -10,6 +9,8 @@ import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.List; + @Service @Transactional @RequiredArgsConstructor @@ -23,15 +24,20 @@ public class QuizServiceImpl implements QuizService { public void createQuiz(long quizSetId, QuizCreateRequest quizCreateRequest) { QuizSet quizSet = quizSetRepository.findById(quizSetId).get(); - Quiz quiz = new Quiz(); + Quiz quiz = new Quiz().builder() + .title(quizCreateRequest.getTitle()) + .description(quizCreateRequest.getDescription()) + .answer(quizCreateRequest.getAnswer()) + .image(quizCreateRequest.getImage()) + .quizSet(quizSet) + .build(); - quiz.setTitle(quizCreateRequest.getTitle()); - quiz.setDescription(quizCreateRequest.getDescription()); - quiz.setAnswer(quizCreateRequest.getAnswer()); - quiz.setImage(quizCreateRequest.getImage()); - - - quiz.setQuizSet(quizSet); + if (!quiz.isSingle()) { + quiz.setChoice1(quizCreateRequest.getChoice1()); + quiz.setChoice2(quizCreateRequest.getChoice2()); + quiz.setChoice3(quizCreateRequest.getChoice3()); + quiz.setChoice4(quizCreateRequest.getChoice4()); + } quizRepository.save(quiz); } @@ -46,4 +52,5 @@ public class QuizServiceImpl implements QuizService { quizRepository.deleteById(quizId); return true; } + } diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizSetService.java b/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizSetService.java index 748283b..a62a979 100644 --- a/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizSetService.java +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizSetService.java @@ -13,4 +13,5 @@ public interface QuizSetService { void deleteQuizSet(long quizSetId); + QuizSet findQuizSet(long quizSetId); } diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizSetServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizSetServiceImpl.java index a167cca..aefdd03 100644 --- a/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizSetServiceImpl.java +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizSetServiceImpl.java @@ -1,7 +1,7 @@ package com.edufocus.edufocus.quiz.service; -import com.edufocus.edufocus.quiz.entity.QuizSet; -import com.edufocus.edufocus.quiz.entity.SetCreateRequest; +import com.edufocus.edufocus.quiz.entity.*; +import com.edufocus.edufocus.quiz.repository.QuizRepository; import com.edufocus.edufocus.quiz.repository.QuizSetRepository; import com.edufocus.edufocus.user.model.entity.User; import com.edufocus.edufocus.user.model.repository.UserRepository; @@ -17,6 +17,7 @@ public class QuizSetServiceImpl implements QuizSetService { private final QuizSetRepository quizSetRepository; private final UserRepository userRepository; + private final QuizRepository quizRepository; @Override @@ -42,4 +43,9 @@ public class QuizSetServiceImpl implements QuizSetService { public void deleteQuizSet(long quizSetId) { quizSetRepository.deleteById(quizSetId); } + + @Override + public QuizSet findQuizSet(long quizSetId) { + return quizSetRepository.findById(quizSetId).get(); + } }