Skip to content

Commit

Permalink
Merge pull request #92 from feliciagan/qnhist
Browse files Browse the repository at this point in the history
Qnhist
  • Loading branch information
jolynloh authored Nov 4, 2024
2 parents c7b8533 + 9865824 commit 73012f8
Show file tree
Hide file tree
Showing 34 changed files with 9,373 additions and 25 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jobs:
collab-service,
code-execution-service,
communication-service,
qn-history-service,
]
steps:
- name: Checkout code
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ docker-compose down
- Matching Service: http://localhost:3002
- Collab Service: http://localhost:3003
- Code Execution Service: http://localhost:3004
- Question History Service: http://localhost:3006
- Frontend: http://localhost:5173
2 changes: 2 additions & 0 deletions backend/matching-service/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ RABBITMQ_DEFAULT_PASS=password #comment out if use case is (1)
RABBITMQ_ADDR=amqp://admin:password@rabbitmq:5672 #comment out if use case is (1)

QUESTION_SERVICE_URL=http://question-service:3000/api/questions

QN_HISTORY_SERVICE_URL=http://qn-history-service:3006/api/qnhistories
51 changes: 35 additions & 16 deletions backend/matching-service/src/handlers/websocketHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "./matchHandler";
import { io } from "../server";
import { v4 as uuidv4 } from "uuid";
import { questionService } from "../utils/api";
import { qnHistoryService, questionService } from "../utils/api";

enum MatchEvents {
// Receive
Expand Down Expand Up @@ -120,23 +120,42 @@ export const handleWebsocketMatchEvents = (socket: Socket) => {
userConnections.delete(uid);
});

socket.on(MatchEvents.MATCH_ACCEPT_REQUEST, (matchId: string) => {
const partnerAccepted = handleMatchAccept(matchId);
if (partnerAccepted) {
const match = getMatchById(matchId);
if (!match) {
return;
}
socket.on(
MatchEvents.MATCH_ACCEPT_REQUEST,
(matchId: string, userId1: string, userId2: string) => {
const partnerAccepted = handleMatchAccept(matchId);
if (partnerAccepted) {
const match = getMatchById(matchId);
if (!match) {
return;
}

const { complexity, category } = match;
questionService
.get("/random", { params: { complexity, category } })
.then((res) => {
const { id } = res.data.question;
io.to(matchId).emit(MatchEvents.MATCH_SUCCESSFUL, id);
});
const { complexity, category, language } = match;
questionService
.get("/random", { params: { complexity, category } })
.then((res) => {
const qnId = res.data.question.id;
qnHistoryService
.post("/", {
userIds: [userId1, userId2],
questionId: qnId,
title: res.data.question.title,
submissionStatus: "Attempted",
dateAttempted: new Date(),
timeTaken: 0,
language: language,
})
.then((res) => {
io.to(matchId).emit(
MatchEvents.MATCH_SUCCESSFUL,
qnId,
res.data.qnHistory.id
);
});
});
}
}
});
);

socket.on(
MatchEvents.MATCH_DECLINE_REQUEST,
Expand Down
11 changes: 11 additions & 0 deletions backend/matching-service/src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@ const QUESTION_SERVICE_URL =
process.env.QUESTION_SERVICE_URL ||
"http://question-service:3000/api/questions";

const QN_HISTORY_SERVICE_URL =
process.env.QN_HISTORY_SERVICE_URL ||
"http://qn-history-service:3006/api/qnhistories";

export const questionService = axios.create({
baseURL: QUESTION_SERVICE_URL,
headers: {
"Content-Type": "application/json",
},
});

export const qnHistoryService = axios.create({
baseURL: QN_HISTORY_SERVICE_URL,
headers: {
"Content-Type": "application/json",
},
});
5 changes: 5 additions & 0 deletions backend/qn-history-service/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
coverage
node_modules
tests
.env*
*.md
25 changes: 25 additions & 0 deletions backend/qn-history-service/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
NODE_ENV=development
SERVICE_PORT=3006

ORIGINS=http://localhost:5173,http://127.0.0.1:5173

# if using cloud MongoDB, replace with actual URI (run service separately)
MONGO_CLOUD_URI=<MONGO_CLOUD_URI>

MONGO_URI_TEST=mongodb://mongo:mongo@test-mongo:27017/

# if using local MongoDB (run service with docker-compose)
## MongoDB credentials
MONGO_INITDB_ROOT_USERNAME=root
MONGO_INITDB_ROOT_PASSWORD=example

## Mongo Express credentials
ME_CONFIG_BASICAUTH_USERNAME=admin
ME_CONFIG_BASICAUTH_PASSWORD=password

## Do not change anything below this line
ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_INITDB_ROOT_USERNAME}
ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_INITDB_ROOT_PASSWORD}
ME_CONFIG_MONGODB_URL=mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@qn-history-service-mongo:27017/

MONGO_LOCAL_URI=mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@qn-history-service-mongo:27017/
13 changes: 13 additions & 0 deletions backend/qn-history-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:20-alpine

WORKDIR /qn-history-service

COPY package*.json ./

RUN npm ci

COPY . .

EXPOSE 3006

CMD ["npm", "run", "dev"]
29 changes: 29 additions & 0 deletions backend/qn-history-service/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Question History Service Guide

> Please ensure that you have completed the backend set-up [here](../README.md) before proceeding.

## Setting-up Question History Service

1. In the `qn-history-service` directory, create a copy of the `.env.sample` file and name it `.env`.

2. To connect to your cloud MongoDB instead of your local MongoDB, set the `NODE_ENV` to `production` instead of `development`.

3. Update `MONGO_INITDB_ROOT_USERNAME`, `MONGO_INITDB_ROOT_PASSWORD` to change your MongoDB credentials if necessary.

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.

## Running Question History Service without Docker

> Make sure you have the cloud MongoDB URI in your .env file and set NODE_ENV to production already.

1. Open Command Line/Terminal and navigate into the `qn-history-service` directory.

2. Run the command: `npm install`. This will install all the necessary dependencies.

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.

## After running

1. To view Question History Service documentation, go to http://localhost:3006/docs.

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.
22 changes: 22 additions & 0 deletions backend/qn-history-service/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";

export default [
{ files: ["**/*.{js,mjs,cjs,ts}"] },
{ languageOptions: { globals: globals.node } },
{
rules: {
"@typescript-eslint/no-unused-vars": [
"warn",
{
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
caughtErrorsIgnorePattern: "^_",
},
],
},
},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
];
Loading

0 comments on commit 73012f8

Please sign in to comment.