From 3a7204cfba812f1f49bbfe67ae6be21e6c78dea9 Mon Sep 17 00:00:00 2001 From: kgc91747 Date: Thu, 1 Aug 2024 15:02:44 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20Quiz=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lecture/service/LectureServiceImpl.java | 2 +- .../quiz/controller/QuizController.java | 12 ++--- .../edufocus/edufocus/quiz/entity/Choice.java | 2 +- .../quiz/entity/MyQuizSetResponse.java | 2 +- .../edufocus/edufocus/quiz/entity/Quiz.java | 2 +- .../edufocus/quiz/entity/QuizSet.java | 10 +++-- .../quiz/repository/QuizSetRepository.java | 2 +- .../edufocus/quiz/service/QuizSetService.java | 10 ++--- .../quiz/service/QuizSetServiceImpl.java | 45 +++++++++---------- .../report/service/ReportServiceImpl.java | 2 +- 10 files changed, 41 insertions(+), 48 deletions(-) diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java index 4c1bc84..7c202eb 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java @@ -46,7 +46,7 @@ public class LectureServiceImpl implements LectureService { .time(lectureCreateRequest.getTime()) .build(); - if (image != null && !image.isEmpty()) { + if (image != null) { String uid = UUID.randomUUID().toString(); String imagePath = imagePathProperties.getPath(); 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 cf0ecc8..3a27228 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 @@ -30,7 +30,7 @@ public class QuizController { @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public ResponseEntity createQuizSet(@RequestHeader("Authorization") String accessToken, @RequestPart QuizSetCreateRequest quizSetCreateRequest , @RequestPart(value = "images", required = false) List images) throws IOException { - Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); + long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); QuizSet quizSet = quizSetService.createQuizSet(userId, quizSetCreateRequest.getTitle()); @@ -46,16 +46,12 @@ 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)); + 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); @@ -66,7 +62,7 @@ public class QuizController { @GetMapping public ResponseEntity getQuizSets(@RequestHeader("Authorization") String accessToken) { - Long userId = Long.parseLong(jwtUtil.getUserId(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/Choice.java b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/Choice.java index d31f8a1..8078db1 100644 --- a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/Choice.java +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/Choice.java @@ -14,7 +14,7 @@ public class Choice { @Id @GeneratedValue - private Long id; + private long id; @ManyToOne @JoinColumn(name = "QuizId") 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 index da8947f..9bf177c 100644 --- a/backend/src/main/java/com/edufocus/edufocus/quiz/entity/MyQuizSetResponse.java +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/MyQuizSetResponse.java @@ -8,7 +8,7 @@ import lombok.*; @AllArgsConstructor public class MyQuizSetResponse { - private Long quizSetId; + 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 7eed889..d0737b6 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 @@ -17,7 +17,7 @@ public class Quiz { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + private long id; @ManyToOne @JoinColumn(name = "quizset_id") 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 8357279..816638c 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 @@ -4,8 +4,7 @@ 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; +import lombok.*; import java.util.ArrayList; import java.util.List; @@ -13,11 +12,14 @@ import java.util.List; @Entity @Getter @Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor public class QuizSet { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + private long id; @ManyToOne @JoinColumn(name = "user_id") @@ -29,7 +31,7 @@ public class QuizSet { @OneToMany(mappedBy = "quizSet", orphanRemoval = true) @JsonManagedReference - private List quizzes = new ArrayList(); + private List quizzes; public void addQuiz(Quiz quiz) { this.quizzes.add(quiz); 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 89bc7bc..c3282c7 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 @@ -7,5 +7,5 @@ import java.util.List; public interface QuizSetRepository extends JpaRepository { - List findQuizSetsByUserId(Long userId); + List findQuizSetsByUserId(long userId); } 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 1f8e4d7..4b25ebc 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 @@ -10,15 +10,15 @@ import java.util.List; @Service public interface QuizSetService { - QuizSet createQuizSet(Long userId, String title); + QuizSet createQuizSet(long userId, String title); void updateQuizSet(QuizSet quizSet); - boolean deleteQuizSet(Long userId, 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); + 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 c65a071..c5c68c8 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 @@ -10,7 +10,7 @@ import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; -import java.util.Optional; +import java.util.NoSuchElementException; @Service @Transactional @@ -22,12 +22,13 @@ public class QuizSetServiceImpl implements QuizSetService { private final UserRepository userRepository; @Override - public QuizSet createQuizSet(Long userId, String title) { - QuizSet quizSet = new QuizSet(); + public QuizSet createQuizSet(long userId, String title) { + User user = userRepository.findById(userId).orElseThrow(NoSuchElementException::new); - User user = userRepository.findById(userId).get(); - quizSet.setUser(user); - quizSet.setTitle(title); + QuizSet quizSet = QuizSet.builder() + .user(user) + .title(title) + .build(); return quizSetRepository.save(quizSet); } @@ -38,10 +39,10 @@ public class QuizSetServiceImpl implements QuizSetService { } @Override - public boolean deleteQuizSet(Long userId, Long quizSetId) { - Optional quizSet = quizSetRepository.findById(quizSetId); + public boolean deleteQuizSet(long userId, long quizSetId) { + QuizSet quizSet = quizSetRepository.findById(quizSetId).orElseThrow(NoSuchElementException::new); - if (quizSet.isEmpty() || userId != quizSet.get().getUser().getId()) { + if (userId != quizSet.getUser().getId()) { return false; } @@ -50,22 +51,17 @@ public class QuizSetServiceImpl implements QuizSetService { } @Override - public QuizSet findQuizSet(Long quizSetId) { - return quizSetRepository.findById(quizSetId).get(); + public QuizSet findQuizSet(long quizSetId) { + return quizSetRepository.findById(quizSetId).orElseThrow(NoSuchElementException::new); } @Override - public QuizSetResponse findQuizSetResponse(Long quizSetId) { - Optional quizSet = quizSetRepository.findById(quizSetId); - - if (quizSet.isEmpty()) { - return null; - } - + public QuizSetResponse findQuizSetResponse(long quizSetId) { + QuizSet quizSet = quizSetRepository.findById(quizSetId).orElseThrow(NoSuchElementException::new); List quizResponses = new ArrayList<>(); - for (Quiz quiz : quizSet.get().getQuizzes()) { - QuizResponse quizResponse = new QuizResponse().builder() + for (Quiz quiz : quizSet.getQuizzes()) { + QuizResponse quizResponse = QuizResponse.builder() .question(quiz.getQuestion()) .image(quiz.getImage()) .choices(quiz.getChoices()) @@ -73,21 +69,20 @@ public class QuizSetServiceImpl implements QuizSetService { quizResponses.add(quizResponse); } - QuizSetResponse quizSetResponse = new QuizSetResponse().builder() - .title(quizSet.get().getTitle()) + return QuizSetResponse.builder() + .title(quizSet.getTitle()) .quizzes(quizResponses) .build(); - return quizSetResponse; } @Override - public List findMyQuizSetResponses(Long userId) { + public List findMyQuizSetResponses(long userId) { List quizSetList = quizSetRepository.findQuizSetsByUserId(userId); List myQuizSetResponses = new ArrayList<>(); for (QuizSet quizSet : quizSetList) { - MyQuizSetResponse myQuizSetResponse = new MyQuizSetResponse().builder() + MyQuizSetResponse myQuizSetResponse = MyQuizSetResponse.builder() .quizSetId(quizSet.getId()) .title(quizSet.getTitle()) .build(); diff --git a/backend/src/main/java/com/edufocus/edufocus/report/service/ReportServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/report/service/ReportServiceImpl.java index dd22a44..1d6449d 100644 --- a/backend/src/main/java/com/edufocus/edufocus/report/service/ReportServiceImpl.java +++ b/backend/src/main/java/com/edufocus/edufocus/report/service/ReportServiceImpl.java @@ -55,7 +55,7 @@ public class ReportServiceImpl implements ReportService { for (Quiz quiz : quizList) { for (AnswerInput answerInput : answerInputList) { - if (quiz.getId().equals(answerInput.getAnswerinputID())) { + if (quiz.getId() == answerInput.getAnswerinputID()) { if(quiz.getAnswer().equals(answerInput.getAnswer())) { correctCount++;