Skip to content

Commit

Permalink
SetQuestion event
Browse files Browse the repository at this point in the history
  • Loading branch information
gycgabriel committed Oct 14, 2023
1 parent 69c82e5 commit 74f4e42
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
14 changes: 14 additions & 0 deletions services/collaboration-service/src/db/prisma-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ export async function saveAttempt(room_id: string): Promise<void> {
});
}

export async function setRoomQuestion(
room_id: string,
question_id: string
): Promise<void> {
await prisma.room.update({
where: {
room_id: room_id,
},
data: {
question_id: question_id,
},
});
}

export async function createOrUpdateRoomWithUser(
room_id: string,
user_id: string
Expand Down
9 changes: 9 additions & 0 deletions services/collaboration-service/src/routes/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getRoom,
getSavedRoomText,
saveAttempt,
setRoomQuestion,
} from "../db/prisma-db";

import {
Expand All @@ -33,6 +34,7 @@ enum SocketEvents {
ROOM_UPDATE = "api/collaboration-service/room/update",
ROOM_SAVE = "api/collaboration-service/room/save",
ROOM_LOAD = "api/collaboration-service/room/load",
QUESTION_SET = "api/collaboration-service/question/set",
}

const socketMap: Record<string, SocketDetails> = {};
Expand Down Expand Up @@ -195,6 +197,13 @@ function initSocketListeners(io: Server, socket: Socket, room_id: string) {
);

socket.on(SocketEvents.ROOM_LOAD, () => loadTextFromDb(io, socket, room_id));

socket.on(SocketEvents.QUESTION_SET, (question: string) => {
setRoomQuestion(room_id, question).then(() => {
console.log("Question set:", question);
});
io.to(room_id).emit(SocketEvents.QUESTION_SET, question);
});
}

export const roomRouter = (io: Server) => {
Expand Down

0 comments on commit 74f4e42

Please sign in to comment.