Skip to content

Commit

Permalink
feat : 학생의 수강신청을 승인할 때, 이미 열려있는 강의라면, 학생 정보를 캐싱하는 기능 구현 (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
binary-ho committed Aug 22, 2023
1 parent b4d64c9 commit a76c54f
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@
import gdsc.binaryho.imhere.core.enrollment.infrastructure.EnrollmentInfoRepository;
import gdsc.binaryho.imhere.core.enrollment.model.response.EnrollmentInfoResponse;
import gdsc.binaryho.imhere.core.lecture.Lecture;
import gdsc.binaryho.imhere.core.lecture.application.AttendeeCacheEvent;
import gdsc.binaryho.imhere.core.lecture.application.OpenLectureService;
import gdsc.binaryho.imhere.core.lecture.exception.LectureNotFoundException;
import gdsc.binaryho.imhere.core.lecture.infrastructure.LectureRepository;
import gdsc.binaryho.imhere.core.lecture.model.OpenLecture;
import gdsc.binaryho.imhere.core.lecture.model.StudentIds;
import gdsc.binaryho.imhere.core.member.Member;
import gdsc.binaryho.imhere.security.util.AuthenticationHelper;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -25,8 +30,10 @@
public class EnrollmentService {

private final AuthenticationHelper authenticationHelper;
private final OpenLectureService openLectureService;
private final LectureRepository lectureRepository;
private final EnrollmentInfoRepository enrollmentInfoRepository;
private final ApplicationEventPublisher eventPublisher;

@Transactional
public void requestEnrollment(Long lectureId) {
Expand All @@ -48,11 +55,23 @@ public void approveStudents(Long lectureId, Long studentId) {

enrollmentInfo.setEnrollmentState(EnrollmentState.APPROVAL);

cacheStudentIfLectureIsOpen(lectureId, enrollmentInfo);

log.info("[수강신청 승인] 강의 : {} ({}) 학생 : {} ({})"
, () -> enrollmentInfo.getLecture().getLectureName(), () -> enrollmentInfo.getLecture().getLecturerName()
, () -> enrollmentInfo.getMember().getUnivId(), () -> enrollmentInfo.getMember().getName());
}

private void cacheStudentIfLectureIsOpen(Long lectureId, EnrollmentInfo enrollmentInfo) {
Optional<OpenLecture> lecture = openLectureService.find(lectureId);

if (lecture.isPresent()) {
Long studentId = enrollmentInfo.getMember().getId();

eventPublisher.publishEvent(new AttendeeCacheEvent(lectureId, new StudentIds(studentId)));
}
}

private EnrollmentInfo getEnrollmentInfo(Long lectureId, Long studentId) {
return enrollmentInfoRepository
.findByMemberIdAndLectureId(studentId, lectureId)
Expand Down

0 comments on commit a76c54f

Please # to comment.