Skip to content

Commit 4802cd3

Browse files
author
Aishwarya Nair
committed
Merge branch 'test-updated-merge' of https://github.com/CS3219-AY2324S1/ay2324s1-course-assessment-g16 into test-updated-merge
2 parents e7005ed + f844166 commit 4802cd3

File tree

3 files changed

+40
-42
lines changed

3 files changed

+40
-42
lines changed

CollaborationService/services/collaborationService.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,20 @@ const startCollaboration = async (socket, io) => {
9090

9191
console.log(`init code input storage for session ${sessionId}`);
9292
socket.broadcast.to(sessionId).emit("init-code", language, codes);
93-
93+
let questionValue;
94+
try {
95+
console.log("The session:",session)
96+
const questionObject = await getQuestionById(session.questionId);
97+
questionValue = questionObject.question;
98+
console.log('The question description:', questionValue);
99+
await new Promise((resolve) => {
100+
socket.broadcast.to(sessionId).emit("recv-question", questionValue);
101+
resolve();
102+
});
103+
} catch (error) {
104+
console.error("Error retrieving or emitting question:", error);
105+
}
106+
94107
socket.on("update-code", async (codes) => {
95108
console.log(`Code changed`);
96109
socket.broadcast.to(sessionId).emit("code-changed", codes);
@@ -111,14 +124,6 @@ const startCollaboration = async (socket, io) => {
111124
await updateCollaborativeInput(sessionId, []);
112125
});
113126

114-
socket.on("get-question", async () => {
115-
console.log(`Getting question for session ${sessionId}`);
116-
117-
const question = await getQuestionById(session.questionId);
118-
119-
socket.emit("recv-question", question);
120-
});
121-
122127
socket.on("extend-time", async () => {
123128
timerDelay = 2000;
124129

Frontend/src/Components/Collaboration/CollaborationWindow.jsx

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ const CollaborationWindow = () => {
5151
// Handle new user joining
5252
});
5353

54+
socket.current.on('recv-question', (receivedQuestion) => {
55+
console.log('Received question: ', receivedQuestion);
56+
setQuestion(receivedQuestion);
57+
});
58+
5459
socket.current.on("init-code", (lang, codes) => {
5560
setCollaborativeInput(codes);
5661
if (lang === "None") {
@@ -108,24 +113,6 @@ const CollaborationWindow = () => {
108113

109114
const userId = getUserId();
110115

111-
const fetchQuestion = async () => {
112-
try {
113-
const response = await axios.get(`http://localhost:3004/home/${userId}`);
114-
if (response.status === 200) {
115-
const json = response.data;
116-
setQuestion(json.questionId);
117-
} else {
118-
console.log("Response is not ok:", response.statusText);
119-
}
120-
} catch (error) {
121-
console.error("Loading questions encountered error:", error);
122-
}
123-
};
124-
125-
useEffect(() => {
126-
fetchQuestion();
127-
}, []);
128-
129116
const showToast = (message) => {
130117
setToast({visible: true, message});
131118
setTimeout(() => {
@@ -266,11 +253,13 @@ const CollaborationWindow = () => {
266253
</div>
267254
<div className="content-area">
268255
<div className="question-section">
269-
{question && (
256+
{question ? (
270257
<>
271-
<h2>{question.title}</h2>
272-
<p>{question.description}</p>
258+
<h2>{question.title}</h2>
259+
<p>{question.description}</p>
273260
</>
261+
) : (
262+
<p>Loading question...</p>
274263
)}
275264
</div>
276265
<div className="editor-section">

QuestionService/controllers/questionController.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,22 @@ const getMatchQuestion = async (req, res) => {
6060
};
6161

6262
const getQuestion = async (req, res) => {
63-
const { questionId } = req.params.id;
64-
65-
const question = await Question.findById(questionId);
66-
67-
const response = {
68-
questionId: questionId,
69-
question: question,
70-
};
71-
72-
console.log("get", questionId);
73-
74-
return res.status(200).json(response);
63+
try {
64+
const questionId = req.params.id;
65+
const question = await Question.findOne({ _id: questionId });
66+
if (!question) {
67+
return res.status(404).json({ error: "Question not found" });
68+
}
69+
const response = {
70+
questionId: questionId,
71+
question: question,
72+
};
73+
console.log("Identified a question:", response);
74+
return res.status(200).json(response);
75+
} catch (error) {
76+
console.error("Error:", error);
77+
return res.status(500).json({ error: "Internal server error" });
78+
}
7579
};
7680

7781
const getAllQuestions = async (req, res) => {

0 commit comments

Comments
 (0)