Skip to content
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] 수강중인 전체 학생 정보 조회 #126

Merged
merged 5 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.example.epari.assignment.dto.submission.SubmissionRequestDto;
import com.example.epari.assignment.dto.submission.SubmissionResponseDto;
import com.example.epari.assignment.service.SubmissionService;
import com.example.epari.course.repository.CourseRepository;
import com.example.epari.global.annotation.CurrentUserEmail;
import com.example.epari.user.domain.Student;
import com.example.epari.user.repository.StudentRepository;
Expand All @@ -22,7 +23,9 @@ public class SubmissionController {

private final SubmissionService submissionService;

private final StudentRepository studentRepository; // 추가
private final StudentRepository studentRepository;

private final CourseRepository courseRepository;

@PreAuthorize("hasRole('STUDENT')")
@PostMapping
Expand Down Expand Up @@ -54,8 +57,15 @@ public ResponseEntity<SubmissionResponseDto> getSubmission(
@GetMapping("/list")
public ResponseEntity<List<SubmissionResponseDto>> getAllSubmissions(
@PathVariable Long courseId,
@PathVariable Long assignmentId) {
List<SubmissionResponseDto> submissions = submissionService.getSubmissionsByAssignmentId(assignmentId);
@PathVariable Long assignmentId,
@CurrentUserEmail String email) { // 강사 권한 검증을 위해 추가

// 강사가 해당 강의의 담당자인지 확인
if (!courseRepository.existsByCourseIdAndInstructorEmail(courseId, email)) {
throw new IllegalArgumentException("해당 강의의 조회 권한이 없습니다.");
}

List<SubmissionResponseDto> submissions = submissionService.getSubmissionsWithStudents(courseId, assignmentId);
return ResponseEntity.ok(submissions);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,15 @@ public static SubmissionResponseDto from(Submission submission) {
.build();
}

public static SubmissionResponseDto createUnsubmitted(Student student, com.example.epari.assignment.domain.Assignment assignment) {
return SubmissionResponseDto.builder()
.description("")
.grade(null)
.feedback(null)
.status("미제출")
.files(List.of())
.student(StudentInfo.from(student))
.assignment(AssignmentInfo.from(assignment))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ public interface SubmissionRepository extends JpaRepository<Submission, Long> {
"JOIN a.course c " +
"WHERE c.id = :courseId")
List<Submission> findByCourseId(Long courseId);

@Query("SELECT s FROM Submission s " +
"LEFT JOIN FETCH s.student " +
"JOIN FETCH s.assignment a " +
"JOIN FETCH a.course c " +
"WHERE a.id = :assignmentId " +
"AND c.id = :courseId")
List<Submission> findByAssignmentIdAndCourseIdWithStudent(Long assignmentId, Long courseId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,40 @@ public void deleteSubmission(Long courseId, Long assignmentId, Long submissionId
submissionRepository.delete(submission);
}

/**
* 전체 학생 조회
*/
public List<SubmissionResponseDto> getSubmissionsWithStudents(Long courseId, Long assignmentId) {
// 과제 정보 조회
Assignment assignment = assignmentRepository.findById(assignmentId)
.orElseThrow(() -> new IllegalArgumentException("과제를 찾을 수 없습니다."));

// 과제가 해당 코스의 것인지 확인
if (!assignment.getCourse().getId().equals(courseId)) {
throw new IllegalArgumentException("해당 코스의 과제가 아닙니다.");
}

// 제출된 과제 목록 조회
List<Submission> submissions = submissionRepository
.findByAssignmentIdAndCourseIdWithStudent(assignmentId, courseId);

// 해당 코스의 모든 학생 목록 조회
List<Student> enrolledStudents = studentRepository.findByCourseId(courseId);

// 제출/미제출 학생 모두 포함한 응답 DTO 생성
return enrolledStudents.stream()
.map(student -> {
Optional<Submission> studentSubmission = submissions.stream()
.filter(sub -> sub.getStudent().getId().equals(student.getId()))
.findFirst();

return studentSubmission
.map(SubmissionResponseDto::from)
.orElse(SubmissionResponseDto.createUnsubmitted(student, assignment));
})
.collect(Collectors.toList());
}

/**
* 파일 다운로드
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.example.epari.user.repository;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

import com.example.epari.user.domain.Student;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Optional;

/**
Expand All @@ -14,4 +18,7 @@
public interface StudentRepository extends JpaRepository<Student, Long> {

Optional<Student> findByEmail(String email);

@Query("SELECT cs.student FROM CourseStudent cs WHERE cs.course.id = :courseId")
List<Student> findByCourseId(@Param("courseId") Long courseId);
}
Loading