From b9cd3de0432ab32700b1636058d0d8b5040b9d17 Mon Sep 17 00:00:00 2001 From: kgc9007 Date: Thu, 18 Jul 2024 14:55:54 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Quiz=20=ED=80=B4=EC=A6=88=EC=85=8B=20?= =?UTF-8?q?=EB=8B=A8=EC=9C=84=EB=A1=9C=20=ED=80=B4=EC=A6=88=20=EB=93=B1?= =?UTF-8?q?=EB=A1=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lecture/controller/LectureController.java | 6 +-- ...eRegist.java => LectureCreateRequest.java} | 2 +- .../lecture/service/LectureService.java | 4 +- .../lecture/service/LectureServiceImpl.java | 14 +++--- .../quiz/controller/QuizController.java | 42 +++++++++++++++++ .../quiz/entity/MutipleQuizCreateRequest.java | 20 ++++++++ .../edufocus/edufocus/quiz/entity/Quiz.java | 2 +- .../quiz/entity/QuizCreateRequest.java | 23 +++++++++ .../edufocus/quiz/entity/QuizSet.java | 2 +- .../quiz/entity/QuizSetCreateRequest.java | 22 +++++++++ .../quiz/entity/SetCreateRequest.java | 18 +++++++ .../quiz/repository/QuizRepository.java | 5 +- .../quiz/repository/QuizSetRepository.java | 6 ++- .../edufocus/quiz/service/QuizService.java | 10 ++++ .../quiz/service/QuizServiceImpl.java | 47 ++++++++++++++++++- .../edufocus/quiz/service/QuizSetService.java | 12 +++++ .../quiz/service/QuizSetServiceImpl.java | 43 ++++++++++++++++- 17 files changed, 259 insertions(+), 19 deletions(-) rename backend/src/main/java/com/edufocus/edufocus/lecture/entity/{LectureRegist.java => LectureCreateRequest.java} (92%) create mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/entity/MutipleQuizCreateRequest.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizCreateRequest.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizSetCreateRequest.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/entity/SetCreateRequest.java diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/controller/LectureController.java b/backend/src/main/java/com/edufocus/edufocus/lecture/controller/LectureController.java index 8cc015c..fd8471c 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/controller/LectureController.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/controller/LectureController.java @@ -1,7 +1,7 @@ package com.edufocus.edufocus.lecture.controller; import com.edufocus.edufocus.lecture.entity.Lecture; -import com.edufocus.edufocus.lecture.entity.LectureRegist; +import com.edufocus.edufocus.lecture.entity.LectureCreateRequest; import com.edufocus.edufocus.lecture.service.LectureService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -20,8 +20,8 @@ public class LectureController { private final LectureService lectureService; @PostMapping - public ResponseEntity createLecture (@RequestBody long userId, @RequestBody LectureRegist lectureRegist) { - lectureService.createLecture(userId, lectureRegist); + public ResponseEntity createLecture (@RequestBody long userId, @RequestBody LectureCreateRequest lectureCreateRequest) { + lectureService.createLecture(userId, lectureCreateRequest); return new ResponseEntity<>(HttpStatus.CREATED); } diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureRegist.java b/backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureCreateRequest.java similarity index 92% rename from backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureRegist.java rename to backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureCreateRequest.java index 24790e3..7ce8bfb 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureRegist.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureCreateRequest.java @@ -10,7 +10,7 @@ import java.util.Date; @Getter @NoArgsConstructor @AllArgsConstructor -public class LectureRegist { +public class LectureCreateRequest { private String title; diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureService.java b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureService.java index 2731e5a..cfd87e6 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureService.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureService.java @@ -1,7 +1,7 @@ package com.edufocus.edufocus.lecture.service; import com.edufocus.edufocus.lecture.entity.Lecture; -import com.edufocus.edufocus.lecture.entity.LectureRegist; +import com.edufocus.edufocus.lecture.entity.LectureCreateRequest; import org.springframework.stereotype.Service; import java.util.List; @@ -10,7 +10,7 @@ import java.util.List; @Service public interface LectureService { - void createLecture(long userId, LectureRegist lectureRegist); + void createLecture(long userId, LectureCreateRequest lectureCreateRequest); boolean deleteLecture(long userId, long LectureId); 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 4f120d8..79b7093 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 @@ -1,7 +1,7 @@ package com.edufocus.edufocus.lecture.service; import com.edufocus.edufocus.lecture.entity.Lecture; -import com.edufocus.edufocus.lecture.entity.LectureRegist; +import com.edufocus.edufocus.lecture.entity.LectureCreateRequest; import com.edufocus.edufocus.lecture.repository.LectureRepository; import com.edufocus.edufocus.user.model.entity.User; import com.edufocus.edufocus.user.model.repository.UserRepository; @@ -21,18 +21,18 @@ public class LectureServiceImpl implements LectureService { private final UserRepository userRepository; @Override - public void createLecture(long userId, LectureRegist lectureRegist) { + public void createLecture(long userId, LectureCreateRequest lectureCreateRequest) { User user = userRepository.findById(userId).get(); Lecture lecture = new Lecture(); lecture.setUser(user); - lecture.setTitle(lectureRegist.getTitle()); - lecture.setDescription(lectureRegist.getDescription()); - lecture.setStartDate(lectureRegist.getStartDate()); - lecture.setEndDate(lectureRegist.getEndDate()); - lecture.setPlan(lectureRegist.getPlan()); + lecture.setTitle(lectureCreateRequest.getTitle()); + lecture.setDescription(lectureCreateRequest.getDescription()); + lecture.setStartDate(lectureCreateRequest.getStartDate()); + lecture.setEndDate(lectureCreateRequest.getEndDate()); + lecture.setPlan(lectureCreateRequest.getPlan()); lectureRepository.save(lecture); } 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 b77b3d4..3c18199 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,4 +1,46 @@ package com.edufocus.edufocus.quiz.controller; +import com.edufocus.edufocus.quiz.entity.*; +import com.edufocus.edufocus.quiz.service.QuizService; +import com.edufocus.edufocus.quiz.service.QuizSetService; +import com.edufocus.edufocus.user.model.service.UserService; +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; + +@RestController +@RequestMapping("/quiz") +@Slf4j +@RequiredArgsConstructor public class QuizController { + + private final QuizService quizService; + + private final QuizSetService quizSetService; + + private final UserService userService; + + @PostMapping + public ResponseEntity createQuizSet(@RequestBody QuizSetCreateRequest quizSetCreateRequest) { + + Long userId = 1L; + + String title = quizSetCreateRequest.getTitle(); + String image = quizSetCreateRequest.getImage(); + + SetCreateRequest setCreateRequest = new SetCreateRequest(userId, title, image); + + QuizSet quizSet = quizSetService.createQuizSet(setCreateRequest); + + for (QuizCreateRequest quizCreateRequest : quizSetCreateRequest.getQuizzes()) { + quizService.createQuiz(quizSet.getId(), quizCreateRequest); + } + + return new ResponseEntity<>(HttpStatus.CREATED); + } } 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 new file mode 100644 index 0000000..20dbf54 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/MutipleQuizCreateRequest.java @@ -0,0 +1,20 @@ +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 b997e38..4ea9b43 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 @@ -11,7 +11,7 @@ public class Quiz { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private int id; + private Long id; @ManyToOne @JoinColumn(name = "quizset_id") 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 new file mode 100644 index 0000000..d5c38da --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizCreateRequest.java @@ -0,0 +1,23 @@ +package com.edufocus.edufocus.quiz.entity; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.ToString; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class QuizCreateRequest { + + private String title; + + private String description; + + private boolean isSingle; + + private String answer; + + private String image; + +} 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 7198a4c..b067277 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 @@ -15,7 +15,7 @@ public class QuizSet { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private int id; + private Long id; @ManyToOne @JoinColumn(name = "user_id") 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 new file mode 100644 index 0000000..20a0025 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizSetCreateRequest.java @@ -0,0 +1,22 @@ +package com.edufocus.edufocus.quiz.entity; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Getter +@NoArgsConstructor +@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/SetCreateRequest.java b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/SetCreateRequest.java new file mode 100644 index 0000000..a391ceb --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/SetCreateRequest.java @@ -0,0 +1,18 @@ +package com.edufocus.edufocus.quiz.entity; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class SetCreateRequest { + + private Long UserId; + + private String title; + + private String image; + +} 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 ba2085b..7428689 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 @@ -1,4 +1,7 @@ package com.edufocus.edufocus.quiz.repository; -public interface QuizRepository { +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/repository/QuizSetRepository.java b/backend/src/main/java/com/edufocus/edufocus/quiz/repository/QuizSetRepository.java index 820dff9..81883a6 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 @@ -1,4 +1,8 @@ package com.edufocus.edufocus.quiz.repository; -public interface QuizSetRepository { +import com.edufocus.edufocus.quiz.entity.QuizSet; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface QuizSetRepository 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 3b84367..3174e89 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,4 +1,14 @@ 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; + +@Service public interface QuizService { + + void createQuiz(long quizSetId, 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 0a06913..5f34010 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,4 +1,49 @@ package com.edufocus.edufocus.quiz.service; -public class QuizServiceImpl { +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; +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; + +@Service +@Transactional +@RequiredArgsConstructor +public class QuizServiceImpl implements QuizService { + + private final QuizRepository quizRepository; + + private final QuizSetRepository quizSetRepository; + + @Override + public void createQuiz(long quizSetId, QuizCreateRequest quizCreateRequest) { + QuizSet quizSet = quizSetRepository.findById(quizSetId).get(); + + Quiz quiz = new Quiz(); + + quiz.setTitle(quizCreateRequest.getTitle()); + quiz.setDescription(quizCreateRequest.getDescription()); + quiz.setAnswer(quizCreateRequest.getAnswer()); + quiz.setImage(quizCreateRequest.getImage()); + + + quiz.setQuizSet(quizSet); + + 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 bbf8570..748283b 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,4 +1,16 @@ package com.edufocus.edufocus.quiz.service; +import com.edufocus.edufocus.quiz.entity.QuizSet; +import com.edufocus.edufocus.quiz.entity.SetCreateRequest; +import org.springframework.stereotype.Service; + +@Service public interface QuizSetService { + + QuizSet createQuizSet(SetCreateRequest setCreateRequest); + + void updateQuizSet(QuizSet quizSet); + + void deleteQuizSet(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 d27c69b..a167cca 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,4 +1,45 @@ package com.edufocus.edufocus.quiz.service; -public class QuizSetServiceImpl { +import com.edufocus.edufocus.quiz.entity.QuizSet; +import com.edufocus.edufocus.quiz.entity.SetCreateRequest; +import com.edufocus.edufocus.quiz.repository.QuizSetRepository; +import com.edufocus.edufocus.user.model.entity.User; +import com.edufocus.edufocus.user.model.repository.UserRepository; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@Transactional +@RequiredArgsConstructor +public class QuizSetServiceImpl implements QuizSetService { + + private final QuizSetRepository quizSetRepository; + + private final UserRepository userRepository; + + + @Override + public QuizSet createQuizSet(SetCreateRequest setCreateRequest) { + QuizSet quizSet = new QuizSet(); + + User user = userRepository.findById(setCreateRequest.getUserId()).get(); + + quizSet.setUser(user); + + quizSet.setTitle(setCreateRequest.getTitle()); + quizSet.setImage(setCreateRequest.getImage()); + + return quizSetRepository.save(quizSet); + } + + @Override + public void updateQuizSet(QuizSet quizSet) { + + } + + @Override + public void deleteQuizSet(long quizSetId) { + quizSetRepository.deleteById(quizSetId); + } }