diff --git a/src/main/java/com/gdschongik/gdsc/domain/study/application/MentorStudyService.java b/src/main/java/com/gdschongik/gdsc/domain/study/application/MentorStudyService.java index 03927c1c7..0988720a0 100644 --- a/src/main/java/com/gdschongik/gdsc/domain/study/application/MentorStudyService.java +++ b/src/main/java/com/gdschongik/gdsc/domain/study/application/MentorStudyService.java @@ -1,6 +1,7 @@ package com.gdschongik.gdsc.domain.study.application; -import static com.gdschongik.gdsc.global.exception.ErrorCode.STUDY_NOT_FOUND; +import static com.gdschongik.gdsc.global.exception.ErrorCode.*; +import static java.util.stream.Collectors.*; import com.gdschongik.gdsc.domain.member.domain.Member; import com.gdschongik.gdsc.domain.study.dao.AssignmentHistoryRepository; @@ -26,8 +27,8 @@ import java.time.LocalDate; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Set; -import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; @@ -70,33 +71,30 @@ public Page getStudyStudents(Long studyId, Pageable pageab List studentIds = studyHistories.getContent().stream() .map(studyHistory -> studyHistory.getStudent().getId()) .toList(); - List studyAchievements = studyAchievementRepository.findByStudyIdAndMemberIds(studyId, studentIds); List attendances = attendanceRepository.findByStudyIdAndMemberIds(studyId, studentIds); List assignmentHistories = assignmentHistoryRepository.findByStudyIdAndMemberIds(studyId, studentIds); + // StudyAchievement, Attendance, AssignmentHistory에 대해 Member의 id를 key로 하는 Map 생성 + Map> studyAchievementMap = studyAchievements.stream() + .collect(groupingBy( + studyAchievement -> studyAchievement.getStudent().getId())); + Map> attendanceMap = attendances.stream() + .collect(groupingBy(attendance -> attendance.getStudent().getId())); + Map> assignmentHistoryMap = assignmentHistories.stream() + .collect(groupingBy( + assignmentHistory -> assignmentHistory.getMember().getId())); + List response = new ArrayList<>(); studyHistories.getContent().forEach(studyHistory -> { - List currentStudyAchievements = studyAchievements.stream() - .filter(studyAchievement -> studyAchievement - .getStudent() - .getId() - .equals(studyHistory.getStudent().getId())) - .toList(); - List currentAttendances = attendances.stream() - .filter(attendance -> attendance - .getStudent() - .getId() - .equals(studyHistory.getStudent().getId())) - .toList(); - List currentAssignmentHistories = assignmentHistories.stream() - .filter(assignmentHistory -> assignmentHistory - .getMember() - .getId() - .equals(studyHistory.getStudent().getId())) - .toList(); + List currentStudyAchievements = + studyAchievementMap.getOrDefault(studyHistory.getStudent().getId(), new ArrayList<>()); + List currentAttendances = + attendanceMap.getOrDefault(studyHistory.getStudent().getId(), new ArrayList<>()); + List currentAssignmentHistories = + assignmentHistoryMap.getOrDefault(studyHistory.getStudent().getId(), new ArrayList<>()); List studyTodos = new ArrayList<>(); studyDetails.forEach(studyDetail -> { @@ -175,12 +173,12 @@ public void updateStudy(Long studyId, StudyUpdateRequest request) { List studyDetails = studyDetailRepository.findAllByStudyIdOrderByWeekAsc(studyId); // StudyDetail ID를 추출하여 Set으로 저장 - Set studyDetailIds = studyDetails.stream().map(StudyDetail::getId).collect(Collectors.toSet()); + Set studyDetailIds = studyDetails.stream().map(StudyDetail::getId).collect(toSet()); // 요청된 StudyCurriculumCreateRequest의 StudyDetail ID를 추출하여 Set으로 저장 Set requestIds = request.studyCurriculums().stream() .map(StudyCurriculumCreateRequest::studyDetailId) - .collect(Collectors.toSet()); + .collect(toSet()); studyDetailValidator.validateUpdateStudyDetail(studyDetailIds, requestIds);