feat: 강의 만들기/입장 로직 추가

This commit is contained in:
박정민 2024-08-05 10:19:53 +09:00
parent ec839ab8f0
commit 2636833458
5 changed files with 124 additions and 143 deletions

View File

@ -235,8 +235,14 @@ public class LectureServiceImpl implements LectureService {
Lecture l;
l = lecture.get();
if(l.isOnline())
{
l.setOnline(false);
}
else
{
l.setOnline(true);
}
lectureRepository.save(l);

View File

@ -43,6 +43,7 @@ public class Controller {
private final RegistrationService registrationService;
private final UserRepository userRepository;
private final LectureRepository lectureRepository;
@Value("${livekit.api.key}")
private String LIVEKIT_API_KEY;
@ -50,53 +51,15 @@ public class Controller {
private String LIVEKIT_API_SECRET;
private static final ObjectMapper objectMapper = new ObjectMapper();
/**
* @param params JSON object with roomName and participantName
* @return JSON object with the JWT token
*/
// @PostMapping(value = "/token")
// public ResponseEntity<Map<String, String>> createToken(@RequestBody Map<String, String> params) {
// String roomName = params.get("roomName");
// String participantName = params.get("participantName");
//
// if (roomName == null || participantName == null) {
// return ResponseEntity.badRequest().body(Map.of("errorMessage", "roomName and participantName are required"));
// }
//
// AccessToken token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET);
// token.setName(participantName);
// token.setIdentity(participantName);
// token.addGrants(new RoomJoin(true), new RoomName(roomName));
//
//
//
// return ResponseEntity.ok(Map.of("token", token.toJwt()));
// }
@PostMapping(value = "/makeroom/{lecture_id}")
public ResponseEntity<String> startLecture(@PathVariable("lecture_id") Long id, HttpServletRequest request) throws Exception {
String userToken = request.getHeader("Authorization");
Long userId = Long.parseLong(jwtUtil.getUserId(userToken));
videoSertvice.startOnline(userId, id);
return new ResponseEntity<>("방만들기 성공",HttpStatus.OK);
}
public static String serializeIdentityData(IdentityData identityData) throws JsonProcessingException, JsonProcessingException {
return objectMapper.writeValueAsString(identityData);
}
@PostMapping(value = "/joinroom/{lecture_id}")
public ResponseEntity<Map<String, String>> joinRoom(@PathVariable("lecture_id") Long id, HttpServletRequest request) throws Exception
{
public ResponseEntity<Map<String, String>> joinRoom(@PathVariable("lecture_id") Long id, HttpServletRequest request) throws Exception {
// 조인할떄 고려해야할점
// 1. 신청됀 강의인지
// 2. 강의기 On인지
String userToken = request.getHeader("Authorization");
@ -105,12 +68,50 @@ public class Controller {
Lecture lecture = lectureRepository.findById(id).orElse(null);
Random random = new Random();
String roomName = lecture.getTitle();
String participantName = userService.getUserName(userId);
System.out.println(participantName);
System.out.println();
int randomNumber = 10000 + random.nextInt(80000);
AccessToken token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET);
if (findUser.getRole() == UserRole.STUDENT) {
if (videoSertvice.isRoomAccessible(id, userId)) {
IdentityData identityData = new IdentityData(participantName, "학생");
String jsonIdentity = serializeIdentityData(identityData);
token.setIdentity(jsonIdentity);
token.setName(participantName);
token.addGrants(new RoomJoin(true), new RoomName(roomName));
return ResponseEntity.ok(Map.of("token", token.toJwt()));
} else {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(Map.of("error", "방에 들어갈 수 없습니다."));
}
}
return ResponseEntity.ok(Map.of("token", null));
}
@PostMapping(value = "/makeroom/{lecture_id}")
public ResponseEntity<Map<String, String>> makeRoom(@PathVariable("lecture_id") Long id, HttpServletRequest request) throws Exception {
String userToken = request.getHeader("Authorization");
Long userId = Long.parseLong(jwtUtil.getUserId(userToken));
User findUser = userRepository.findById(userId).orElse(null);
Lecture lecture = lectureRepository.findById(id).orElse(null);
String randStr = String.valueOf(randomNumber);
String roomName = lecture.getTitle();
String participantName = userService.getUserName(userId);
@ -122,80 +123,25 @@ public class Controller {
if (findUser.getRole() == UserRole.ADMIN) {//&& lecture.isOnline() ) {
videoSertvice.startOnline(userId, id);
IdentityData identityData = new IdentityData(participantName, "강사");
String jsonIdentity = serializeIdentityData(identityData);
token.setIdentity(jsonIdentity);
token.setName(participantName);
token.addGrants(new RoomJoin(true), new RoomName(roomName), new RoomCreate(true));
videoSertvice.startOnline(userId, id);
System.out.println();
return ResponseEntity.ok(Map.of("token", token.toJwt()));
}
else if(findUser.getRole()==UserRole.STUDENT )// && lecture.isOnline() )
{
IdentityData identityData = new IdentityData(participantName, "학생");
String jsonIdentity = serializeIdentityData(identityData);
token.setIdentity(jsonIdentity);
token.setName(participantName);
token.addGrants(new RoomJoin(true), new RoomName(roomName));
videoSertvice.startOnline(userId, id);
return ResponseEntity.ok(Map.of("token", token.toJwt()));
}
return ResponseEntity.ok(Map.of("token", null));
// String userToken = request.getHeader("Authorization");
//
// Long userId = Long.parseLong(jwtUtil.getUserId(userToken));
// LectureDetailResponse lecture= lectureService.findLectureById(userId,id);
//
//
// //RegistrationStatus registrationStatus = registrationService.isOnline(userId,id);
//
// if(registrationStatus==RegistrationStatus.ACCEPTED)
// {
// String roomName = lecture.getTitle();
// String participantName = userService.getUserName(userId);
//
//
// AccessToken token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET);
// token.setName(participantName);
// token.setIdentity(participantName);
// token.addGrants(new RoomJoin(true), new RoomName(roomName));
//
// //videoSertvice.startOnline(userId,id);
//
//
//
// return ResponseEntity.ok(Map.of("token", token.toJwt()));
// }
// else{
// return ResponseEntity.status(HttpStatus.FORBIDDEN).body(Map.of("errorMessage", "Not accepted"));
//
// }
}
@PostMapping(value = "/livekit/webhook", consumes = "application/webhook+json")
@ -207,9 +153,10 @@ public class Controller {
} catch (Exception e) {
System.err.println("Error validating webhook event: " + e.getMessage());
}
return ResponseEntity.ok("ok");
}
}

View File

@ -5,4 +5,7 @@ import java.sql.SQLException;
public interface VideoSertvice {
void startOnline(Long userId,Long lectureId) throws SQLException;
boolean isRoomAccessible(Long lectureId,Long userId);
}

View File

@ -1,7 +1,10 @@
package com.edufocus.edufocus.video.service;
import com.edufocus.edufocus.lecture.entity.Lecture;
import com.edufocus.edufocus.lecture.entity.LectureDetailResponse;
import com.edufocus.edufocus.lecture.service.LectureService;
import com.edufocus.edufocus.registration.entity.RegistrationStatus;
import com.edufocus.edufocus.registration.service.RegistrationService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@ -13,9 +16,31 @@ import java.sql.SQLException;
public class VideoServiceImpl implements VideoSertvice{
private final LectureService lectureService;
private final RegistrationService registrationService;
@Override
public void startOnline(Long userId,Long lectureId) throws SQLException {
lectureService.changeState(lectureId);
}
@Override
public boolean isRoomAccessible(Long lectureId, Long userId) {
LectureDetailResponse lecture= lectureService.findLectureById(userId,userId);
RegistrationStatus registrationStatus = registrationService.getStatus(userId,lectureId);
if(registrationStatus==RegistrationStatus.ACCEPTED && lecture.isOnline())
{
return true;
}
else
{
return false;
}
}
}

View File

@ -21,7 +21,7 @@ jwt.salt=${SALT}
jwt.access-token.expiretime=3000000
# Refresh Token ?? ?? (??? ??)
jwt.refresh-token.expiretime=50400000
jwt.refresh-token.expiretime=504000000
#jwt.refresh-token.expiretime=4000
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=${DATA_SOURCE_URL}