Skip to content

Commit

Permalink
Merge pull request #46 from CS3219-AY2425S1/branch-rubesh-collabInteg…
Browse files Browse the repository at this point in the history
…ration

Add relevant code for CORS between question-service and matching
  • Loading branch information
bhnuka authored Nov 3, 2024
2 parents 2bc0ab5 + 625005e commit 98f558a
Show file tree
Hide file tree
Showing 9 changed files with 360 additions and 158 deletions.
6 changes: 5 additions & 1 deletion peerprep/backend/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ services:
- "1234:1234"
- "3003:3003"
env_file:
- ./collab-service/.env
- ./collab-service/.env

networks:
my-network:
driver: bridge
1 change: 1 addition & 0 deletions peerprep/backend/matching-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^1.7.7",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.21.1",
Expand Down
39 changes: 38 additions & 1 deletion peerprep/backend/matching-service/src/services/queueManager2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const getStatus = async (userId: string) => {
try {
const sessionID = await getSessionStatus(userId);
if (sessionID !== null) {
// inject the question finder here, and return tgt with sessionID
return "matched on Session ID: " + sessionID;
} else {
const requestStatus = await getRequestStatus(userId);
Expand All @@ -85,4 +86,40 @@ export const getStatus = async (userId: string) => {
console.error('Error in getStatus:', error);
throw new Error("Failed to retrieve the status of the user's match request");
}
}
}

/*
export const getStatus = async (userId: string, topic: string, difficulty: string) => {
try {
const sessionID = await getSessionStatus(userId);
if (sessionID !== null) {
// Fetch a random question based on topic and difficulty
const question = await getRandomQuestionByTopicAndDifficulty(topic, difficulty);
// If a question is found, return it with the session ID
if (question) {
return {
status: "matched",
sessionId: sessionID,
question: {
id: question.questionId,
title: question.title,
description: question.description
}
};
} else {
return { status: "matched", sessionId: sessionID, question: null };
}
} else {
const requestStatus = await getRequestStatus(userId);
if (requestStatus !== null) {
return { status: requestStatus };
}
return { status: "none" };
}
} catch (error) {
console.error('Error in getStatus:', error);
throw new Error("Failed to retrieve the status of the user's match request");
}
};
*/
23 changes: 22 additions & 1 deletion peerprep/backend/matching-service/src/utils/redisUtils3.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ChainableCommander } from "ioredis";
import axios from 'axios';
import connectRedis from "../../config/redis";
import { Session } from "../models/Session";
import { Redis } from 'ioredis';
Expand Down Expand Up @@ -304,7 +305,7 @@ const createSession = async (
): Promise<string> =>{
const sessionId = "lol";
try {
const sessionId = `${userId1}-${userId2}-${Date.now()}`;
let sessionId = `${userId1}-${userId2}-${Date.now()}`;
const session: Session = {
sessionId: sessionId,
userId1,
Expand All @@ -313,6 +314,13 @@ const createSession = async (
difficulty,
timestamp: Date.now(),
};

/* RANDOM QUESTION STARTS HERE @MAHI
const randomQuestion = await getRandomQuestionFromQuestionService(topic, difficulty);
console.log("Random Question:", randomQuestion?.title);
sessionId = sessionId + "Q" + randomQuestion?.title
*/

await saveSession(session);
// return sessionId as string
// convert to string to match the return type
Expand All @@ -323,6 +331,19 @@ const createSession = async (
}
};

export const getRandomQuestionFromQuestionService = async (topic: string, difficulty: string) => {
try {
//const response = await axios.get(`http://localhost:8080/api/questions/random-question`, {
const response = await axios.get(`http://question-service:8080/api/questions/random-question`, {
params: { topic: topic, difficulty: difficulty }
});
return response.data;
} catch (error) {
console.error('Error fetching random question from question-service:', error);
throw new Error('Failed to fetch random question from question service');
}
};

export const findMatchInQueueByTopic = async (
userId: string
): Promise<String | null> => {
Expand Down
Loading

0 comments on commit 98f558a

Please sign in to comment.