Skip to content

Commit 3687a80

Browse files
authored
Merge pull request #32 from StudyLink-SW-Project/feature/#31
[FEAT] #31 스터디룸 목록 조회 api 구현
2 parents 723f046 + 3bb2cde commit 3687a80

File tree

6 files changed

+195
-0
lines changed

6 files changed

+195
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.example.be.config;
2+
3+
import io.livekit.server.RoomServiceClient;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
@Configuration
9+
public class LiveKitConfig {
10+
11+
@Value("${livekit.api.key}")
12+
private String LIVEKIT_API_KEY;
13+
14+
@Value("${livekit.api.secret}")
15+
private String LIVEKIT_API_SECRET;
16+
17+
@Value("${livekit.api.host}")
18+
private String LIVEKIT_API_HOST;
19+
20+
@Bean
21+
public RoomServiceClient roomServiceClient() {
22+
return RoomServiceClient.create(
23+
LIVEKIT_API_HOST,
24+
LIVEKIT_API_KEY,
25+
LIVEKIT_API_SECRET
26+
);
27+
}
28+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.example.be.service;
2+
3+
import com.example.be.web.dto.RoomDTO;
4+
import io.livekit.server.RoomService;
5+
import io.livekit.server.RoomServiceClient;
6+
import livekit.LivekitModels;
7+
import livekit.LivekitRoom;
8+
import lombok.RequiredArgsConstructor;
9+
import lombok.extern.slf4j.Slf4j;
10+
import org.springframework.stereotype.Service;
11+
import retrofit2.Call;
12+
import retrofit2.Response;
13+
14+
import java.io.IOException;
15+
import java.util.List;
16+
import java.util.stream.Collectors;
17+
18+
@Slf4j
19+
@Service
20+
@RequiredArgsConstructor
21+
public class RoomServiceImpl {
22+
private final RoomServiceClient roomServiceClient;
23+
24+
public RoomDTO.RoomListResponseDto getAllRooms() {
25+
try {
26+
Call<List<LivekitModels.Room>> call = roomServiceClient.listRooms();
27+
Response<List<LivekitModels.Room>> response = call.execute();
28+
29+
if (!response.isSuccessful()) {
30+
throw new RuntimeException("Failed to fetch rooms: " + response.errorBody().string());
31+
}
32+
33+
List<LivekitModels.Room> rooms = response.body();
34+
List<RoomDTO.RoomDto> roomDtos = rooms.stream()
35+
.map(this::convertToDto)
36+
.collect(Collectors.toList());
37+
38+
return RoomDTO.RoomListResponseDto.builder()
39+
.rooms(roomDtos)
40+
.totalCount(roomDtos.size())
41+
.build();
42+
} catch (IOException e) {
43+
log.error("Failed to fetch rooms from LiveKit", e);
44+
throw new RuntimeException("Failed to fetch rooms from LiveKit", e);
45+
}
46+
}
47+
48+
49+
private RoomDTO.RoomDto convertToDto(LivekitModels.Room room) {
50+
return RoomDTO.RoomDto.builder()
51+
.sid(room.getSid())
52+
.name(room.getName())
53+
.creationTime(room.getCreationTime())
54+
.numParticipants(room.getNumParticipants())
55+
.maxParticipants(room.getMaxParticipants())
56+
.metadata(room.getMetadata())
57+
.build();
58+
}
59+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.example.be.web.controller;
2+
3+
4+
import com.example.be.apiPayload.ApiResponse;
5+
import com.example.be.service.RoomServiceImpl;
6+
import com.example.be.web.dto.RoomDTO;
7+
import io.livekit.server.AccessToken;
8+
import io.livekit.server.RoomJoin;
9+
import io.livekit.server.RoomList;
10+
import io.livekit.server.VideoGrant;
11+
import io.swagger.v3.oas.annotations.Operation;
12+
import lombok.RequiredArgsConstructor;
13+
import org.springframework.beans.factory.annotation.Value;
14+
import org.springframework.http.HttpHeaders;
15+
import org.springframework.web.bind.annotation.GetMapping;
16+
import org.springframework.web.bind.annotation.RequestMapping;
17+
import org.springframework.web.bind.annotation.RestController;
18+
import org.springframework.web.client.RestTemplate;
19+
20+
@RestController
21+
@RequestMapping("/room")
22+
@RequiredArgsConstructor
23+
public class RoomController {
24+
private final RoomServiceImpl roomService;
25+
26+
27+
@GetMapping("/rooms")
28+
@Operation(summary = "방 목록 조회", description = "생성된 모든 방 목록을 조회합니다.")
29+
public ApiResponse<RoomDTO.RoomListResponseDto> getAllRooms() {
30+
//
31+
// AccessToken accessToken = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET);
32+
//
33+
// // Grant 설정
34+
// RoomList roomList = new RoomList(true);
35+
// accessToken.addGrants(roomList);
36+
//
37+
// String jwt = accessToken.toJwt();
38+
// HttpHeaders headers = new HttpHeaders();
39+
// headers.add("Authorization", "Bearer " + jwt);
40+
41+
return ApiResponse.onSuccess(roomService.getAllRooms());
42+
}
43+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.example.be.web.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
8+
public class ParticipantDTO {
9+
10+
@Builder
11+
@Getter
12+
@NoArgsConstructor
13+
@AllArgsConstructor
14+
public static class ParticipantDto {
15+
private String sid;
16+
private String identity;
17+
private String name;
18+
private String metadata;
19+
private long joinedAt;
20+
}
21+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.example.be.web.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
8+
import java.util.List;
9+
10+
public class RoomDTO {
11+
12+
@Builder
13+
@Getter
14+
@NoArgsConstructor
15+
@AllArgsConstructor
16+
public static class RoomDto {
17+
private String sid;
18+
private String name;
19+
private long creationTime;
20+
private int numParticipants;
21+
private int maxParticipants;
22+
private String metadata;
23+
}
24+
25+
@Builder
26+
@Getter
27+
@NoArgsConstructor
28+
@AllArgsConstructor
29+
public static class CreateRoomRequestDto {
30+
private String roomName;
31+
private Integer maxParticipants;
32+
private String metadata;
33+
}
34+
35+
@Builder
36+
@Getter
37+
@NoArgsConstructor
38+
@AllArgsConstructor
39+
public static class RoomListResponseDto {
40+
private List<RoomDto> rooms;
41+
private int totalCount;
42+
}
43+
}

src/main/resources/application.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,6 @@ server:
7878

7979
livekit:
8080
api:
81+
host: ${LIVEKIT_API_HOST}
8182
key: ${LIVEKIT_API_KEY}
8283
secret: ${LIVEKIT_API_SECRET}

0 commit comments

Comments
 (0)