feat: Lecture 강의 상세조회 수정

강의 상세조회시 유저정보를 확인해 수강중인지 여부를 추가로 전달
This commit is contained in:
kgc9007 2024-07-19 15:57:48 +09:00
parent be1d1293ed
commit f3859e6a65
6 changed files with 53 additions and 9 deletions

View File

@ -67,13 +67,21 @@ public class LectureController {
}
@GetMapping("/{lectureId}")
public ResponseEntity<?> findById(@PathVariable long lectureId) {
LectureDetailResponse lectureDetailResponse = lectureService.findLectureById(lectureId);
public ResponseEntity<?> findById(@RequestHeader(value = "Authorization", required = false) String accessToken, @PathVariable long lectureId) {
Long userId = null;
if (accessToken != null) {
userId = Long.parseLong(jwtUtil.getUserId(accessToken));
}
LectureDetailResponse lectureDetailResponse = lectureService.findLectureById(userId, lectureId);
if (lectureDetailResponse == null) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(lectureDetailResponse, HttpStatus.OK);
}

View File

@ -30,4 +30,6 @@ public class LectureDetailResponse {
private String plan;
private boolean online;
private String status;
}

View File

@ -0,0 +1,9 @@
package com.edufocus.edufocus.lecture.entity;
public enum UserStatus {
MANAGED_BY_ME, // 내가 관리하는 강의 - 강사
MANAGED_BY_OTHERS, // 내가 관리하지 않은 강의 - 강사
ENROLLED, // 내가 수강 중인 강의 - 학생
PENDING, // 내가 수강신청하고 승인 대기 중인 강의 - 학생
NOT_ENROLLED // 내가 수강신청하지 않은 강의 - 학생/비로그인
}

View File

@ -19,7 +19,7 @@ public interface LectureService {
List<LectureSearchResponse> findAllLecture();
LectureDetailResponse findLectureById(long lectureId);
LectureDetailResponse findLectureById(Long userId, long lectureId);
List<LectureSearchResponse> findMyLecture(long userId);

View File

@ -1,11 +1,9 @@
package com.edufocus.edufocus.lecture.service;
import com.edufocus.edufocus.lecture.entity.Lecture;
import com.edufocus.edufocus.lecture.entity.LectureCreateRequest;
import com.edufocus.edufocus.lecture.entity.LectureSearchResponse;
import com.edufocus.edufocus.lecture.entity.LectureDetailResponse;
import com.edufocus.edufocus.lecture.entity.*;
import com.edufocus.edufocus.lecture.repository.LectureRepository;
import com.edufocus.edufocus.registration.entity.Registration;
import com.edufocus.edufocus.registration.entity.RegistrationStatus;
import com.edufocus.edufocus.registration.repository.RegistrationRepository;
import com.edufocus.edufocus.user.model.entity.User;
import com.edufocus.edufocus.user.model.entity.UserRole;
@ -110,14 +108,38 @@ public class LectureServiceImpl implements LectureService {
}
@Override
public LectureDetailResponse findLectureById(long lectureId) {
public LectureDetailResponse findLectureById(Long userId, long lectureId) {
Lecture lecture = lectureRepository.findById(lectureId).get();
if (lecture == null) {
return null;
}
String userStatus;
if (userId == null) {
userStatus = String.valueOf(UserStatus.NOT_ENROLLED);
} else {
User user = userRepository.findById(userId).get();
if (user.getRole() == UserRole.ADMIN) {
if (lecture.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);
}
}
}
LectureDetailResponse lectureDetailResponse = new LectureDetailResponse().builder()
.id(lecture.getId())
.title(lecture.getTitle())
@ -128,6 +150,7 @@ public class LectureServiceImpl implements LectureService {
.plan(lecture.getPlan())
.online(lecture.isOnline())
.teacherName(lecture.getUser().getName())
.status(userStatus)
.build();
return lectureDetailResponse;

View File

@ -10,4 +10,6 @@ import java.util.List;
@Repository
public interface RegistrationRepository extends JpaRepository<Registration, Long> {
List<Registration> findAllByUserId(@Param("userId") Long userId);
Registration findByUserIdAndLectureId(Long userId, Long lectureId);
}