-
Notifications
You must be signed in to change notification settings - Fork 3
[feat] 퀴즈 생성 기능 추가 #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2b9349e
:sparkles: feat : 퀴즈 생성 기능 구현
silver-eunjoo 679ee07
chore: Java 스타일 수정
3800aa8
:wrench: chore : 테스트 application.yml 수정
silver-eunjoo 0c1e44c
:recycle: refactor : PR 리뷰 반영 (메서드 네이밍, static import, 빌더 패턴 삭제)
silver-eunjoo e2d396c
chore: Java 스타일 수정
14b7a63
:lipstick: style : 파라미터 네이밍 변경 thumbnailFile로 변경
silver-eunjoo a9a3a55
Merge branch 'dev' into feat/4
silver-eunjoo 8a1973f
:bug: fix : 충돌 해결 및 기본생성자 접근 임시로 해제
silver-eunjoo b304ece
chore: Java 스타일 수정
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -40,4 +40,7 @@ out/ | |
| .env | ||
|
|
||
| ### .idea ### | ||
| .idea | ||
| .idea | ||
|
|
||
| ### images/thumbnail ### | ||
| images/thumbnail/** | ||
36 changes: 36 additions & 0 deletions
36
backend/src/main/java/io/f1/backend/domain/question/app/QuestionService.java
This file contains hidden or 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,36 @@ | ||
| package io.f1.backend.domain.question.app; | ||
|
|
||
| import static io.f1.backend.domain.question.mapper.QuestionMapper.questionRequestToQuestion; | ||
| import static io.f1.backend.domain.question.mapper.TextQuestionMapper.questionRequestToTextQuestion; | ||
|
|
||
| import io.f1.backend.domain.question.dao.QuestionRepository; | ||
| import io.f1.backend.domain.question.dao.TextQuestionRepository; | ||
| import io.f1.backend.domain.question.dto.QuestionRequest; | ||
| import io.f1.backend.domain.question.entity.Question; | ||
| import io.f1.backend.domain.question.entity.TextQuestion; | ||
| import io.f1.backend.domain.quiz.entity.Quiz; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class QuestionService { | ||
|
|
||
| private final QuestionRepository questionRepository; | ||
| private final TextQuestionRepository textQuestionRepository; | ||
|
|
||
| @Transactional | ||
| public void saveQuestion(Quiz quiz, QuestionRequest request) { | ||
|
|
||
| Question question = questionRequestToQuestion(quiz, request); | ||
| quiz.addQuestion(question); | ||
| questionRepository.save(question); | ||
|
|
||
| TextQuestion textQuestion = questionRequestToTextQuestion(question, request.getContent()); | ||
| textQuestionRepository.save(textQuestion); | ||
| question.addTextQuestion(textQuestion); | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
backend/src/main/java/io/f1/backend/domain/question/dao/QuestionRepository.java
This file contains hidden or 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,7 @@ | ||
| package io.f1.backend.domain.question.dao; | ||
|
|
||
| import io.f1.backend.domain.question.entity.Question; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface QuestionRepository extends JpaRepository<Question, Long> {} |
7 changes: 7 additions & 0 deletions
7
backend/src/main/java/io/f1/backend/domain/question/dao/TextQuestionRepository.java
This file contains hidden or 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,7 @@ | ||
| package io.f1.backend.domain.question.dao; | ||
|
|
||
| import io.f1.backend.domain.question.entity.TextQuestion; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface TextQuestionRepository extends JpaRepository<TextQuestion, Long> {} |
18 changes: 18 additions & 0 deletions
18
backend/src/main/java/io/f1/backend/domain/question/dto/QuestionRequest.java
This file contains hidden or 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,18 @@ | ||
| package io.f1.backend.domain.question.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
|
|
||
| import lombok.AccessLevel; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| public class QuestionRequest { | ||
|
|
||
| @NotBlank(message = "문제를 입력해주세요.") | ||
| private String content; | ||
|
|
||
| @NotBlank(message = "정답을 입력해주세요.") | ||
| private String answer; | ||
| } |
This file contains hidden or 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 hidden or 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
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/io/f1/backend/domain/question/mapper/QuestionMapper.java
This file contains hidden or 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,12 @@ | ||
| package io.f1.backend.domain.question.mapper; | ||
|
|
||
| import io.f1.backend.domain.question.dto.QuestionRequest; | ||
| import io.f1.backend.domain.question.entity.Question; | ||
| import io.f1.backend.domain.quiz.entity.Quiz; | ||
|
|
||
| public class QuestionMapper { | ||
|
|
||
| public static Question questionRequestToQuestion(Quiz quiz, QuestionRequest questionRequest) { | ||
| return new Question(quiz, questionRequest.getAnswer()); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
backend/src/main/java/io/f1/backend/domain/question/mapper/TextQuestionMapper.java
This file contains hidden or 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,11 @@ | ||
| package io.f1.backend.domain.question.mapper; | ||
|
|
||
| import io.f1.backend.domain.question.entity.Question; | ||
| import io.f1.backend.domain.question.entity.TextQuestion; | ||
|
|
||
| public class TextQuestionMapper { | ||
|
|
||
| public static TextQuestion questionRequestToTextQuestion(Question question, String content) { | ||
| return new TextQuestion(question, content); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
backend/src/main/java/io/f1/backend/domain/quiz/api/QuizController.java
This file contains hidden or 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,38 @@ | ||
| package io.f1.backend.domain.quiz.api; | ||
|
|
||
| import io.f1.backend.domain.quiz.app.QuizService; | ||
| import io.f1.backend.domain.quiz.dto.QuizCreateRequest; | ||
| import io.f1.backend.domain.quiz.dto.QuizCreateResponse; | ||
|
|
||
| import jakarta.validation.Valid; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestPart; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/quizzes") | ||
| @RequiredArgsConstructor | ||
| public class QuizController { | ||
|
|
||
| private final QuizService quizService; | ||
|
|
||
| @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE) | ||
| public ResponseEntity<QuizCreateResponse> saveQuiz( | ||
| @RequestPart(required = false) MultipartFile thumbnailFile, | ||
| @Valid @RequestPart QuizCreateRequest request) | ||
| throws IOException { | ||
| QuizCreateResponse response = quizService.saveQuiz(thumbnailFile, request); | ||
|
|
||
| return ResponseEntity.status(HttpStatus.CREATED).body(response); | ||
| } | ||
| } |
94 changes: 94 additions & 0 deletions
94
backend/src/main/java/io/f1/backend/domain/quiz/app/QuizService.java
This file contains hidden or 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,94 @@ | ||
| package io.f1.backend.domain.quiz.app; | ||
|
|
||
| import static io.f1.backend.domain.quiz.mapper.QuizMapper.quizCreateRequestToQuiz; | ||
| import static io.f1.backend.domain.quiz.mapper.QuizMapper.quizToQuizCreateResponse; | ||
|
|
||
| import io.f1.backend.domain.question.app.QuestionService; | ||
| import io.f1.backend.domain.question.dto.QuestionRequest; | ||
| import io.f1.backend.domain.quiz.dao.QuizRepository; | ||
| import io.f1.backend.domain.quiz.dto.QuizCreateRequest; | ||
| import io.f1.backend.domain.quiz.dto.QuizCreateResponse; | ||
| import io.f1.backend.domain.quiz.entity.Quiz; | ||
| import io.f1.backend.domain.user.dao.UserRepository; | ||
| import io.f1.backend.domain.user.entity.User; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class QuizService { | ||
|
|
||
| @Value("${file.thumbnail-path}") | ||
| private String uploadPath; | ||
|
|
||
| @Value("${file.default-thumbnail-url}") | ||
| private String defaultThumbnailPath; | ||
|
|
||
| // TODO : 시큐리티 구현 이후 삭제해도 되는 의존성 주입 | ||
| private final UserRepository userRepository; | ||
| private final QuestionService questionService; | ||
| private final QuizRepository quizRepository; | ||
|
|
||
| @Transactional | ||
| public QuizCreateResponse saveQuiz(MultipartFile thumbnailFile, QuizCreateRequest request) | ||
| throws IOException { | ||
| String thumbnailPath = defaultThumbnailPath; | ||
|
|
||
| if (thumbnailFile != null && !thumbnailFile.isEmpty()) { | ||
| validateImageFile(thumbnailFile); | ||
| thumbnailPath = convertToThumbnailPath(thumbnailFile); | ||
| } | ||
|
|
||
| // TODO : 시큐리티 구현 이후 삭제 (data.sql로 초기 저장해둔 유저 get), 나중엔 현재 로그인한 유저의 아이디를 받아오도록 수정 | ||
| User user = userRepository.findById(1L).orElseThrow(RuntimeException::new); | ||
|
|
||
| Quiz quiz = quizCreateRequestToQuiz(request, thumbnailPath, user); | ||
|
|
||
| Quiz savedQuiz = quizRepository.save(quiz); | ||
|
|
||
| for (QuestionRequest qRequest : request.getQuestions()) { | ||
| questionService.saveQuestion(savedQuiz, qRequest); | ||
| } | ||
|
|
||
| return quizToQuizCreateResponse(savedQuiz); | ||
| } | ||
|
|
||
| private void validateImageFile(MultipartFile thumbnailFile) { | ||
|
|
||
| if (!thumbnailFile.getContentType().startsWith("image")) { | ||
| // TODO : 이후 커스텀 예외로 변경 | ||
| throw new IllegalArgumentException("이미지 파일을 업로드해주세요."); | ||
| } | ||
|
|
||
| List<String> allowedExt = List.of("jpg", "jpeg", "png", "webp"); | ||
| if (!allowedExt.contains(getExtension(thumbnailFile.getOriginalFilename()))) { | ||
| throw new IllegalArgumentException("지원하지 않는 확장자입니다."); | ||
| } | ||
| } | ||
|
|
||
| private String convertToThumbnailPath(MultipartFile thumbnailFile) throws IOException { | ||
| String originalFilename = thumbnailFile.getOriginalFilename(); | ||
| String ext = getExtension(originalFilename); | ||
| String savedFilename = UUID.randomUUID().toString() + "." + ext; | ||
|
|
||
| Path savePath = Paths.get(uploadPath, savedFilename).toAbsolutePath(); | ||
| thumbnailFile.transferTo(savePath.toFile()); | ||
|
|
||
| return "/images/thumbnail/" + savedFilename; | ||
| } | ||
|
|
||
| private String getExtension(String filename) { | ||
| return filename.substring(filename.lastIndexOf(".") + 1); | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
backend/src/main/java/io/f1/backend/domain/quiz/dao/QuizRepository.java
This file contains hidden or 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,7 @@ | ||
| package io.f1.backend.domain.quiz.dao; | ||
|
|
||
| import io.f1.backend.domain.quiz.entity.Quiz; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface QuizRepository extends JpaRepository<Quiz, Long> {} |
31 changes: 31 additions & 0 deletions
31
backend/src/main/java/io/f1/backend/domain/quiz/dto/QuizCreateRequest.java
This file contains hidden or 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,31 @@ | ||
| package io.f1.backend.domain.quiz.dto; | ||
|
|
||
| import io.f1.backend.domain.question.dto.QuestionRequest; | ||
| import io.f1.backend.domain.quiz.entity.QuizType; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import jakarta.validation.constraints.Size; | ||
|
|
||
| import lombok.AccessLevel; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Getter | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| public class QuizCreateRequest { | ||
|
|
||
| @NotBlank(message = "퀴즈 제목을 설정해주세요.") | ||
| private String title; | ||
|
|
||
| @NotNull(message = "퀴즈 종류를 선택해주세요.") | ||
| private QuizType quizType; | ||
|
|
||
| @NotBlank(message = "퀴즈 설명을 적어주세요.") | ||
| private String description; | ||
|
|
||
| @Size(min = 10, max = 80, message = "문제는 최소 10개, 최대 80개로 정해주세요.") | ||
| private List<QuestionRequest> questions; | ||
| } |
11 changes: 11 additions & 0 deletions
11
backend/src/main/java/io/f1/backend/domain/quiz/dto/QuizCreateResponse.java
This file contains hidden or 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,11 @@ | ||
| package io.f1.backend.domain.quiz.dto; | ||
|
|
||
| import io.f1.backend.domain.quiz.entity.QuizType; | ||
|
|
||
| public record QuizCreateResponse( | ||
| Long id, | ||
| String title, | ||
| QuizType quizType, | ||
| String description, | ||
| String thumbnailUrl, | ||
| Long creatorId) {} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[L4-변경제안]
현재 id를 제외한 모든 필드가 필수값으로 보이는데 이런 상황에선 생성자를 사용하게되면 필수값들을 강제할 수 있다는 점에서 빌더보단 생성자가 의도가 더 명확하지않나 생각됩니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
의견 감사합니다 ! 사실 처음에 생성자로 했다가 빌더로 변경했었는데, 그 이유가 아무래도 들어가는 필드값이 많다보니 생성자로 해두었을 때 가독성이 낮고, 어떤 필드에 뭐가 들어가는건지 좀 보기 힘들더라구요..! 그래서 빌더패턴으로 수정했었는데,,,
세희님 리뷰 듣고 저 두 특징에 대해 고민을 좀 해봤는데, 어차피
dto -> entity를 mapper로 분리해놨으니, 서비스에서 이 생성자 코드를 볼 일은 없다고 생각이 되고, 그렇게 되면 가독성 이슈를 조금 덜 신경써도 되겠다는 생각이 듭니다! 그래서 필드값을 강제할 수 있는 생성자로 수정하는 것도 괜찮겠다고 생각이드네요..! 이 부분 수정하겠습니다 👍