forked from kookmin-sw/cap-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
229 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
backend/src/main/java/com/project/capstone/quiz/controller/QuizController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.project.capstone.quiz.controller; | ||
|
||
|
||
import com.project.capstone.auth.domain.PrincipalDetails; | ||
import com.project.capstone.quiz.controller.dto.CreateQuizRequest; | ||
import com.project.capstone.quiz.controller.dto.QuizResponse; | ||
import com.project.capstone.quiz.service.QuizService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/quiz") | ||
public class QuizController { | ||
private final QuizService quizService; | ||
|
||
// 퀴즈 생성 | ||
@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); | ||
return ResponseEntity.ok().body("퀴즈 생성 완료"); | ||
} | ||
|
||
// 단건 조회 | ||
@GetMapping("/{id}") | ||
public ResponseEntity<QuizResponse> getQuiz(@PathVariable Long id) { | ||
QuizResponse quizResponse = quizService.getQuiz(id); | ||
return ResponseEntity.ok().body(quizResponse); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
backend/src/main/java/com/project/capstone/quiz/controller/dto/CreateQuizRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.project.capstone.quiz.controller.dto; | ||
|
||
import com.project.capstone.quiz.domain.QuizType; | ||
|
||
public record CreateQuizRequest( | ||
QuizType type, | ||
String description, | ||
String answer, | ||
String example1, | ||
String example2, | ||
String example3, | ||
String example4 | ||
) { | ||
} |
25 changes: 25 additions & 0 deletions
25
backend/src/main/java/com/project/capstone/quiz/controller/dto/QuizResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.project.capstone.quiz.controller.dto; | ||
|
||
import com.project.capstone.quiz.domain.Quiz; | ||
import com.project.capstone.quiz.domain.QuizType; | ||
|
||
import java.util.UUID; | ||
|
||
public record QuizResponse ( | ||
Long id, | ||
UUID memberId, | ||
Long bookId, | ||
Long clubId, | ||
QuizType type, | ||
String description, | ||
String answer, | ||
String example1, | ||
String example2, | ||
String example3, | ||
String example4 | ||
) { | ||
public QuizResponse(Quiz quiz) { | ||
this(quiz.getId(), quiz.getMember().getId(), quiz.getBook().getId(), quiz.getClub() == null ? null : quiz.getClub().getId(), quiz.getType(), | ||
quiz.getDescription(), quiz.getAnswer(), quiz.getExample1(), quiz.getExample2(), quiz.getExample3(), quiz.getExample4()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
backend/src/main/java/com/project/capstone/quiz/domain/QuizRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.project.capstone.quiz.domain; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface QuizRepository extends JpaRepository<Quiz, Long> { | ||
Optional<Quiz> findQuizById(Long id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
backend/src/main/java/com/project/capstone/quiz/exception/QuizException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.project.capstone.quiz.exception; | ||
|
||
import com.project.capstone.common.exception.BaseException; | ||
import com.project.capstone.common.exception.ExceptionType; | ||
|
||
public class QuizException extends BaseException { | ||
public QuizException(ExceptionType exceptionType) { | ||
super(exceptionType); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
backend/src/main/java/com/project/capstone/quiz/exception/QuizExceptionType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.project.capstone.quiz.exception; | ||
|
||
import com.project.capstone.common.exception.ExceptionType; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
|
||
import static org.springframework.http.HttpStatus.NOT_FOUND; | ||
|
||
@AllArgsConstructor | ||
public enum QuizExceptionType implements ExceptionType { | ||
QUIZ_NOT_FOUND(NOT_FOUND, 701, "해당 퀴즈를 찾을 수 없습니다.") | ||
; | ||
|
||
private final HttpStatus status; | ||
private final int exceptionCode; | ||
private final String message; | ||
|
||
@Override | ||
public HttpStatus httpStatus() { | ||
return status; | ||
} | ||
|
||
@Override | ||
public int exceptionCode() { | ||
return exceptionCode; | ||
} | ||
|
||
@Override | ||
public String message() { | ||
return message; | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
backend/src/main/java/com/project/capstone/quiz/service/QuizService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.project.capstone.quiz.service; | ||
|
||
import com.project.capstone.book.domain.Book; | ||
import com.project.capstone.book.domain.BookRepository; | ||
import com.project.capstone.book.exception.BookException; | ||
import com.project.capstone.book.exception.BookExceptionType; | ||
import com.project.capstone.club.domain.Club; | ||
import com.project.capstone.club.domain.ClubRepository; | ||
import com.project.capstone.club.exception.ClubException; | ||
import com.project.capstone.club.exception.ClubExceptionType; | ||
import com.project.capstone.member.domain.Member; | ||
import com.project.capstone.member.domain.MemberRepository; | ||
import com.project.capstone.member.exception.MemberException; | ||
import com.project.capstone.member.exception.MemberExceptionType; | ||
import com.project.capstone.quiz.controller.dto.CreateQuizRequest; | ||
import com.project.capstone.quiz.controller.dto.QuizResponse; | ||
import com.project.capstone.quiz.domain.Quiz; | ||
import com.project.capstone.quiz.domain.QuizRepository; | ||
import com.project.capstone.quiz.exception.QuizException; | ||
import com.project.capstone.quiz.exception.QuizExceptionType; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.UUID; | ||
|
||
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; | ||
import static com.project.capstone.quiz.exception.QuizExceptionType.QUIZ_NOT_FOUND; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
@Slf4j | ||
public class QuizService { | ||
|
||
private final QuizRepository quizRepository; | ||
private final MemberRepository memberRepository; | ||
private final BookRepository bookRepository; | ||
private final ClubRepository clubRepository; | ||
|
||
public void createQuiz(String userId, CreateQuizRequest request, Long bookId, Long clubId) { | ||
Member member = memberRepository.findMemberById(UUID.fromString(userId)).orElseThrow( | ||
() -> new MemberException(MEMBER_NOT_FOUND) | ||
); | ||
|
||
Book book = bookRepository.findBookById(bookId).orElseThrow( | ||
() -> new BookException(BOOK_NOT_FOUND) | ||
); | ||
Club club; | ||
if (clubId == null) { | ||
club = null; | ||
} | ||
else { | ||
club = clubRepository.findClubById(clubId).orElseThrow( | ||
()-> new ClubException(CLUB_NOT_FOUND) | ||
); | ||
} | ||
|
||
Quiz saved = quizRepository.save( | ||
Quiz.builder() | ||
.type(request.type()) | ||
.description(request.description()) | ||
.answer(request.answer()) | ||
.example1(request.example1()) | ||
.example2(request.example2()) | ||
.example3(request.example3()) | ||
.example4(request.example4()) | ||
.member(member) | ||
.book(book) | ||
.club(club) | ||
.build() | ||
); | ||
|
||
member.getQuizzes().add(saved); | ||
book.getQuizzes().add(saved); | ||
if (club != null) { | ||
club.getQuizzes().add(saved); | ||
} | ||
} | ||
|
||
|
||
public QuizResponse getQuiz(Long id) { | ||
Quiz quiz = quizRepository.findQuizById(id).orElseThrow( | ||
() -> new QuizException(QUIZ_NOT_FOUND) | ||
); | ||
return new QuizResponse(quiz); | ||
} | ||
} |