-
Notifications
You must be signed in to change notification settings - Fork 0
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
[FEAT] 시험 문제 생성, 수정, 삭제 기능 구현 #74
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
cbda27e
FIX: Test 빌드 오류 수정
hyerin315 688d5ef
REFACTOR: 역할 기반에서 기능 기반으로 로직 변경
hyerin315 3a010a6
REFACTOR: 주석 제거
hyerin315 236fe9b
REFACTOR: 시험 CRUD 엔드포인트 수정
hyerin315 c23a5c8
FEAT: 문제 생성을 위한 정답 필드 추가
hyerin315 ad84c84
FEAT: 문제 번호 재정렬을 위한 메소드 추가
hyerin315 4be63ec
FEAT: 시험 문제 엔드포인트 처리를 위한 Controller 구현
hyerin315 55fcf3d
FEAT: 시험 문제 생성을 위한 Dto 클래스 구현
hyerin315 50e693e
FEAT: 시험 문제 조회를 위한 Dto 클래스 구현
hyerin315 1e1b523
FEAT: 시험 조회 컴포넌트 구현
hyerin315 ede748a
FEAT: 문제 생성, 조회, 삭제를 위한 Service 구현
hyerin315 6c37a1b
FEAT: 문제 수정을 위한 Dto 클래스 구현
hyerin315 3915532
FEAT: 문제 조회을 위한 Repository 클래스 구현
hyerin315 a1690c7
FIX: 문제 DB 삭제가 안되는 문제 해결을 위한 메소드 수정
hyerin315 26f3783
Merge branch 'main' into feature/67-exam-question-management
hyerin315 35747b5
REFACTOR: 역할 권한 검증 방식 변경 및 Exam 객체 생성 방식 변경
hyerin315 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 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
86 changes: 86 additions & 0 deletions
86
src/main/java/com/example/epari/exam/controller/ExamQuestionController.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,86 @@ | ||
package com.example.epari.exam.controller; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.example.epari.exam.dto.request.CreateQuestionRequestDto; | ||
import com.example.epari.exam.dto.request.UpdateQuestionRequestDto; | ||
import com.example.epari.exam.dto.response.ExamQuestionResponseDto; | ||
import com.example.epari.exam.service.ExamQuestionService; | ||
import com.example.epari.global.annotation.CurrentUserEmail; | ||
|
||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
/** | ||
* 시험 문제 관련 HTTP 요청을 처리하는 Controller 클래스 | ||
*/ | ||
@RestController | ||
@RequestMapping("/api/courses/{courseId}/exams/{examId}/questions") | ||
@RequiredArgsConstructor | ||
public class ExamQuestionController { | ||
|
||
private final ExamQuestionService examQuestionService; | ||
|
||
// 시험 문제 생성 | ||
@PostMapping | ||
@PreAuthorize("hasRole('INSTRUCTOR') and @courseSecurityChecker.checkInstructorAccess(#courseId, #instructorEmail)") | ||
public ResponseEntity<Long> createQuestion( | ||
@PathVariable Long courseId, | ||
@PathVariable Long examId, | ||
@RequestBody @Valid CreateQuestionRequestDto request, | ||
@CurrentUserEmail String instructorEmail) { | ||
Long questionId = examQuestionService.addQuestion(courseId, examId, request, instructorEmail); | ||
return ResponseEntity.ok(questionId); | ||
} | ||
|
||
// 시험 문제 재정렬 | ||
@PutMapping("/{questionId}/order") | ||
@PreAuthorize("hasRole('INSTRUCTOR') and @courseSecurityChecker.checkInstructorAccess(#courseId, #instructorEmail)") | ||
public ResponseEntity<Void> reorderQuestion( | ||
@PathVariable Long courseId, | ||
@PathVariable Long examId, | ||
@PathVariable Long questionId, | ||
@RequestParam int newNumber, | ||
@CurrentUserEmail String instructorEmail) { | ||
examQuestionService.reorderQuestions(courseId, examId, questionId, newNumber, instructorEmail); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
// 시험 문제 수정 | ||
@PutMapping("/{questionId}") | ||
@PreAuthorize("hasRole('INSTRUCTOR') and @courseSecurityChecker.checkInstructorAccess(#courseId, #instructorEmail)") | ||
public ResponseEntity<ExamQuestionResponseDto> updateQuestion( | ||
@PathVariable Long courseId, | ||
@PathVariable Long examId, | ||
@PathVariable Long questionId, | ||
@RequestBody @Valid UpdateQuestionRequestDto requestDto, | ||
@CurrentUserEmail String instructorEmail) { | ||
|
||
ExamQuestionResponseDto updatedQuestion = examQuestionService.updateQuestion( | ||
courseId, examId, questionId, requestDto, instructorEmail); | ||
return ResponseEntity.ok(updatedQuestion); | ||
} | ||
|
||
// 시험 문제 삭제 | ||
@DeleteMapping("/{questionId}") | ||
@PreAuthorize("hasRole('INSTRUCTOR') and @courseSecurityChecker.checkInstructorAccess(#courseId, #instructorEmail)") | ||
public ResponseEntity<Void> deleteQuestion( | ||
@PathVariable Long courseId, | ||
@PathVariable Long examId, | ||
@PathVariable Long questionId, | ||
@CurrentUserEmail String instructorEmail) { | ||
|
||
examQuestionService.deleteQuestion(courseId, examId, questionId, instructorEmail); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
} |
33 changes: 0 additions & 33 deletions
33
src/main/java/com/example/epari/exam/controller/InstructorExamController.java
This file was deleted.
Oops, something went wrong.
33 changes: 0 additions & 33 deletions
33
src/main/java/com/example/epari/exam/controller/StudentExamController.java
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -4,8 +4,8 @@ | |
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.example.epari.global.common.base.BaseTimeEntity; | ||
import com.example.epari.course.domain.Course; | ||
import com.example.epari.global.common.base.BaseTimeEntity; | ||
|
||
import jakarta.persistence.CascadeType; | ||
import jakarta.persistence.Column; | ||
|
@@ -59,8 +59,8 @@ public class Exam extends BaseTimeEntity { | |
private List<ExamQuestion> questions = new ArrayList<>(); | ||
|
||
@Builder | ||
private Exam(String title, LocalDateTime examDateTime, Integer duration, | ||
Integer totalScore, String description, Course course) { | ||
private Exam(String title, LocalDateTime examDateTime, | ||
Integer duration, Integer totalScore, String description, Course course) { | ||
this.title = title; | ||
this.examDateTime = examDateTime; | ||
this.duration = duration; | ||
|
@@ -83,4 +83,42 @@ public void addQuestion(ExamQuestion question) { | |
question.setExam(this); | ||
} | ||
|
||
public void reorderQuestions(Long questionId, int newNumber) { | ||
// 1. 이동할 문제 찾기 | ||
ExamQuestion targetQuestion = questions.stream() | ||
.filter(q -> q.getId().equals(questionId)) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException("문제를 찾을 수 없습니다. ID: " + questionId)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 추후 커스텀 예외를 생성하여 예외 처리를 일관성 있게 유지해주시면 좋겠습니다! |
||
|
||
// 2. 현재 문제 번호 | ||
int currentNumber = targetQuestion.getExamNumber(); | ||
|
||
// 3. 유효성 검사 | ||
if (newNumber < 1 || newNumber > questions.size()) { | ||
throw new IllegalArgumentException("유효하지 않은 문제 번호입니다: " + newNumber); | ||
} | ||
|
||
// 4. 문제 번호 재정렬 | ||
if (currentNumber < newNumber) { | ||
// 현재 위치에서 뒤로 이동하는 경우 | ||
questions.stream() | ||
.filter(q -> q.getExamNumber() > currentNumber && q.getExamNumber() <= newNumber) | ||
.forEach(q -> q.updateExamNumber(q.getExamNumber() - 1)); | ||
} else if (currentNumber > newNumber) { | ||
// 현재 위치에서 앞으로 이동하는 경우 | ||
questions.stream() | ||
.filter(q -> q.getExamNumber() >= newNumber && q.getExamNumber() < currentNumber) | ||
.forEach(q -> q.updateExamNumber(q.getExamNumber() + 1)); | ||
} | ||
|
||
// 5. 대상 문제의 번호 업데이트 | ||
targetQuestion.updateExamNumber(newNumber); | ||
} | ||
|
||
public void reorderQuestionsAfterDelete(int deletedNumber) { | ||
questions.stream() | ||
.filter(q -> q.getExamNumber() > deletedNumber) | ||
.forEach(q -> q.updateExamNumber(q.getExamNumber() - 1)); | ||
} | ||
|
||
} |
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
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.
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.
역할을 추출하는 코드가 중복되고 있네요!
만약 역할을 추출해야하는 상황이 반복된다면, 커스텀 어노테이션을 정의해보시는 것도 좋은 방법일 것 같습니다!
@CurrentUserEmail
과 같은 방식으로요!