diff --git a/services/collaboration-service/src/db/prisma-db.ts b/services/collaboration-service/src/db/prisma-db.ts index f85ef4b2..43da0fbd 100644 --- a/services/collaboration-service/src/db/prisma-db.ts +++ b/services/collaboration-service/src/db/prisma-db.ts @@ -115,6 +115,20 @@ export async function saveAttempt(room_id: string): Promise { }); } +export async function setRoomQuestion( + room_id: string, + question_id: string +): Promise { + await prisma.room.update({ + where: { + room_id: room_id, + }, + data: { + question_id: question_id, + }, + }); +} + export async function createOrUpdateRoomWithUser( room_id: string, user_id: string diff --git a/services/collaboration-service/src/routes/room.ts b/services/collaboration-service/src/routes/room.ts index 3e2fa74f..cd374943 100644 --- a/services/collaboration-service/src/routes/room.ts +++ b/services/collaboration-service/src/routes/room.ts @@ -13,6 +13,7 @@ import { getRoom, getSavedRoomText, saveAttempt, + setRoomQuestion, } from "../db/prisma-db"; import { @@ -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 = {}; @@ -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) => {