From 9757378bd70ee5ef3aac719fdc94f9660a4ae8af Mon Sep 17 00:00:00 2001 From: kgc9007 Date: Wed, 17 Jul 2024 17:37:49 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Lecture=20lecture=20=EB=93=B1=EB=A1=9D,?= =?UTF-8?q?=20=EC=82=AD=EC=A0=9C=20=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= =?UTF-8?q?=20Quiz=20=EC=97=94=ED=8B=B0=ED=8B=B0=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 | 23 ++++---- .../edufocus/lecture/entity/Lecture.java | 6 ++- .../lecture/entity/LectureRegist.java | 27 ++++++++++ .../lecture/repository/LectureRepository.java | 2 +- .../lecture/service/LectureService.java | 12 ++--- .../lecture/service/LectureServiceImpl.java | 44 ++++++++++----- .../quiz/controller/QuizController.java | 4 ++ .../edufocus/edufocus/quiz/entity/Quiz.java | 54 +++++++++++++++++++ .../edufocus/quiz/entity/QuizSet.java | 40 ++++++++++++++ .../quiz/repository/QuizRepository.java | 4 ++ .../quiz/repository/QuizSetRepository.java | 4 ++ .../edufocus/quiz/service/QuizService.java | 4 ++ .../quiz/service/QuizServiceImpl.java | 4 ++ .../edufocus/quiz/service/QuizSetService.java | 4 ++ .../quiz/service/QuizSetServiceImpl.java | 4 ++ .../edufocus/user/model/entity/User.java | 3 +- 16 files changed, 206 insertions(+), 33 deletions(-) create mode 100644 backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureRegist.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/controller/QuizController.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/entity/Quiz.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizSet.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/repository/QuizRepository.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/repository/QuizSetRepository.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizService.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizServiceImpl.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizSetService.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizSetServiceImpl.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 ca8e1ff..8cc015c 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,6 +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.service.LectureService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -19,26 +20,28 @@ public class LectureController { private final LectureService lectureService; @PostMapping - public ResponseEntity createLecture (@RequestBody Lecture lecture) { - lectureService.createLecture(lecture); + public ResponseEntity createLecture (@RequestBody long userId, @RequestBody LectureRegist lectureRegist) { + lectureService.createLecture(userId, lectureRegist); return new ResponseEntity<>(HttpStatus.CREATED); } @DeleteMapping("/{lectureId}") - public ResponseEntity deleteLecture (@PathVariable long lectureId) { - lectureService.deleteLecture(lectureId); + public ResponseEntity deleteLecture (@RequestBody long userId, @PathVariable long lectureId) { + if (!lectureService.deleteLecture(userId, lectureId)) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + return new ResponseEntity<>(HttpStatus.NO_CONTENT); } @GetMapping public ResponseEntity findAllLecture () { List lectures = lectureService.findAllLecture(); - return new ResponseEntity<>(lectures, HttpStatus.OK); - } - @GetMapping("/teacherId/{teacherId}") - public ResponseEntity findByTeacherId (@PathVariable String teacherId) { - List lectures = lectureService.findLectureByTeacherId(teacherId); + if (lectures.isEmpty()) { + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + return new ResponseEntity<>(lectures, HttpStatus.OK); } @@ -53,4 +56,6 @@ public class LectureController { return new ResponseEntity<>(lecture, HttpStatus.OK); } + + } diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/entity/Lecture.java b/backend/src/main/java/com/edufocus/edufocus/lecture/entity/Lecture.java index 5ae3972..6df25fc 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/entity/Lecture.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/entity/Lecture.java @@ -1,5 +1,6 @@ package com.edufocus.edufocus.lecture.entity; +import com.edufocus.edufocus.user.model.entity.User; import jakarta.persistence.*; import lombok.Getter; import lombok.Setter; @@ -15,8 +16,9 @@ public class Lecture { @Column private Long id; - @Column(name = "teacher_id") - private String teacherId; + @ManyToOne + @JoinColumn(name = "user_id") + private User user; @Column private String title; diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureRegist.java b/backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureRegist.java new file mode 100644 index 0000000..24790e3 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureRegist.java @@ -0,0 +1,27 @@ +package com.edufocus.edufocus.lecture.entity; + +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.Date; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class LectureRegist { + + private String title; + + private String description; + + @Temporal(TemporalType.DATE) + private Date startDate; + + @Temporal(TemporalType.DATE) + private Date endDate; + + private String plan; + +} diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/repository/LectureRepository.java b/backend/src/main/java/com/edufocus/edufocus/lecture/repository/LectureRepository.java index 032085b..6366507 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/repository/LectureRepository.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/repository/LectureRepository.java @@ -2,6 +2,7 @@ package com.edufocus.edufocus.lecture.repository; import com.edufocus.edufocus.lecture.entity.Lecture; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @@ -11,5 +12,4 @@ import java.util.List; public interface LectureRepository extends JpaRepository { Lecture findByTitle(@Param("title") String title); - List findByTeacherId(@Param("teacherId") String teacherId); } 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 777fd58..2731e5a 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,8 +1,7 @@ package com.edufocus.edufocus.lecture.service; import com.edufocus.edufocus.lecture.entity.Lecture; -import jakarta.transaction.Transactional; -import lombok.RequiredArgsConstructor; +import com.edufocus.edufocus.lecture.entity.LectureRegist; import org.springframework.stereotype.Service; import java.util.List; @@ -11,13 +10,12 @@ import java.util.List; @Service public interface LectureService { - void createLecture(Lecture lecture); + void createLecture(long userId, LectureRegist lectureRegist); - void deleteLecture(long lectureId); + boolean deleteLecture(long userId, long LectureId); + + List findAllLecture(); Lecture findLectureByTitle(String title); - List findLectureByTeacherId(String teacherId); - - List findAllLecture(); } 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 0431045..4f120d8 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,31 +1,57 @@ 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.repository.LectureRepository; +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.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; -import java.util.Optional; @Service @Transactional @RequiredArgsConstructor public class LectureServiceImpl implements LectureService { - @Autowired private final LectureRepository lectureRepository; + private final UserRepository userRepository; + @Override - public void createLecture(Lecture lecture) { + public void createLecture(long userId, LectureRegist lectureRegist) { + + 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()); + lectureRepository.save(lecture); } @Override - public void deleteLecture(long lectureId) { + public boolean deleteLecture(long userId, long lectureId) { + Lecture lecture = lectureRepository.findById(lectureId).get(); + + if (lecture.getUser().getId() != userId) { + return false; + } + lectureRepository.deleteById(lectureId); + return true; + } + + @Override + public List findAllLecture() { + return lectureRepository.findAll(); } @Override @@ -33,13 +59,5 @@ public class LectureServiceImpl implements LectureService { return lectureRepository.findByTitle(title); } - @Override - public List findLectureByTeacherId(String teacherId) { - return lectureRepository.findByTeacherId(teacherId); - } - @Override - public List findAllLecture() { - return lectureRepository.findAll(); - } } 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 new file mode 100644 index 0000000..b77b3d4 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/controller/QuizController.java @@ -0,0 +1,4 @@ +package com.edufocus.edufocus.quiz.controller; + +public class QuizController { +} 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 new file mode 100644 index 0000000..b997e38 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/Quiz.java @@ -0,0 +1,54 @@ +package com.edufocus.edufocus.quiz.entity; + +import jakarta.persistence.*; +import lombok.Getter; +import lombok.Setter; + +@Entity +@Getter +@Setter +public class Quiz { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private int id; + + @ManyToOne + @JoinColumn(name = "quizset_id") + private QuizSet quizSet; + + @Column + private String title; + + @Column + private String description; + + @Column + private String answer; + + @Column (name = "is_single") + private boolean isSingle; + + @Column + private String image; + + @Column + private String choice1; + + @Column + private String choice2; + + @Column + private String choice3; + + @Column + private String choice4; + + public void setQuizSet(QuizSet quizSet) { + this.quizSet = quizSet; + + if (!quizSet.getQuizzes().contains(this)) { + quizSet.getQuizzes().remove(this); + } + } +} 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 new file mode 100644 index 0000000..7198a4c --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/entity/QuizSet.java @@ -0,0 +1,40 @@ +package com.edufocus.edufocus.quiz.entity; + +import com.edufocus.edufocus.user.model.entity.User; +import jakarta.persistence.*; +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + +@Entity +@Getter +@Setter +public class QuizSet { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private int id; + + @ManyToOne + @JoinColumn(name = "user_id") + private User user; + + @Column + private String title; + + @Column + private String image; + + @OneToMany(mappedBy = "quizSet") + private List quizzes = new ArrayList(); + + public void addQuiz(Quiz quiz) { + this.quizzes.add(quiz); + + if (quiz.getQuizSet() != this) { + quiz.setQuizSet(this); + } + } +} 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 new file mode 100644 index 0000000..ba2085b --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/repository/QuizRepository.java @@ -0,0 +1,4 @@ +package com.edufocus.edufocus.quiz.repository; + +public interface QuizRepository { +} 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 new file mode 100644 index 0000000..820dff9 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/repository/QuizSetRepository.java @@ -0,0 +1,4 @@ +package com.edufocus.edufocus.quiz.repository; + +public interface QuizSetRepository { +} 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 new file mode 100644 index 0000000..3b84367 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizService.java @@ -0,0 +1,4 @@ +package com.edufocus.edufocus.quiz.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 new file mode 100644 index 0000000..0a06913 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizServiceImpl.java @@ -0,0 +1,4 @@ +package com.edufocus.edufocus.quiz.service; + +public class QuizServiceImpl { +} 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 new file mode 100644 index 0000000..bbf8570 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizSetService.java @@ -0,0 +1,4 @@ +package com.edufocus.edufocus.quiz.service; + +public interface QuizSetService { +} 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 new file mode 100644 index 0000000..d27c69b --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizSetServiceImpl.java @@ -0,0 +1,4 @@ +package com.edufocus.edufocus.quiz.service; + +public class QuizSetServiceImpl { +} diff --git a/backend/src/main/java/com/edufocus/edufocus/user/model/entity/User.java b/backend/src/main/java/com/edufocus/edufocus/user/model/entity/User.java index 8914cd3..78b62ea 100644 --- a/backend/src/main/java/com/edufocus/edufocus/user/model/entity/User.java +++ b/backend/src/main/java/com/edufocus/edufocus/user/model/entity/User.java @@ -17,7 +17,8 @@ public class User { @GeneratedValue(strategy = GenerationType.IDENTITY) // 자동 증가 설정 private Long id; - private String user_id; + @Column(name = "user_id", unique = true, nullable = false) + private String userId; private String email; private String password; @Enumerated(EnumType.STRING) // 혹은 EnumType.ORDINAL