diff --git a/src/main/java/gdsc/binaryho/imhere/core/lecture/application/LectureService.java b/src/main/java/gdsc/binaryho/imhere/core/lecture/application/LectureService.java index 39cd763..d71cd54 100644 --- a/src/main/java/gdsc/binaryho/imhere/core/lecture/application/LectureService.java +++ b/src/main/java/gdsc/binaryho/imhere/core/lecture/application/LectureService.java @@ -5,6 +5,7 @@ import gdsc.binaryho.imhere.core.enrollment.infrastructure.EnrollmentInfoRepository; import gdsc.binaryho.imhere.core.lecture.Lecture; import gdsc.binaryho.imhere.core.lecture.LectureState; +import gdsc.binaryho.imhere.core.lecture.application.port.AttendeeCacheRepository; import gdsc.binaryho.imhere.core.lecture.application.port.OpenLectureCacheRepository; import gdsc.binaryho.imhere.core.lecture.exception.LectureNotFoundException; import gdsc.binaryho.imhere.core.lecture.infrastructure.LectureRepository; @@ -16,6 +17,8 @@ import gdsc.binaryho.imhere.core.member.Member; import gdsc.binaryho.imhere.security.util.AuthenticationHelper; import java.util.List; +import java.util.Optional; +import java.util.Set; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import lombok.extern.log4j.Log4j2; @@ -35,6 +38,7 @@ public class LectureService { private final LectureRepository lectureRepository; private final EnrollmentInfoRepository enrollmentInfoRepository; private final OpenLectureCacheRepository openLectureCacheRepository; + private final AttendeeCacheRepository attendeeCacheRepository; private final ApplicationEventPublisher eventPublisher; @@ -64,11 +68,30 @@ private List findStudentLectures(Member currentStudent) { @Transactional(readOnly = true) public LectureResponse getStudentOpenLectures() { Member currentStudent = authenticationHelper.getCurrentMember(); - // TODO : 캐시 먼저 확인 추가 + + List openLectures = findCachedOpenLectures(currentStudent.getId()); + + if (NotEmpty(openLectures)) { + return LectureResponse.from(openLectures); + } + List studentOpenLectures = lectureRepository.findOpenAndApprovalLecturesByMemberId(currentStudent.getId()); return LectureResponse.createLectureResponseFromLectures(studentOpenLectures); } + private boolean NotEmpty(List openLectures) { + return openLectures.size() > 0; + } + + private List findCachedOpenLectures(Long studentId) { + Set lectureIds = attendeeCacheRepository.findLectureIds(studentId); + return lectureIds.stream() + .map(openLectureCacheRepository::findByLectureId) + .filter(Optional::isPresent) + .map(Optional::get) + .collect(Collectors.toList()); + } + @Transactional(readOnly = true) public LectureResponse getOwnedLectures() { Member currentLecturer = authenticationHelper.getCurrentMember(); diff --git a/src/main/java/gdsc/binaryho/imhere/core/lecture/model/response/LectureResponse.java b/src/main/java/gdsc/binaryho/imhere/core/lecture/model/response/LectureResponse.java index dced692..d145e74 100644 --- a/src/main/java/gdsc/binaryho/imhere/core/lecture/model/response/LectureResponse.java +++ b/src/main/java/gdsc/binaryho/imhere/core/lecture/model/response/LectureResponse.java @@ -3,11 +3,13 @@ import gdsc.binaryho.imhere.core.enrollment.EnrollmentInfo; import gdsc.binaryho.imhere.core.lecture.Lecture; import gdsc.binaryho.imhere.core.lecture.LectureState; +import gdsc.binaryho.imhere.core.lecture.model.OpenLecture; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.tags.Tag; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; +import lombok.AllArgsConstructor; import lombok.Getter; @Getter @@ -34,8 +36,17 @@ public static LectureResponse createLectureResponseFromEnrollmentInfos( return new LectureResponse(lectureInfos); } + public static LectureResponse from(List openLectures) { + List lectureInfos = openLectures.stream() + .map(openLecture -> new LectureInfo(openLecture.getId(), openLecture.getName(), + openLecture.getLecturerName(), LectureState.OPEN, new ArrayList<>())) + .collect(Collectors.toList()); + return new LectureResponse(lectureInfos); + } + @Getter @Tag(name = "LectureDto", description = "수업 정보") + @AllArgsConstructor public static class LectureInfo { private final Long lectureId;