forked from boostcampwm-2024/web27-Preview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroom.repository.ts
More file actions
83 lines (73 loc) · 2.97 KB
/
Copy pathroom.repository.ts
File metadata and controls
83 lines (73 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "nestjs-redis-om";
import { Repository } from "redis-om";
import { RoomEntity } from "@/room/room.entity";
import { RoomDto } from "@/room/dto/room.dto";
@Injectable()
export class RoomRepository {
public constructor(
@InjectRepository(RoomEntity)
private readonly roomRepository: Repository<RoomEntity>
) {}
// TODO : .from 메서드 구현 필요?
public async getAllRoom(): Promise<RoomDto[]> {
const allRooms = await this.roomRepository.search().return.all();
return allRooms.map((room: RoomEntity) => {
const connectionMap = JSON.parse(room.connectionMap || "{}");
return {
connectionMap: JSON.parse(room.connectionMap || "{}"),
createdAt: room.createdAt,
host: JSON.parse(room.host),
maxParticipants: room.maxParticipants,
maxQuestionListLength: room.maxQuestionListLength,
currentIndex: room.currentIndex,
status: room.status,
title: room.title,
id: room.id,
category: room.category,
inProgress: room.inProgress,
participants: Object.keys(connectionMap).length,
};
});
}
public async getRoom(id: string): Promise<RoomDto> {
const room = await this.roomRepository.search().where("id").eq(id).return.first();
if (!room) return null;
const connectionMap = JSON.parse(room.connectionMap || "{}");
return {
category: room.category,
inProgress: room.inProgress,
connectionMap,
createdAt: room.createdAt,
currentIndex: room.currentIndex,
maxQuestionListLength: room.maxQuestionListLength,
host: JSON.parse(room.host),
participants: Object.keys(connectionMap).length,
maxParticipants: room.maxParticipants,
status: room.status,
title: room.title,
id: room.id,
};
}
public async setRoom(dto: RoomDto): Promise<void> {
const room = new RoomEntity();
room.id = dto.id;
room.category = dto.category;
room.inProgress = dto.inProgress;
room.title = dto.title;
room.status = dto.status;
room.currentIndex = dto.currentIndex;
room.connectionMap = JSON.stringify(dto.connectionMap);
room.maxParticipants = dto.maxParticipants;
room.maxQuestionListLength = dto.maxQuestionListLength;
room.createdAt = Date.now();
room.host = JSON.stringify(dto.host);
await this.roomRepository.save(room.id, room);
}
public async removeRoom(id: string): Promise<void> {
const entities = await this.roomRepository.search().where("id").equals(id).return.all();
for await (const entity of entities) {
await this.roomRepository.remove(entity.id);
}
}
}