Skip to content

Commit

Permalink
BE: [fix] 세션 없이 시험 리스트 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
HOJEONGKIMM committed Nov 3, 2024
1 parent 5ed3f78 commit ea262bd
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,72 @@ public class ExamController {
@Autowired
private ExamService examService;

// // "before" 상태의 Exam 리스트 조회
// @GetMapping("/before")
// public ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getBeforeExams(HttpSession session) {
// return getExamsByStatus("BEFORE", session);
// }
//
// // "in-progress" 상태의 Exam 리스트 조회
// @GetMapping("/in-progress")
// public ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getInProgressExams(HttpSession session) {
// return getExamsByStatus("IN_PROGRESS", session);
// }
//
// // "done" 상태의 Exam 리스트 조회
// @GetMapping("/done")
// public ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getDoneExams(HttpSession session) {
// return getExamsByStatus("DONE", session);
// }

// 공통 메서드: 상태별 Exam 조회
// private ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getExamsByStatus(String status, HttpSession session) {
// Integer adminId = (Integer) session.getAttribute("adminId");
// if (adminId == null) {
// throw new BaseException(BaseResponseCode.UNAUTHORIZED);
// }
//
// ExamStatus examStatus = ExamStatus.fromString(status);
// if (examStatus == null) {
// throw new BaseException(BaseResponseCode.INVALID_STATUS);
// }
//
// List<ExamResponseDTO> examList = examService.getExamsByStatus(adminId, examStatus);
// return ResponseEntity.ok(new BaseResponse<>(examList));
// }

// "before" 상태의 Exam 리스트 조회
@GetMapping("/before")
public ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getBeforeExams(HttpSession session) {
return getExamsByStatus("BEFORE", session);
public ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getBeforeExams() {
return getExamsByStatus("BEFORE");
}

// "in-progress" 상태의 Exam 리스트 조회
@GetMapping("/in-progress")
public ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getInProgressExams(HttpSession session) {
return getExamsByStatus("IN_PROGRESS", session);
public ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getInProgressExams() {
return getExamsByStatus("IN_PROGRESS");
}

// "done" 상태의 Exam 리스트 조회
@GetMapping("/done")
public ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getDoneExams(HttpSession session) {
return getExamsByStatus("DONE", session);
public ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getDoneExams() {
return getExamsByStatus("DONE");
}

// 공통 메서드: 상태별 Exam 조회
private ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getExamsByStatus(String status, HttpSession session) {
Integer adminId = (Integer) session.getAttribute("adminId");
if (adminId == null) {
throw new BaseException(BaseResponseCode.UNAUTHORIZED);
}

// 공통 메서드: 상태별 Exam 조회 (세션 검증 없이)
private ResponseEntity<BaseResponse<List<ExamResponseDTO>>> getExamsByStatus(String status) {
ExamStatus examStatus = ExamStatus.fromString(status);
if (examStatus == null) {
throw new BaseException(BaseResponseCode.INVALID_STATUS);
}

List<ExamResponseDTO> examList = examService.getExamsByStatus(adminId, examStatus);
// adminId를 사용하지 않는 조회 방식으로 수정
List<ExamResponseDTO> examList = examService.getExamsByStatus(null, examStatus);
return ResponseEntity.ok(new BaseResponse<>(examList));
}



// ExamCode로 특정 Exam 조회 (POST 요청)
@PostMapping("/{examId}/code")
public ResponseEntity<BaseResponse<ExamResponseDTO>> getExamByCode(@PathVariable Integer examId, @RequestBody ExamCodeRequestDTO examCodeRequestDTO, HttpSession session) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ public interface ExamRepository extends JpaRepository<Exam, Integer> {

// 랜덤 코드와 adminId로 Exam 조회
Exam findByExamRandomCode(String examRandomCode);


// adminId 없이 상태로만 Exam 조회
List<Exam> findByExamStatus(ExamStatus examStatus);
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,66 @@ public boolean existsById(Integer examId) {
}

// Admin ID와 ExamStatus로 시험 목록 조회
// public List<ExamResponseDTO> getExamsByStatus(Integer adminId, ExamStatus examStatus) {
// return examRepository.findByAdmin_AdminIdAndExamStatus(adminId, examStatus).stream()
// .map(exam -> new ExamResponseDTO(
// exam.getExamId(),
// exam.getExamName(),
// exam.getExamSemester(),
// exam.getExamStudentNumber(),
// exam.getExamLocation(),
// exam.getExamDate(),
// exam.getExamStartTime(),
// exam.getExamDuration(),
// exam.getExamStatus(),
// exam.getExamNotice(),
// exam.getSession() != null ? exam.getSession().getSessionId() : null,
// exam.getExamRandomCode()
// ))
// .collect(Collectors.toList());
// }

// Admin ID 없이 ExamStatus로만 시험 목록 조회
public List<ExamResponseDTO> getExamsByStatus(Integer adminId, ExamStatus examStatus) {
return examRepository.findByAdmin_AdminIdAndExamStatus(adminId, examStatus).stream()
.map(exam -> new ExamResponseDTO(
exam.getExamId(),
exam.getExamName(),
exam.getExamSemester(),
exam.getExamStudentNumber(),
exam.getExamLocation(),
exam.getExamDate(),
exam.getExamStartTime(),
exam.getExamDuration(),
exam.getExamStatus(),
exam.getExamNotice(),
exam.getSession() != null ? exam.getSession().getSessionId() : null,
exam.getExamRandomCode()
))
.collect(Collectors.toList());
if (adminId == null) {
return examRepository.findByExamStatus(examStatus).stream()
.map(exam -> new ExamResponseDTO(
exam.getExamId(),
exam.getExamName(),
exam.getExamSemester(),
exam.getExamStudentNumber(),
exam.getExamLocation(),
exam.getExamDate(),
exam.getExamStartTime(),
exam.getExamDuration(),
exam.getExamStatus(),
exam.getExamNotice(),
exam.getSession() != null ? exam.getSession().getSessionId() : null,
exam.getExamRandomCode()
))
.collect(Collectors.toList());
} else {
return examRepository.findByAdmin_AdminIdAndExamStatus(adminId, examStatus).stream()
.map(exam -> new ExamResponseDTO(
exam.getExamId(),
exam.getExamName(),
exam.getExamSemester(),
exam.getExamStudentNumber(),
exam.getExamLocation(),
exam.getExamDate(),
exam.getExamStartTime(),
exam.getExamDuration(),
exam.getExamStatus(),
exam.getExamNotice(),
exam.getSession() != null ? exam.getSession().getSessionId() : null,
exam.getExamRandomCode()
))
.collect(Collectors.toList());
}
}



// ExamCode로 시험 조회
public ExamResponseDTO getExamByCode(String examCode) {
Exam exam = examRepository.findByExamRandomCode(examCode);
Expand Down

0 comments on commit ea262bd

Please sign in to comment.