diff --git a/backend/src/main/java/com/project/capstone/assignment/controller/dto/AssignmentResponse.java b/backend/src/main/java/com/project/capstone/assignment/controller/dto/AssignmentResponse.java index b7dfe5b85b..40a90a61cd 100644 --- a/backend/src/main/java/com/project/capstone/assignment/controller/dto/AssignmentResponse.java +++ b/backend/src/main/java/com/project/capstone/assignment/controller/dto/AssignmentResponse.java @@ -3,6 +3,9 @@ import com.project.capstone.assignment.domain.Assignment; import com.project.capstone.content.controller.dto.ContentResponse; import com.project.capstone.content.domain.Content; +import com.project.capstone.quiz.controller.dto.QuizResponse; +import com.project.capstone.quiz.domain.Quiz; + import java.util.ArrayList; import java.util.List; @@ -11,10 +14,12 @@ public record AssignmentResponse( String name, String startDate, String endDate, - List contentList + List contentList, + List quizList ) { public AssignmentResponse(Assignment assignment) { - this(assignment.getId(), assignment.getName(), assignment.getStartDate(), assignment.getEndDate(), createContentList(assignment.getContents())); + this(assignment.getId(), assignment.getName(), assignment.getStartDate(), + assignment.getEndDate(), createContentList(assignment.getContents()), createQuizList(assignment.getQuizzes())); } private static List createContentList(List contents) { @@ -24,4 +29,12 @@ private static List createContentList(List contents) { } return contentResponseList; } + + private static List createQuizList(List quizzes) { + List quizResponseList = new ArrayList<>(); + for (Quiz quiz : quizzes) { + quizResponseList.add(new QuizResponse(quiz)); + } + return quizResponseList; + } } diff --git a/backend/src/main/java/com/project/capstone/assignment/controller/dto/CreateAssignmentRequest.java b/backend/src/main/java/com/project/capstone/assignment/controller/dto/CreateAssignmentRequest.java index 797bb9cc22..8e51af33dd 100644 --- a/backend/src/main/java/com/project/capstone/assignment/controller/dto/CreateAssignmentRequest.java +++ b/backend/src/main/java/com/project/capstone/assignment/controller/dto/CreateAssignmentRequest.java @@ -1,7 +1,10 @@ package com.project.capstone.assignment.controller.dto; +import com.project.capstone.assignment.domain.AssignmentType; + public record CreateAssignmentRequest( String name, + AssignmentType type, String startDate, String endDate ) { diff --git a/backend/src/main/java/com/project/capstone/assignment/domain/Assignment.java b/backend/src/main/java/com/project/capstone/assignment/domain/Assignment.java index 820b75cca5..d68c81805d 100644 --- a/backend/src/main/java/com/project/capstone/assignment/domain/Assignment.java +++ b/backend/src/main/java/com/project/capstone/assignment/domain/Assignment.java @@ -5,6 +5,8 @@ import com.project.capstone.assignment.controller.dto.CreateAssignmentRequest; import com.project.capstone.club.domain.Club; import com.project.capstone.content.domain.Content; +import com.project.capstone.content.domain.ContentType; +import com.project.capstone.quiz.domain.Quiz; import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Getter; @@ -26,6 +28,10 @@ public class Assignment { private String name; + @Enumerated(EnumType.STRING) + @Column(name = "assignment_type") + private AssignmentType assignmentType; + @Column(name = "start_date") private String startDate; @@ -36,11 +42,15 @@ public class Assignment { @OneToMany(mappedBy = "assignment") List contents = new ArrayList<>(); + @JsonManagedReference + @OneToMany(mappedBy = "assignment") + List quizzes = new ArrayList<>(); + @JsonBackReference @ManyToOne private Club club; public Assignment(CreateAssignmentRequest request, Club club) { - this(null, request.name(), request.startDate(), request.endDate(), new ArrayList<>(), club); + this(null, request.name(), request.type(), request.startDate(), request.endDate(), new ArrayList<>(), new ArrayList<>(), club); } } diff --git a/backend/src/main/java/com/project/capstone/assignment/domain/AssignmentType.java b/backend/src/main/java/com/project/capstone/assignment/domain/AssignmentType.java new file mode 100644 index 0000000000..436e3498e0 --- /dev/null +++ b/backend/src/main/java/com/project/capstone/assignment/domain/AssignmentType.java @@ -0,0 +1,30 @@ +package com.project.capstone.assignment.domain; + +import com.fasterxml.jackson.annotation.JsonCreator; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; + +@AllArgsConstructor +@Getter +@Slf4j +public enum AssignmentType { + + Review("독후감"), + Quotation("인용구"), + ShortReview("한줄평"), + Quiz("퀴즈") + ; + + private final String type; + + @JsonCreator(mode = JsonCreator.Mode.DELEGATING) + public static AssignmentType from(String type) { + for (AssignmentType assignmentType : AssignmentType.values()) { + if (assignmentType.getType().equals(type)) { + return assignmentType; + } + } + throw new RuntimeException("잘못된 과제 타입 입니다."); + } +} diff --git a/backend/src/main/java/com/project/capstone/assignment/exception/AssignmentExceptionType.java b/backend/src/main/java/com/project/capstone/assignment/exception/AssignmentExceptionType.java index 55e5379465..c9255e16e6 100644 --- a/backend/src/main/java/com/project/capstone/assignment/exception/AssignmentExceptionType.java +++ b/backend/src/main/java/com/project/capstone/assignment/exception/AssignmentExceptionType.java @@ -10,7 +10,8 @@ @AllArgsConstructor public enum AssignmentExceptionType implements ExceptionType { ASSIGNMENT_NOT_FOUND(NOT_FOUND, 601, "해당 과제를 찾을 수 없습니다."), - NOT_EXIST_BOOK(BAD_REQUEST, 602, "대표책이 지정되지 않았습니다.") + NOT_EXIST_BOOK(BAD_REQUEST, 602, "대표책이 지정되지 않았습니다."), + NOT_MATCH_TYPE(BAD_REQUEST, 603, "과제 타입이 서로 맞지 않습니다.") ; diff --git a/backend/src/main/java/com/project/capstone/assignment/service/AssignmentService.java b/backend/src/main/java/com/project/capstone/assignment/service/AssignmentService.java index cb4abaad62..a86202fdde 100644 --- a/backend/src/main/java/com/project/capstone/assignment/service/AssignmentService.java +++ b/backend/src/main/java/com/project/capstone/assignment/service/AssignmentService.java @@ -31,6 +31,7 @@ public void createAssignment(String userId, CreateAssignmentRequest request, Lon if (!club.getManagerId().toString().equals(userId)) { throw new ClubException(UNAUTHORIZED_ACTION); } + log.info(String.valueOf(request.type())); Assignment saved = assignmentRepository.save(new Assignment(request, club)); club.getAssignments().add(saved); diff --git a/backend/src/main/java/com/project/capstone/content/service/ContentService.java b/backend/src/main/java/com/project/capstone/content/service/ContentService.java index 354ad68fa2..75e3f7c00b 100644 --- a/backend/src/main/java/com/project/capstone/content/service/ContentService.java +++ b/backend/src/main/java/com/project/capstone/content/service/ContentService.java @@ -33,8 +33,7 @@ import java.util.UUID; import java.util.stream.Collectors; -import static com.project.capstone.assignment.exception.AssignmentExceptionType.ASSIGNMENT_NOT_FOUND; -import static com.project.capstone.assignment.exception.AssignmentExceptionType.NOT_EXIST_BOOK; +import static com.project.capstone.assignment.exception.AssignmentExceptionType.*; import static com.project.capstone.book.exception.BookExceptionType.BOOK_NOT_FOUND; import static com.project.capstone.club.exception.ClubExceptionType.CLUB_NOT_FOUND; import static com.project.capstone.content.exception.ContentExceptionType.CONTENT_NOT_FOUND; @@ -82,6 +81,9 @@ public void createContent(String userId, ContentCreateRequest request, Long club assignment = assignmentRepository.findAssignmentById(asId).orElseThrow( () -> new AssignmentException(ASSIGNMENT_NOT_FOUND) ); + if (!assignment.getAssignmentType().toString().equals(request.contentType().toString())) { + throw new AssignmentException(NOT_MATCH_TYPE); + } } @@ -94,6 +96,7 @@ public void createContent(String userId, ContentCreateRequest request, Long club .member(member) .book(book) .club(club) + .assignment(assignment) .build() ); diff --git a/backend/src/main/java/com/project/capstone/quiz/controller/QuizController.java b/backend/src/main/java/com/project/capstone/quiz/controller/QuizController.java index d8c9b0bd36..b5c088babd 100644 --- a/backend/src/main/java/com/project/capstone/quiz/controller/QuizController.java +++ b/backend/src/main/java/com/project/capstone/quiz/controller/QuizController.java @@ -20,8 +20,8 @@ public class QuizController { @PostMapping("/create") public ResponseEntity createQuiz(@AuthenticationPrincipal PrincipalDetails details, @RequestBody CreateQuizRequest request, - @RequestParam Long bookId, @RequestParam(required = false) Long clubId) { - quizService.createQuiz(details.getUserId(), request, bookId, clubId); + @RequestParam(required = false) Long clubId, @RequestParam(required = false) Long asId) { + quizService.createQuiz(details.getUserId(), request, clubId, asId); return ResponseEntity.ok().body("퀴즈 생성 완료"); } diff --git a/backend/src/main/java/com/project/capstone/quiz/controller/dto/CreateQuizRequest.java b/backend/src/main/java/com/project/capstone/quiz/controller/dto/CreateQuizRequest.java index 15b091bc78..360972bb09 100644 --- a/backend/src/main/java/com/project/capstone/quiz/controller/dto/CreateQuizRequest.java +++ b/backend/src/main/java/com/project/capstone/quiz/controller/dto/CreateQuizRequest.java @@ -1,8 +1,11 @@ package com.project.capstone.quiz.controller.dto; +import com.project.capstone.book.controller.dto.AddBookRequest; import com.project.capstone.quiz.domain.QuizType; public record CreateQuizRequest( + + AddBookRequest addBookRequest, QuizType type, String description, String answer, diff --git a/backend/src/main/java/com/project/capstone/quiz/domain/Quiz.java b/backend/src/main/java/com/project/capstone/quiz/domain/Quiz.java index 58f2611cd2..bf98032f85 100644 --- a/backend/src/main/java/com/project/capstone/quiz/domain/Quiz.java +++ b/backend/src/main/java/com/project/capstone/quiz/domain/Quiz.java @@ -1,6 +1,7 @@ package com.project.capstone.quiz.domain; import com.fasterxml.jackson.annotation.JsonBackReference; +import com.project.capstone.assignment.domain.Assignment; import com.project.capstone.book.domain.Book; import com.project.capstone.club.domain.Club; import com.project.capstone.member.domain.Member; @@ -45,4 +46,8 @@ public class Quiz { @ManyToOne private Club club; + @JsonBackReference + @ManyToOne + private Assignment assignment; + } diff --git a/backend/src/main/java/com/project/capstone/quiz/service/QuizService.java b/backend/src/main/java/com/project/capstone/quiz/service/QuizService.java index 4579b9d9cd..333ca5ba8a 100644 --- a/backend/src/main/java/com/project/capstone/quiz/service/QuizService.java +++ b/backend/src/main/java/com/project/capstone/quiz/service/QuizService.java @@ -1,5 +1,9 @@ package com.project.capstone.quiz.service; +import com.project.capstone.assignment.domain.Assignment; +import com.project.capstone.assignment.domain.AssignmentRepository; +import com.project.capstone.assignment.exception.AssignmentException; +import com.project.capstone.assignment.exception.AssignmentExceptionType; import com.project.capstone.book.domain.Book; import com.project.capstone.book.domain.BookRepository; import com.project.capstone.book.exception.BookException; @@ -24,6 +28,7 @@ import java.util.UUID; +import static com.project.capstone.assignment.exception.AssignmentExceptionType.*; import static com.project.capstone.book.exception.BookExceptionType.BOOK_NOT_FOUND; import static com.project.capstone.club.exception.ClubExceptionType.CLUB_NOT_FOUND; import static com.project.capstone.member.exception.MemberExceptionType.MEMBER_NOT_FOUND; @@ -38,15 +43,18 @@ public class QuizService { private final MemberRepository memberRepository; private final BookRepository bookRepository; private final ClubRepository clubRepository; + private final AssignmentRepository assignmentRepository; + private final static String QUIZ_TYPE = "퀴즈"; - public void createQuiz(String userId, CreateQuizRequest request, Long bookId, Long clubId) { + public void createQuiz(String userId, CreateQuizRequest request, Long clubId, Long asId) { Member member = memberRepository.findMemberById(UUID.fromString(userId)).orElseThrow( () -> new MemberException(MEMBER_NOT_FOUND) ); - Book book = bookRepository.findBookById(bookId).orElseThrow( - () -> new BookException(BOOK_NOT_FOUND) + Book book = bookRepository.findBookByIsbn(request.addBookRequest().isbn()).orElseGet( + () -> bookRepository.save(new Book(request.addBookRequest())) ); + Club club; if (clubId == null) { club = null; @@ -55,6 +63,22 @@ public void createQuiz(String userId, CreateQuizRequest request, Long bookId, Lo club = clubRepository.findClubById(clubId).orElseThrow( ()-> new ClubException(CLUB_NOT_FOUND) ); + if (club.getBook() == null) { + throw new AssignmentException(NOT_EXIST_BOOK); + } + } + + Assignment assignment; + if (asId == null) { + assignment = null; + } + else { + assignment = assignmentRepository.findAssignmentById(asId).orElseThrow( + () -> new AssignmentException(ASSIGNMENT_NOT_FOUND) + ); + if (!assignment.getAssignmentType().toString().equals(QUIZ_TYPE)) { + throw new AssignmentException(NOT_MATCH_TYPE); + } } Quiz saved = quizRepository.save( @@ -69,11 +93,15 @@ public void createQuiz(String userId, CreateQuizRequest request, Long bookId, Lo .member(member) .book(book) .club(club) + .assignment(assignment) .build() ); member.getQuizzes().add(saved); book.getQuizzes().add(saved); + if (assignment != null) { + assignment.getQuizzes().add(saved); + } if (club != null) { club.getQuizzes().add(saved); }