Merge branch 'BE/userinfo' into 'backend'

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

See merge request s11-webmobile1-sub2/S11P12A701!36
This commit is contained in:
박정민 2024-08-05 13:18:24 +09:00
commit 3baad15fdd
5 changed files with 124 additions and 143 deletions

View File

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

View File

@ -41,6 +41,7 @@ public class Controller {
private final RegistrationService registrationService; private final RegistrationService registrationService;
private final UserRepository userRepository; private final UserRepository userRepository;
private final LectureRepository lectureRepository; private final LectureRepository lectureRepository;
@Value("${livekit.api.key}") @Value("${livekit.api.key}")
private String LIVEKIT_API_KEY; private String LIVEKIT_API_KEY;
@ -48,68 +49,23 @@ public class Controller {
private String LIVEKIT_API_SECRET; private String LIVEKIT_API_SECRET;
private static final ObjectMapper objectMapper = new ObjectMapper(); 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 { public static String serializeIdentityData(IdentityData identityData) throws JsonProcessingException, JsonProcessingException {
return objectMapper.writeValueAsString(identityData); return objectMapper.writeValueAsString(identityData);
} }
@PostMapping(value = "/joinroom/{lecture_id}") @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"); String userToken = request.getHeader("Authorization");
Long userId = Long.parseLong(jwtUtil.getUserId(userToken)); Long userId = Long.parseLong(jwtUtil.getUserId(userToken));
User findUser= userRepository.findById(userId).orElse(null); User findUser = userRepository.findById(userId).orElse(null);
Lecture lecture= lectureRepository.findById(id).orElse(null); Lecture lecture = lectureRepository.findById(id).orElse(null);
Random random = new Random();
System.out.println();
int randomNumber = 10000 + random.nextInt(80000);
String randStr = String.valueOf(randomNumber);
String roomName = lecture.getTitle(); String roomName = lecture.getTitle();
String participantName = userService.getUserName(userId); String participantName = userService.getUserName(userId);
System.out.println(participantName); System.out.println(participantName);
@ -117,82 +73,72 @@ public class Controller {
AccessToken token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET); AccessToken token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET);
if(findUser.getRole()==UserRole.ADMIN ){//&& lecture.isOnline() ) { if (findUser.getRole() == UserRole.STUDENT) {
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() )
{
if (videoSertvice.isRoomAccessible(id, userId)) {
IdentityData identityData = new IdentityData(participantName, "학생"); IdentityData identityData = new IdentityData(participantName, "학생");
String jsonIdentity = serializeIdentityData(identityData); String jsonIdentity = serializeIdentityData(identityData);
token.setIdentity(jsonIdentity); token.setIdentity(jsonIdentity);
token.setName(participantName); token.setName(participantName);
token.addGrants(new RoomJoin(true), new RoomName(roomName)); 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", token.toJwt()));
} else {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(Map.of("error", "방에 들어갈 수 없습니다."));
}
} }
return ResponseEntity.ok(Map.of("token", null)); 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 = "/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 roomName = lecture.getTitle();
String participantName = userService.getUserName(userId);
System.out.println(participantName);
AccessToken token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET);
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));
return ResponseEntity.ok(Map.of("token", token.toJwt()));
}
return ResponseEntity.ok(Map.of("token", null));
} }
@ -205,9 +151,10 @@ public class Controller {
} catch (Exception e) { } catch (Exception e) {
System.err.println("Error validating webhook event: " + e.getMessage()); System.err.println("Error validating webhook event: " + e.getMessage());
} }
return ResponseEntity.ok("ok"); return ResponseEntity.ok("ok");
} }
} }

View File

@ -5,4 +5,7 @@ import java.sql.SQLException;
public interface VideoSertvice { public interface VideoSertvice {
void startOnline(Long userId,Long lectureId) throws SQLException; 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; package com.edufocus.edufocus.video.service;
import com.edufocus.edufocus.lecture.entity.Lecture; 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.lecture.service.LectureService;
import com.edufocus.edufocus.registration.entity.RegistrationStatus;
import com.edufocus.edufocus.registration.service.RegistrationService;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -13,9 +16,31 @@ import java.sql.SQLException;
public class VideoServiceImpl implements VideoSertvice{ public class VideoServiceImpl implements VideoSertvice{
private final LectureService lectureService; private final LectureService lectureService;
private final RegistrationService registrationService;
@Override @Override
public void startOnline(Long userId,Long lectureId) throws SQLException { public void startOnline(Long userId,Long lectureId) throws SQLException {
lectureService.changeState(lectureId); 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 jwt.access-token.expiretime=3000000
# Refresh Token ?? ?? (??? ??) # Refresh Token ?? ?? (??? ??)
jwt.refresh-token.expiretime=50400000 jwt.refresh-token.expiretime=504000000
#jwt.refresh-token.expiretime=4000 #jwt.refresh-token.expiretime=4000
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=${DATA_SOURCE_URL} spring.datasource.url=${DATA_SOURCE_URL}