From c36e444d9c548ad835f008738f876f9666fd0d7f Mon Sep 17 00:00:00 2001 From: kgc91747 Date: Thu, 1 Aug 2024 17:01:00 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20Lecture=20=EA=B0=95=EC=9D=98=20?= =?UTF-8?q?=EC=83=81=EC=84=B8=EC=A1=B0=ED=9A=8C=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lecture/service/LectureService.java | 2 + .../lecture/service/LectureServiceImpl.java | 81 ++++++++++--------- 2 files changed, 44 insertions(+), 39 deletions(-) diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureService.java b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureService.java index 54d0d3e..fac837d 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureService.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureService.java @@ -21,6 +21,8 @@ public interface LectureService { List findAllLecture(); + String getUserStatus(Long userId, Lecture lecture); + LectureDetailResponse findLectureById(Long userId, long lectureId); List findMyLecture(long userId); diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java index 7c202eb..58958f9 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java @@ -134,55 +134,58 @@ public class LectureServiceImpl implements LectureService { } @Override - public LectureDetailResponse findLectureById(Long userId, long lectureId) { - Optional lecture = lectureRepository.findById(lectureId); - if (lecture.isEmpty()) { - return null; - } - - String userStatus; + public String getUserStatus(Long userId, Lecture lecture) { if (userId == null) { - userStatus = String.valueOf(UserStatus.NOT_ENROLLED); - } else { - User user = userRepository.findById(userId).get(); - - if (user.getRole() == UserRole.ADMIN) { - if (lecture.get().getUser().getId() == user.getId()) { - userStatus = String.valueOf(UserStatus.MANAGED_BY_ME); - } else { - userStatus = String.valueOf(UserStatus.MANAGED_BY_OTHERS); - } - } else { - Registration registration = registrationRepository.findByUserIdAndLectureId(userId, lectureId); - - if (registration == null) { - userStatus = String.valueOf(UserStatus.NOT_ENROLLED); - } else if (registration.getStatus() == RegistrationStatus.ACCEPTED) { - userStatus = String.valueOf(UserStatus.ENROLLED); - } else { - userStatus = String.valueOf(UserStatus.PENDING); - } - } + return String.valueOf(UserStatus.NOT_ENROLLED); } + User user = userRepository.findById(userId).orElseThrow(NoSuchElementException::new); + UserRole userRole = user.getRole(); + + if (userRole == UserRole.ADMIN) { + if (lecture.getUser() == user) { + return String.valueOf(UserStatus.MANAGED_BY_ME); + } + return String.valueOf(UserStatus.MANAGED_BY_OTHERS); + } + + Registration registration = registrationRepository.findByUserIdAndLectureId(userId, lecture.getId()); + + if (registration == null) { + return String.valueOf(UserStatus.NOT_ENROLLED); + } + + if (registration.getStatus() == RegistrationStatus.ACCEPTED) { + return String.valueOf(UserStatus.ENROLLED); + } + + return String.valueOf(UserStatus.PENDING); + } + + @Override + public LectureDetailResponse findLectureById(Long userId, long lectureId) { + Lecture lecture = lectureRepository.findById(lectureId).orElseThrow(NoSuchElementException::new); + + String userStatus = getUserStatus(userId, lecture); + return LectureDetailResponse.builder() - .id(lecture.get().getId()) - .title(lecture.get().getTitle()) - .description(lecture.get().getDescription()) - .plan(lecture.get().getPlan()) - .image(lecture.get().getImage()) - .startDate(lecture.get().getStartDate()) - .endDate(lecture.get().getEndDate()) - .time(lecture.get().getTime()) - .online(lecture.get().isOnline()) - .teacherName(lecture.get().getUser().getName()) + .id(lecture.getId()) + .title(lecture.getTitle()) + .description(lecture.getDescription()) + .plan(lecture.getPlan()) + .image(lecture.getImage()) + .startDate(lecture.getStartDate()) + .endDate(lecture.getEndDate()) + .time(lecture.getTime()) + .online(lecture.isOnline()) + .teacherName(lecture.getUser().getName()) .status(userStatus) .build(); } public List findMyLecture(long userId) { - User user = userRepository.findById(userId).get(); + User user = userRepository.findById(userId).orElseThrow(NoSuchElementException::new); List myLectureList = new ArrayList<>();