From 8dd5676a265c5c7aba2ec24c446181defed77cbd Mon Sep 17 00:00:00 2001 From: kgc9007 Date: Mon, 29 Jul 2024 17:54:27 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20Quiz=20=EB=93=B1=EB=A1=9D,=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EC=9A=94=EC=B2=AD=20=ED=98=95=EC=8B=9D=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../quiz/controller/QuizController.java | 24 ++---------- .../edufocus/edufocus/quiz/entity/Choice.java | 29 ++++++++++++++ ...eRequest.java => ChoiceCreateRequest.java} | 7 ++-- .../edufocus/edufocus/quiz/entity/Quiz.java | 35 +++++++---------- .../quiz/entity/QuizCreateRequest.java | 16 ++------ .../edufocus/quiz/entity/QuizResponse.java | 21 ++++++++++ .../quiz/entity/QuizSetCreateRequest.java | 4 -- .../edufocus/quiz/entity/QuizSetResponse.java | 20 ++++++++++ .../edufocus/quiz/entity/QuizType.java | 5 --- .../quiz/repository/ChoiceRepository.java | 8 ++++ .../quiz/repository/QuizRepository.java | 1 - .../edufocus/quiz/service/ChoiceService.java | 7 ++++ .../quiz/service/ChoiceServiceImpl.java | 25 ++++++++++++ .../edufocus/quiz/service/QuizService.java | 4 +- .../quiz/service/QuizServiceImpl.java | 39 +++++++++++-------- .../edufocus/quiz/service/QuizSetService.java | 7 +++- .../quiz/service/QuizSetServiceImpl.java | 36 ++++++++++++++--- .../controller/RegistrationController.java | 4 +- 18 files changed, 196 insertions(+), 96 deletions(-) create mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/entity/Choice.java rename backend/src/main/java/com/edufocus/edufocus/quiz/entity/{SetCreateRequest.java => ChoiceCreateRequest.java} (68%) create mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizResponse.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizSetResponse.java delete mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizType.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/repository/ChoiceRepository.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/service/ChoiceService.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/service/ChoiceServiceImpl.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 fc3abcd..ac58dd2 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 @@ -33,28 +33,10 @@ public class QuizController { , @RequestPart(value = "image", required = false) MultipartFile image) throws IOException { Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); - String title = quizSetCreateRequest.getTitle(); - SetCreateRequest setCreateRequest = new SetCreateRequest(userId, title); - - QuizSet quizSet = quizSetService.createQuizSet(setCreateRequest); - if (image != null && !image.isEmpty()) { - String uid = UUID.randomUUID().toString(); - - String currentPath = "backend/src/main/resources/images/"; - File checkPathFile = new File(currentPath); - if (!checkPathFile.exists()) { - checkPathFile.mkdirs(); - } - - File savingImage = new File(currentPath + uid + "_" + image.getOriginalFilename()); - image.transferTo(savingImage.toPath()); - String savePath = savingImage.toPath().toString(); - - quizSet.setImage(savePath); - } + QuizSet quizSet = quizSetService.createQuizSet(userId, quizSetCreateRequest.getTitle()); for (QuizCreateRequest quizCreateRequest : quizSetCreateRequest.getQuizzes()) { - quizService.createQuiz(quizSet.getId(), quizCreateRequest); + quizService.createQuiz(quizSet, quizCreateRequest); } return new ResponseEntity<>(HttpStatus.CREATED); @@ -62,7 +44,7 @@ public class QuizController { @GetMapping("/{quizsetId}") public ResponseEntity getQuizzes(@PathVariable Long quizsetId) { - QuizSet quizSet = quizSetService.findQuizSet(quizsetId); + QuizSetResponse quizSet = quizSetService.findQuizSetResponse(quizsetId); return new ResponseEntity<>(quizSet, HttpStatus.OK); } diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/Choice.java b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/Choice.java new file mode 100644 index 0000000..d31f8a1 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/Choice.java @@ -0,0 +1,29 @@ +package com.edufocus.edufocus.quiz.entity; + +import com.fasterxml.jackson.annotation.JsonBackReference; +import jakarta.persistence.*; +import lombok.*; + +@Entity +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class Choice { + + @Id + @GeneratedValue + private Long id; + + @ManyToOne + @JoinColumn(name = "QuizId") + @JsonBackReference + private Quiz quiz; + + @Column + private int num; + + @Column + private String content; +} diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/SetCreateRequest.java b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/ChoiceCreateRequest.java similarity index 68% rename from backend/src/main/java/com/edufocus/edufocus/quiz/entity/SetCreateRequest.java rename to backend/src/main/java/com/edufocus/edufocus/quiz/entity/ChoiceCreateRequest.java index 83caff8..80f8fc4 100644 --- a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/SetCreateRequest.java +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/ChoiceCreateRequest.java @@ -7,10 +7,9 @@ import lombok.NoArgsConstructor; @Getter @NoArgsConstructor @AllArgsConstructor -public class SetCreateRequest { +public class ChoiceCreateRequest { - private Long UserId; - - private String title; + private int num; + private String content; } 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 ad429ac..f9a3cc7 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,11 +1,12 @@ package com.edufocus.edufocus.quiz.entity; -import com.edufocus.edufocus.user.model.entity.UserRole; import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonManagedReference; import jakarta.persistence.*; import lombok.*; +import java.util.List; + @Entity @Getter @Setter @@ -24,31 +25,17 @@ public class Quiz { private QuizSet quizSet; @Column - private String title; - - @Column - private String description; - - @Column - private String answer; - - @Enumerated(EnumType.STRING) - private QuizType quizType; + private String question; @Column private String image; @Column - private String choice1; + private String answer; - @Column - private String choice2; - - @Column - private String choice3; - - @Column - private String choice4; + @OneToMany(mappedBy = "quiz") + @JsonManagedReference + private List choices; public void setQuizSet(QuizSet quizSet) { this.quizSet = quizSet; @@ -57,4 +44,12 @@ public class Quiz { quizSet.getQuizzes().remove(this); } } + + public void addChoice(Choice choice) { + this.choices.add(choice); + + if (choice.getQuiz() != this) { + choice.setQuiz(this); + } + } } 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 c140c79..9ac60bd 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 @@ -4,24 +4,16 @@ import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; +import java.util.List; + @Getter @NoArgsConstructor @AllArgsConstructor public class QuizCreateRequest { - private String title; - - private String description; + private String question; private String answer; - private String quizType; - - private String choice1; - - private String choice2; - - private String choice3; - - private String choice4; + private List choices; } diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizResponse.java b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizResponse.java new file mode 100644 index 0000000..0b0be9f --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizResponse.java @@ -0,0 +1,21 @@ +package com.edufocus.edufocus.quiz.entity; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class QuizResponse { + + private String question; + + private String image; + + private List choices; +} diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizSetCreateRequest.java b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizSetCreateRequest.java index 20a0025..0a137a3 100644 --- a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizSetCreateRequest.java +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizSetCreateRequest.java @@ -11,12 +11,8 @@ import java.util.List; @AllArgsConstructor public class QuizSetCreateRequest { - private Long UserId; - private String title; - private String image; - private List quizzes; } \ No newline at end of file diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizSetResponse.java b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizSetResponse.java new file mode 100644 index 0000000..9fdaf5e --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizSetResponse.java @@ -0,0 +1,20 @@ +package com.edufocus.edufocus.quiz.entity; + +import lombok.*; + +import java.util.ArrayList; +import java.util.List; + +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class QuizSetResponse { + + private String title; + + private String image; + + private List quizzes; +} diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizType.java b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizType.java deleted file mode 100644 index b58f56c..0000000 --- a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizType.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.edufocus.edufocus.quiz.entity; - -public enum QuizType { - SINGLE, MULTIPLE -} diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/repository/ChoiceRepository.java b/backend/src/main/java/com/edufocus/edufocus/quiz/repository/ChoiceRepository.java new file mode 100644 index 0000000..740dda2 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/repository/ChoiceRepository.java @@ -0,0 +1,8 @@ +package com.edufocus.edufocus.quiz.repository; + +import com.edufocus.edufocus.quiz.entity.Choice; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ChoiceRepository extends JpaRepository { + +} 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 9e7d345..c271ee2 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,7 +3,6 @@ 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/ChoiceService.java b/backend/src/main/java/com/edufocus/edufocus/quiz/service/ChoiceService.java new file mode 100644 index 0000000..22b81d1 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/service/ChoiceService.java @@ -0,0 +1,7 @@ +package com.edufocus.edufocus.quiz.service; + +import org.springframework.stereotype.Service; + +@Service +public interface ChoiceService { +} diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/service/ChoiceServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/quiz/service/ChoiceServiceImpl.java new file mode 100644 index 0000000..8f1b2c0 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/service/ChoiceServiceImpl.java @@ -0,0 +1,25 @@ +package com.edufocus.edufocus.quiz.service; + +import com.edufocus.edufocus.quiz.entity.Choice; +import com.edufocus.edufocus.quiz.entity.ChoiceCreateRequest; +import com.edufocus.edufocus.quiz.repository.ChoiceRepository; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@Transactional +@RequiredArgsConstructor +public class ChoiceServiceImpl implements ChoiceService { + + private final ChoiceRepository choiceRepository; + + public void createChoice(ChoiceCreateRequest choiceCreateRequest) { + Choice choice = new Choice().builder() + .num(choiceCreateRequest.getNum()) + .content(choiceCreateRequest.getContent()) + .build(); + + choiceRepository.save(choice); + } +} 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 8b0725f..60c81a5 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,6 +1,7 @@ package com.edufocus.edufocus.quiz.service; import com.edufocus.edufocus.quiz.entity.QuizCreateRequest; +import com.edufocus.edufocus.quiz.entity.QuizSet; import org.springframework.stereotype.Service; import java.util.List; @@ -8,8 +9,7 @@ import java.util.List; @Service public interface QuizService { - void createQuiz(long quizSetId, QuizCreateRequest - QuizCreateRequest); + void createQuiz(QuizSet quizSet, QuizCreateRequest quizCreateRequest); boolean deleteQuiz(long quizId); } 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 0412f68..12c9543 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,43 +1,50 @@ package com.edufocus.edufocus.quiz.service; -import com.edufocus.edufocus.quiz.entity.Quiz; -import com.edufocus.edufocus.quiz.entity.QuizCreateRequest; -import com.edufocus.edufocus.quiz.entity.QuizSet; -import com.edufocus.edufocus.quiz.entity.QuizType; +import com.edufocus.edufocus.quiz.entity.*; +import com.edufocus.edufocus.quiz.repository.ChoiceRepository; import com.edufocus.edufocus.quiz.repository.QuizRepository; import com.edufocus.edufocus.quiz.repository.QuizSetRepository; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.List; + + @Service @Transactional @RequiredArgsConstructor public class QuizServiceImpl implements QuizService { + private final ChoiceRepository choiceRepository; + private final QuizRepository quizRepository; private final QuizSetRepository quizSetRepository; @Override - public void createQuiz(long quizSetId, QuizCreateRequest quizCreateRequest) { - QuizSet quizSet = quizSetRepository.findById(quizSetId).get(); - + public void createQuiz(QuizSet quizSet, QuizCreateRequest quizCreateRequest) { Quiz quiz = new Quiz().builder() - .title(quizCreateRequest.getTitle()) - .description(quizCreateRequest.getDescription()) - .answer(quizCreateRequest.getAnswer()) - .quizType(QuizType.valueOf(quizCreateRequest.getQuizType())) .quizSet(quizSet) + .question(quizCreateRequest.getQuestion()) + .answer(quizCreateRequest.getAnswer()) .build(); - if (!quiz.getQuizType().equals(QuizType.MULTIPLE)) { - quiz.setChoice1(quizCreateRequest.getChoice1()); - quiz.setChoice2(quizCreateRequest.getChoice2()); - quiz.setChoice3(quizCreateRequest.getChoice3()); - quiz.setChoice4(quizCreateRequest.getChoice4()); + List choices = new ArrayList<>(); + + for (ChoiceCreateRequest choiceCreateRequest : quizCreateRequest.getChoices()) { + Choice choice = new Choice().builder() + .quiz(quiz) + .num(choiceCreateRequest.getNum()) + .content(choiceCreateRequest.getContent()) + .build(); + choices.add(choice); + choiceRepository.save(choice); } + quiz.setChoices(choices); + quizRepository.save(quiz); } 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 a62a979..14f3e2b 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 @@ -1,17 +1,20 @@ 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.QuizSetResponse; import org.springframework.stereotype.Service; @Service public interface QuizSetService { - QuizSet createQuizSet(SetCreateRequest setCreateRequest); + QuizSet createQuizSet(Long userId, String title); void updateQuizSet(QuizSet quizSet); void deleteQuizSet(long quizSetId); QuizSet findQuizSet(long quizSetId); + + QuizSetResponse findQuizSetResponse(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 1836ce5..ee582ee 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 @@ -9,6 +9,9 @@ import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.List; + @Service @Transactional @RequiredArgsConstructor @@ -18,16 +21,13 @@ public class QuizSetServiceImpl implements QuizSetService { private final UserRepository userRepository; - @Override - public QuizSet createQuizSet(SetCreateRequest setCreateRequest) { + public QuizSet createQuizSet(Long userId, String title) { QuizSet quizSet = new QuizSet(); - User user = userRepository.findById(setCreateRequest.getUserId()).get(); - + User user = userRepository.findById(userId).get(); quizSet.setUser(user); - - quizSet.setTitle(setCreateRequest.getTitle()); + quizSet.setTitle(title); return quizSetRepository.save(quizSet); } @@ -46,4 +46,28 @@ public class QuizSetServiceImpl implements QuizSetService { public QuizSet findQuizSet(long quizSetId) { return quizSetRepository.findById(quizSetId).get(); } + + @Override + public QuizSetResponse findQuizSetResponse(long quizSetId) { + QuizSet quizSet = findQuizSet(quizSetId); + + List quizResponses = new ArrayList<>(); + for (Quiz quiz : quizSet.getQuizzes()) { + QuizResponse quizResponse = new QuizResponse().builder() + .question(quiz.getQuestion()) + .choices(quiz.getChoices()) + .build(); + quizResponses.add(quizResponse); + } + + QuizSetResponse quizSetResponse = new QuizSetResponse().builder() + .title(quizSet.getTitle()) + .image(quizSet.getImage()) + .quizzes(quizResponses) + .build(); + + return quizSetResponse; + } + + } diff --git a/backend/src/main/java/com/edufocus/edufocus/registration/controller/RegistrationController.java b/backend/src/main/java/com/edufocus/edufocus/registration/controller/RegistrationController.java index 1d28aaf..ee36efb 100644 --- a/backend/src/main/java/com/edufocus/edufocus/registration/controller/RegistrationController.java +++ b/backend/src/main/java/com/edufocus/edufocus/registration/controller/RegistrationController.java @@ -26,11 +26,9 @@ public class RegistrationController { Long lectureId = map.get("lectureId"); if (!registrationServiceImpl.createRegistration(userId, lectureId)) { - String msg = new String("Duplicated Registration"); - return new ResponseEntity<>(msg, HttpStatus.CONFLICT); + return new ResponseEntity<>(HttpStatus.CONFLICT); } - String msg = new String("registration successful"); return new ResponseEntity<>(HttpStatus.CREATED); } From 97c25fff13f8c6c465303028ea12b3f2b2d0d843 Mon Sep 17 00:00:00 2001 From: kgc9007 Date: Tue, 30 Jul 2024 14:23:41 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20Quiz=20=EC=82=AC=EC=A7=84=20?= =?UTF-8?q?=EB=93=B1=EB=A1=9D,=20=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C?= =?UTF-8?q?=20=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 | 30 +++++++++++-- .../quiz/entity/MyQuizSetResponse.java | 14 ++++++ .../edufocus/edufocus/quiz/entity/Quiz.java | 2 +- .../edufocus/quiz/entity/QuizSet.java | 7 +-- .../edufocus/quiz/entity/QuizSetResponse.java | 3 -- .../quiz/repository/ChoiceRepository.java | 8 ---- .../quiz/repository/QuizSetRepository.java | 3 ++ .../edufocus/quiz/service/ChoiceService.java | 7 --- .../quiz/service/ChoiceServiceImpl.java | 25 ----------- .../edufocus/quiz/service/QuizService.java | 5 ++- .../quiz/service/QuizServiceImpl.java | 39 ++++++++-------- .../edufocus/quiz/service/QuizSetService.java | 10 +++-- .../quiz/service/QuizSetServiceImpl.java | 45 +++++++++++++++---- 13 files changed, 114 insertions(+), 84 deletions(-) create mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/entity/MyQuizSetResponse.java delete mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/repository/ChoiceRepository.java delete mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/service/ChoiceService.java delete mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/service/ChoiceServiceImpl.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 ac58dd2..cf0ecc8 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 @@ -12,9 +12,8 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; -import java.io.File; import java.io.IOException; -import java.util.UUID; +import java.util.List; @RestController @RequestMapping("/quiz") @@ -30,13 +29,14 @@ public class QuizController { @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public ResponseEntity createQuizSet(@RequestHeader("Authorization") String accessToken, @RequestPart QuizSetCreateRequest quizSetCreateRequest - , @RequestPart(value = "image", required = false) MultipartFile image) throws IOException { + , @RequestPart(value = "images", required = false) List images) throws IOException { Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); QuizSet quizSet = quizSetService.createQuizSet(userId, quizSetCreateRequest.getTitle()); + int imageIdx = 0; for (QuizCreateRequest quizCreateRequest : quizSetCreateRequest.getQuizzes()) { - quizService.createQuiz(quizSet, quizCreateRequest); + quizService.createQuiz(quizSet, quizCreateRequest, images.get(imageIdx++)); } return new ResponseEntity<>(HttpStatus.CREATED); @@ -46,6 +46,28 @@ public class QuizController { public ResponseEntity getQuizzes(@PathVariable Long quizsetId) { QuizSetResponse quizSet = quizSetService.findQuizSetResponse(quizsetId); + if (quizSet == null) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + return new ResponseEntity<>(quizSet, HttpStatus.OK); } + + @DeleteMapping("/{quizsetId}") + public ResponseEntity deleteQuizSet(@RequestHeader("Authorization") String accessToken, @PathVariable Long quizsetId) { + Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); + + if (!quizSetService.deleteQuizSet(userId, quizsetId)) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + + @GetMapping + public ResponseEntity getQuizSets(@RequestHeader("Authorization") String accessToken) { + Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); + + return new ResponseEntity<>(quizSetService.findMyQuizSetResponses(userId), HttpStatus.OK); + } } diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/MyQuizSetResponse.java b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/MyQuizSetResponse.java new file mode 100644 index 0000000..da8947f --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/MyQuizSetResponse.java @@ -0,0 +1,14 @@ +package com.edufocus.edufocus.quiz.entity; + +import lombok.*; + +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class MyQuizSetResponse { + + private Long quizSetId; + + private String title; +} 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 f9a3cc7..7eed889 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 @@ -33,7 +33,7 @@ public class Quiz { @Column private String answer; - @OneToMany(mappedBy = "quiz") + @OneToMany(mappedBy = "quiz", orphanRemoval = true, cascade = CascadeType.ALL) @JsonManagedReference private List choices; 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 89a2b4d..8357279 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 @@ -27,12 +27,9 @@ public class QuizSet { @Column private String title; - @Column - private String image; - - @OneToMany(mappedBy = "quizSet") + @OneToMany(mappedBy = "quizSet", orphanRemoval = true) @JsonManagedReference - private List quizzes = new ArrayList(); + private List quizzes = new ArrayList(); public void addQuiz(Quiz quiz) { this.quizzes.add(quiz); diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizSetResponse.java b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizSetResponse.java index 9fdaf5e..1d4098d 100644 --- a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizSetResponse.java +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizSetResponse.java @@ -2,7 +2,6 @@ package com.edufocus.edufocus.quiz.entity; import lombok.*; -import java.util.ArrayList; import java.util.List; @Getter @@ -14,7 +13,5 @@ public class QuizSetResponse { private String title; - private String image; - private List quizzes; } diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/repository/ChoiceRepository.java b/backend/src/main/java/com/edufocus/edufocus/quiz/repository/ChoiceRepository.java deleted file mode 100644 index 740dda2..0000000 --- a/backend/src/main/java/com/edufocus/edufocus/quiz/repository/ChoiceRepository.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.edufocus.edufocus.quiz.repository; - -import com.edufocus.edufocus.quiz.entity.Choice; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface ChoiceRepository extends JpaRepository { - -} diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/repository/QuizSetRepository.java b/backend/src/main/java/com/edufocus/edufocus/quiz/repository/QuizSetRepository.java index 81883a6..89bc7bc 100644 --- a/backend/src/main/java/com/edufocus/edufocus/quiz/repository/QuizSetRepository.java +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/repository/QuizSetRepository.java @@ -3,6 +3,9 @@ package com.edufocus.edufocus.quiz.repository; import com.edufocus.edufocus.quiz.entity.QuizSet; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; + public interface QuizSetRepository extends JpaRepository { + List findQuizSetsByUserId(Long userId); } diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/service/ChoiceService.java b/backend/src/main/java/com/edufocus/edufocus/quiz/service/ChoiceService.java deleted file mode 100644 index 22b81d1..0000000 --- a/backend/src/main/java/com/edufocus/edufocus/quiz/service/ChoiceService.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.edufocus.edufocus.quiz.service; - -import org.springframework.stereotype.Service; - -@Service -public interface ChoiceService { -} diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/service/ChoiceServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/quiz/service/ChoiceServiceImpl.java deleted file mode 100644 index 8f1b2c0..0000000 --- a/backend/src/main/java/com/edufocus/edufocus/quiz/service/ChoiceServiceImpl.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.edufocus.edufocus.quiz.service; - -import com.edufocus.edufocus.quiz.entity.Choice; -import com.edufocus.edufocus.quiz.entity.ChoiceCreateRequest; -import com.edufocus.edufocus.quiz.repository.ChoiceRepository; -import jakarta.transaction.Transactional; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; - -@Service -@Transactional -@RequiredArgsConstructor -public class ChoiceServiceImpl implements ChoiceService { - - private final ChoiceRepository choiceRepository; - - public void createChoice(ChoiceCreateRequest choiceCreateRequest) { - Choice choice = new Choice().builder() - .num(choiceCreateRequest.getNum()) - .content(choiceCreateRequest.getContent()) - .build(); - - choiceRepository.save(choice); - } -} 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 60c81a5..92014a2 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 @@ -3,13 +3,14 @@ package com.edufocus.edufocus.quiz.service; import com.edufocus.edufocus.quiz.entity.QuizCreateRequest; import com.edufocus.edufocus.quiz.entity.QuizSet; import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; +import java.io.IOException; import java.util.List; @Service public interface QuizService { - void createQuiz(QuizSet quizSet, QuizCreateRequest quizCreateRequest); + void createQuiz(QuizSet quizSet, QuizCreateRequest quizCreateRequest, MultipartFile quizImage) throws IOException; - boolean deleteQuiz(long quizId); } 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 12c9543..b25adfb 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,15 +1,17 @@ package com.edufocus.edufocus.quiz.service; import com.edufocus.edufocus.quiz.entity.*; -import com.edufocus.edufocus.quiz.repository.ChoiceRepository; import com.edufocus.edufocus.quiz.repository.QuizRepository; -import com.edufocus.edufocus.quiz.repository.QuizSetRepository; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; +import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.UUID; @Service @@ -17,14 +19,10 @@ import java.util.List; @RequiredArgsConstructor public class QuizServiceImpl implements QuizService { - private final ChoiceRepository choiceRepository; - private final QuizRepository quizRepository; - private final QuizSetRepository quizSetRepository; - @Override - public void createQuiz(QuizSet quizSet, QuizCreateRequest quizCreateRequest) { + public void createQuiz(QuizSet quizSet, QuizCreateRequest quizCreateRequest, MultipartFile quizImage) throws IOException { Quiz quiz = new Quiz().builder() .quizSet(quizSet) .question(quizCreateRequest.getQuestion()) @@ -40,23 +38,28 @@ public class QuizServiceImpl implements QuizService { .content(choiceCreateRequest.getContent()) .build(); choices.add(choice); - choiceRepository.save(choice); } quiz.setChoices(choices); + if (quizImage != null && !quizImage.isEmpty()) { + String uid = UUID.randomUUID().toString(); + + String currentPath = "backend/src/main/resources/images/quizzes/"; + File checkPathFile = new File(currentPath); + if (!checkPathFile.exists()) { + checkPathFile.mkdirs(); + } + + File savingImage = new File(currentPath + uid + "_" + quizImage.getOriginalFilename()); + quizImage.transferTo(savingImage.toPath()); + String savePath = savingImage.toPath().toString(); + + quiz.setImage(savePath); + } + quizRepository.save(quiz); } - @Override - public boolean deleteQuiz(long quizId) { - // 유저 아이디 정보 조회 후 검증 로직 추가 예정 - // jwt -> 로그인 유저 정보 조회 - // quizId -> 퀴즈 정보 조회 -> 퀴즈셋 정보 조회 - // 퀴즈셋 생성자와 로그인 유저의 id값이 일치하는지 확인 -> 불일치시 삭제 실패 - - 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 14f3e2b..1f8e4d7 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 @@ -1,9 +1,12 @@ package com.edufocus.edufocus.quiz.service; +import com.edufocus.edufocus.quiz.entity.MyQuizSetResponse; import com.edufocus.edufocus.quiz.entity.QuizSet; import com.edufocus.edufocus.quiz.entity.QuizSetResponse; import org.springframework.stereotype.Service; +import java.util.List; + @Service public interface QuizSetService { @@ -11,10 +14,11 @@ public interface QuizSetService { void updateQuizSet(QuizSet quizSet); - void deleteQuizSet(long quizSetId); + boolean deleteQuizSet(Long userId, Long quizSetId); - QuizSet findQuizSet(long quizSetId); + QuizSet findQuizSet(Long quizSetId); - QuizSetResponse findQuizSetResponse(long quizSetId); + QuizSetResponse findQuizSetResponse(Long quizSetId); + List findMyQuizSetResponses(Long userId); } 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 ee582ee..c65a071 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,6 @@ package com.edufocus.edufocus.quiz.service; 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; @@ -11,6 +10,7 @@ import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; +import java.util.Optional; @Service @Transactional @@ -38,36 +38,65 @@ public class QuizSetServiceImpl implements QuizSetService { } @Override - public void deleteQuizSet(long quizSetId) { + public boolean deleteQuizSet(Long userId, Long quizSetId) { + Optional quizSet = quizSetRepository.findById(quizSetId); + + if (quizSet.isEmpty() || userId != quizSet.get().getUser().getId()) { + return false; + } + quizSetRepository.deleteById(quizSetId); + return true; } @Override - public QuizSet findQuizSet(long quizSetId) { + public QuizSet findQuizSet(Long quizSetId) { return quizSetRepository.findById(quizSetId).get(); } @Override - public QuizSetResponse findQuizSetResponse(long quizSetId) { - QuizSet quizSet = findQuizSet(quizSetId); + public QuizSetResponse findQuizSetResponse(Long quizSetId) { + Optional quizSet = quizSetRepository.findById(quizSetId); + + if (quizSet.isEmpty()) { + return null; + } + List quizResponses = new ArrayList<>(); - for (Quiz quiz : quizSet.getQuizzes()) { + for (Quiz quiz : quizSet.get().getQuizzes()) { QuizResponse quizResponse = new QuizResponse().builder() .question(quiz.getQuestion()) + .image(quiz.getImage()) .choices(quiz.getChoices()) .build(); quizResponses.add(quizResponse); } QuizSetResponse quizSetResponse = new QuizSetResponse().builder() - .title(quizSet.getTitle()) - .image(quizSet.getImage()) + .title(quizSet.get().getTitle()) .quizzes(quizResponses) .build(); return quizSetResponse; } + @Override + public List findMyQuizSetResponses(Long userId) { + List quizSetList = quizSetRepository.findQuizSetsByUserId(userId); + + List myQuizSetResponses = new ArrayList<>(); + for (QuizSet quizSet : quizSetList) { + MyQuizSetResponse myQuizSetResponse = new MyQuizSetResponse().builder() + .quizSetId(quizSet.getId()) + .title(quizSet.getTitle()) + .build(); + + myQuizSetResponses.add(myQuizSetResponse); + } + + return myQuizSetResponses; + + } }