Skip to content

Commit

Permalink
Merge pull request #72 from kookmin-sw/BE_Refactor
Browse files Browse the repository at this point in the history
퀴즈 생성 로직 변경
  • Loading branch information
wjdwlghks authored May 13, 2024
2 parents 156eef7 + 762295e commit 4edad0e
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -11,10 +14,12 @@ public record AssignmentResponse(
String name,
String startDate,
String endDate,
List<ContentResponse> contentList
List<ContentResponse> contentList,
List<QuizResponse> 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<ContentResponse> createContentList(List<Content> contents) {
Expand All @@ -24,4 +29,12 @@ private static List<ContentResponse> createContentList(List<Content> contents) {
}
return contentResponseList;
}

private static List<QuizResponse> createQuizList(List<Quiz> quizzes) {
List<QuizResponse> quizResponseList = new ArrayList<>();
for (Quiz quiz : quizzes) {
quizResponseList.add(new QuizResponse(quiz));
}
return quizResponseList;
}
}
Original file line number Diff line number Diff line change
@@ -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
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -36,11 +42,15 @@ public class Assignment {
@OneToMany(mappedBy = "assignment")
List<Content> contents = new ArrayList<>();

@JsonManagedReference
@OneToMany(mappedBy = "assignment")
List<Quiz> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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("잘못된 과제 타입 입니다.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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, "과제 타입이 서로 맞지 않습니다.")
;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}


Expand All @@ -94,6 +96,7 @@ public void createContent(String userId, ContentCreateRequest request, Long club
.member(member)
.book(book)
.club(club)
.assignment(assignment)
.build()
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("퀴즈 생성 완료");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -45,4 +46,8 @@ public class Quiz {
@ManyToOne
private Club club;

@JsonBackReference
@ManyToOne
private Assignment assignment;

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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(
Expand All @@ -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);
}
Expand Down

0 comments on commit 4edad0e

Please sign in to comment.