Skip to content

Commit

Permalink
feat : 학생이 열린 수업 정보를 가져오기 전에 캐싱된 데이터가 있는지 먼저 확인하고, 있는 경우 캐싱된 결과를 이용하는…
Browse files Browse the repository at this point in the history
… 기능 구현 (#62)
  • Loading branch information
binary-ho committed Aug 22, 2023
1 parent ae2825d commit 1f5fdaa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -64,11 +68,30 @@ private List<Lecture> findStudentLectures(Member currentStudent) {
@Transactional(readOnly = true)
public LectureResponse getStudentOpenLectures() {
Member currentStudent = authenticationHelper.getCurrentMember();
// TODO : 캐시 먼저 확인 추가

List<OpenLecture> openLectures = findCachedOpenLectures(currentStudent.getId());

if (NotEmpty(openLectures)) {
return LectureResponse.from(openLectures);
}

List<Lecture> studentOpenLectures = lectureRepository.findOpenAndApprovalLecturesByMemberId(currentStudent.getId());
return LectureResponse.createLectureResponseFromLectures(studentOpenLectures);
}

private boolean NotEmpty(List<OpenLecture> openLectures) {
return openLectures.size() > 0;
}

private List<OpenLecture> findCachedOpenLectures(Long studentId) {
Set<Long> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,8 +36,17 @@ public static LectureResponse createLectureResponseFromEnrollmentInfos(
return new LectureResponse(lectureInfos);
}

public static LectureResponse from(List<OpenLecture> openLectures) {
List<LectureInfo> 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;
Expand Down

0 comments on commit 1f5fdaa

Please # to comment.