Skip to content

Commit 73012f8

Browse files
authored
Merge pull request #92 from feliciagan/qnhist
Qnhist
2 parents c7b8533 + 9865824 commit 73012f8

File tree

34 files changed

+9373
-25
lines changed

34 files changed

+9373
-25
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ jobs:
4141
collab-service,
4242
code-execution-service,
4343
communication-service,
44+
qn-history-service,
4445
]
4546
steps:
4647
- name: Checkout code

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ docker-compose down
3131
- Matching Service: http://localhost:3002
3232
- Collab Service: http://localhost:3003
3333
- Code Execution Service: http://localhost:3004
34+
- Question History Service: http://localhost:3006
3435
- Frontend: http://localhost:5173

backend/matching-service/.env.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ RABBITMQ_DEFAULT_PASS=password #comment out if use case is (1)
1414
RABBITMQ_ADDR=amqp://admin:password@rabbitmq:5672 #comment out if use case is (1)
1515

1616
QUESTION_SERVICE_URL=http://question-service:3000/api/questions
17+
18+
QN_HISTORY_SERVICE_URL=http://qn-history-service:3006/api/qnhistories

backend/matching-service/src/handlers/websocketHandler.ts

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from "./matchHandler";
1212
import { io } from "../server";
1313
import { v4 as uuidv4 } from "uuid";
14-
import { questionService } from "../utils/api";
14+
import { qnHistoryService, questionService } from "../utils/api";
1515

1616
enum MatchEvents {
1717
// Receive
@@ -120,23 +120,42 @@ export const handleWebsocketMatchEvents = (socket: Socket) => {
120120
userConnections.delete(uid);
121121
});
122122

123-
socket.on(MatchEvents.MATCH_ACCEPT_REQUEST, (matchId: string) => {
124-
const partnerAccepted = handleMatchAccept(matchId);
125-
if (partnerAccepted) {
126-
const match = getMatchById(matchId);
127-
if (!match) {
128-
return;
129-
}
123+
socket.on(
124+
MatchEvents.MATCH_ACCEPT_REQUEST,
125+
(matchId: string, userId1: string, userId2: string) => {
126+
const partnerAccepted = handleMatchAccept(matchId);
127+
if (partnerAccepted) {
128+
const match = getMatchById(matchId);
129+
if (!match) {
130+
return;
131+
}
130132

131-
const { complexity, category } = match;
132-
questionService
133-
.get("/random", { params: { complexity, category } })
134-
.then((res) => {
135-
const { id } = res.data.question;
136-
io.to(matchId).emit(MatchEvents.MATCH_SUCCESSFUL, id);
137-
});
133+
const { complexity, category, language } = match;
134+
questionService
135+
.get("/random", { params: { complexity, category } })
136+
.then((res) => {
137+
const qnId = res.data.question.id;
138+
qnHistoryService
139+
.post("/", {
140+
userIds: [userId1, userId2],
141+
questionId: qnId,
142+
title: res.data.question.title,
143+
submissionStatus: "Attempted",
144+
dateAttempted: new Date(),
145+
timeTaken: 0,
146+
language: language,
147+
})
148+
.then((res) => {
149+
io.to(matchId).emit(
150+
MatchEvents.MATCH_SUCCESSFUL,
151+
qnId,
152+
res.data.qnHistory.id
153+
);
154+
});
155+
});
156+
}
138157
}
139-
});
158+
);
140159

141160
socket.on(
142161
MatchEvents.MATCH_DECLINE_REQUEST,

backend/matching-service/src/utils/api.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,20 @@ const QUESTION_SERVICE_URL =
44
process.env.QUESTION_SERVICE_URL ||
55
"http://question-service:3000/api/questions";
66

7+
const QN_HISTORY_SERVICE_URL =
8+
process.env.QN_HISTORY_SERVICE_URL ||
9+
"http://qn-history-service:3006/api/qnhistories";
10+
711
export const questionService = axios.create({
812
baseURL: QUESTION_SERVICE_URL,
913
headers: {
1014
"Content-Type": "application/json",
1115
},
1216
});
17+
18+
export const qnHistoryService = axios.create({
19+
baseURL: QN_HISTORY_SERVICE_URL,
20+
headers: {
21+
"Content-Type": "application/json",
22+
},
23+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
coverage
2+
node_modules
3+
tests
4+
.env*
5+
*.md
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
NODE_ENV=development
2+
SERVICE_PORT=3006
3+
4+
ORIGINS=http://localhost:5173,http://127.0.0.1:5173
5+
6+
# if using cloud MongoDB, replace with actual URI (run service separately)
7+
MONGO_CLOUD_URI=<MONGO_CLOUD_URI>
8+
9+
MONGO_URI_TEST=mongodb://mongo:mongo@test-mongo:27017/
10+
11+
# if using local MongoDB (run service with docker-compose)
12+
## MongoDB credentials
13+
MONGO_INITDB_ROOT_USERNAME=root
14+
MONGO_INITDB_ROOT_PASSWORD=example
15+
16+
## Mongo Express credentials
17+
ME_CONFIG_BASICAUTH_USERNAME=admin
18+
ME_CONFIG_BASICAUTH_PASSWORD=password
19+
20+
## Do not change anything below this line
21+
ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_INITDB_ROOT_USERNAME}
22+
ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_INITDB_ROOT_PASSWORD}
23+
ME_CONFIG_MONGODB_URL=mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@qn-history-service-mongo:27017/
24+
25+
MONGO_LOCAL_URI=mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@qn-history-service-mongo:27017/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM node:20-alpine
2+
3+
WORKDIR /qn-history-service
4+
5+
COPY package*.json ./
6+
7+
RUN npm ci
8+
9+
COPY . .
10+
11+
EXPOSE 3006
12+
13+
CMD ["npm", "run", "dev"]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Question History Service Guide
2+
3+
> Please ensure that you have completed the backend set-up [here](../README.md) before proceeding.
4+
5+
## Setting-up Question History Service
6+
7+
1. In the `qn-history-service` directory, create a copy of the `.env.sample` file and name it `.env`.
8+
9+
2. To connect to your cloud MongoDB instead of your local MongoDB, set the `NODE_ENV` to `production` instead of `development`.
10+
11+
3. Update `MONGO_INITDB_ROOT_USERNAME`, `MONGO_INITDB_ROOT_PASSWORD` to change your MongoDB credentials if necessary.
12+
13+
4. You can view the MongoDB collections locally using Mongo Express. To set up Mongo Express, update `ME_CONFIG_BASICAUTH_USERNAME` and `ME_CONFIG_BASICAUTH_PASSWORD`. The username and password will be the login credentials when you access Mongo Express at http://localhost:8083.
14+
15+
## Running Question History Service without Docker
16+
17+
> Make sure you have the cloud MongoDB URI in your .env file and set NODE_ENV to production already.
18+
19+
1. Open Command Line/Terminal and navigate into the `qn-history-service` directory.
20+
21+
2. Run the command: `npm install`. This will install all the necessary dependencies.
22+
23+
3. Run the command `npm start` to start the Question History Service in production mode, or use `npm run dev` for development mode, which includes features like automatic server restart when you make code changes.
24+
25+
## After running
26+
27+
1. To view Question History Service documentation, go to http://localhost:3006/docs.
28+
29+
2. Using applications like Postman, you can interact with the Question History Service on port 3006. If you wish to change this, please update the `.env` file.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import globals from "globals";
2+
import pluginJs from "@eslint/js";
3+
import tseslint from "typescript-eslint";
4+
5+
export default [
6+
{ files: ["**/*.{js,mjs,cjs,ts}"] },
7+
{ languageOptions: { globals: globals.node } },
8+
{
9+
rules: {
10+
"@typescript-eslint/no-unused-vars": [
11+
"warn",
12+
{
13+
argsIgnorePattern: "^_",
14+
varsIgnorePattern: "^_",
15+
caughtErrorsIgnorePattern: "^_",
16+
},
17+
],
18+
},
19+
},
20+
pluginJs.configs.recommended,
21+
...tseslint.configs.recommended,
22+
];

0 commit comments

Comments
 (0)