Skip to content
This repository was archived by the owner on Apr 8, 2026. It is now read-only.

Commit cd0787e

Browse files
committed
refactor: Simplify getLobbies method by removing orderBy parameter and optimizing user filtering logic
1 parent 7562550 commit cd0787e

3 files changed

Lines changed: 38 additions & 38 deletions

File tree

dist/repositories/LobbyRepository.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export declare class LobbyRepository {
77
getLobbies(filters?: {
88
lobbyId?: string;
99
userId?: string;
10-
}, orderBy?: string): Promise<Lobby[]>;
10+
}): Promise<Lobby[]>;
1111
getLobby(lobbyId: string): Promise<Lobby | null>;
1212
getUserLobby(userId: string): Promise<Lobby | null>;
1313
getUserLobbies(userId: string): Promise<Lobby[]>;

dist/repositories/LobbyRepository.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,26 @@ class LobbyRepository {
66
this.databaseService = databaseService;
77
}
88
// Méthode générique pour récupérer les lobbies selon des filtres
9-
async getLobbies(filters = {}, orderBy = "lobbyId ASC") {
10-
let query = "SELECT lobbyId, users FROM lobbies WHERE 1=1";
11-
const params = [];
12-
if (filters.lobbyId) {
13-
query += " AND lobbyId = ?";
14-
params.push(filters.lobbyId);
15-
}
16-
if (filters.userId) {
17-
query += " AND JSON_EXTRACT(users, '$') LIKE ?";
18-
params.push(`%"${filters.userId}%"`);
19-
}
20-
query += ` ORDER BY ${orderBy}`;
21-
const rows = await this.databaseService.read(query, params);
9+
async getLobbies(filters = {}) {
10+
const query = "SELECT lobbyId, users FROM lobbies WHERE 1=1";
11+
const rows = await this.databaseService.read(query);
2212
// Parse users JSON for all lobbies
2313
const lobbies = [];
2414
for (const row of rows) {
25-
const users = await this.getUsersByIds(JSON.parse(row.users));
26-
lobbies.push({
27-
lobbyId: row.lobbyId,
28-
users
29-
});
15+
if (filters.userId && row.users.indexOf(filters.userId) !== -1 && filters.userId) {
16+
const users = await this.getUsersByIds(row.users);
17+
lobbies.push({
18+
lobbyId: row.lobbyId,
19+
users
20+
});
21+
}
22+
else if (filters.lobbyId && row.lobbyId === filters.lobbyId) {
23+
const users = await this.getUsersByIds(row.users);
24+
lobbies.push({
25+
lobbyId: row.lobbyId,
26+
users
27+
});
28+
}
3029
}
3130
return lobbies;
3231
}
@@ -37,6 +36,8 @@ class LobbyRepository {
3736
}
3837
async getUserLobby(userId) {
3938
const lobbies = await this.getLobbies({ userId });
39+
if (lobbies.length === 0)
40+
return null;
4041
return lobbies ? { lobbyId: lobbies[0].lobbyId, users: lobbies[0].users } : null;
4142
}
4243
async getUserLobbies(userId) {

src/repositories/LobbyRepository.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,27 @@ export class LobbyRepository {
77

88
// Méthode générique pour récupérer les lobbies selon des filtres
99
async getLobbies(
10-
filters: { lobbyId?: string; userId?: string } = {},
11-
orderBy: string = "lobbyId ASC"
10+
filters: { lobbyId?: string; userId?: string } = {}
1211
): Promise<Lobby[]> {
13-
let query = "SELECT lobbyId, users FROM lobbies WHERE 1=1";
14-
const params = [];
15-
if (filters.lobbyId) {
16-
query += " AND lobbyId = ?";
17-
params.push(filters.lobbyId);
18-
}
19-
if (filters.userId) {
20-
query += " AND JSON_EXTRACT(users, '$') LIKE ?";
21-
params.push(`%"${filters.userId}%"`);
22-
}
23-
query += ` ORDER BY ${orderBy}`;
24-
const rows = await this.databaseService.read<{ lobbyId: string; users: string }>(query, params);
12+
const query = "SELECT lobbyId, users FROM lobbies WHERE 1=1";
13+
const rows = await this.databaseService.read<{ lobbyId: string; users: string[] }>(query);
2514
// Parse users JSON for all lobbies
2615
const lobbies: Lobby[] = [];
2716
for (const row of rows) {
28-
const users = await this.getUsersByIds(JSON.parse(row.users));
29-
lobbies.push({
30-
lobbyId: row.lobbyId,
31-
users
32-
});
17+
if(filters.userId && row.users.indexOf(filters.userId) !== -1 && filters.userId) {
18+
const users = await this.getUsersByIds(row.users);
19+
lobbies.push({
20+
lobbyId: row.lobbyId,
21+
users
22+
});
23+
}
24+
else if (filters.lobbyId && row.lobbyId === filters.lobbyId) {
25+
const users = await this.getUsersByIds(row.users);
26+
lobbies.push({
27+
lobbyId: row.lobbyId,
28+
users
29+
});
30+
}
3331
}
3432
return lobbies;
3533
}
@@ -42,6 +40,7 @@ export class LobbyRepository {
4240

4341
async getUserLobby(userId: string): Promise<Lobby | null> {
4442
const lobbies = await this.getLobbies({ userId });
43+
if (lobbies.length === 0) return null;
4544
return lobbies ? { lobbyId: lobbies[0].lobbyId, users: lobbies[0].users } : null;
4645
}
4746

0 commit comments

Comments
 (0)