Skip to content

Commit 2d887d4

Browse files
committed
Add base code for History Service
1 parent e1a8828 commit 2d887d4

File tree

7 files changed

+158
-0
lines changed

7 files changed

+158
-0
lines changed

Backend/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
QuestionService/.env
22
user-service/.env
33
MatchingService/.env
4+
HistoryService/.env
45
QuestionService/insert_questions_script.py
56

67
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

Backend/HistoryService/Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM node:20
2+
3+
WORKDIR /app
4+
5+
COPY package*.json ./
6+
RUN npm install
7+
8+
COPY . .
9+
10+
EXPOSE 3001
11+
CMD ["npm", "start"]

Backend/HistoryService/app.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const express = require('express')
2+
const mongoose = require('mongoose')
3+
const cors = require('cors')
4+
const dotenv = require("dotenv")
5+
const historyRouter = require("./controllers/histories")
6+
7+
dotenv.config()
8+
9+
const app = express()
10+
app.use(cors())
11+
app.use(express.json())
12+
13+
const mongoURI = `mongodb+srv://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@cs3219.rrxz3.mongodb.net/History-DB?retryWrites=true&w=majority&appName=CS3219`
14+
mongoose.connect(mongoURI)
15+
.then(() => console.log('MongoDB connected'))
16+
.catch(err => console.error('MongoDB connection error:', err));
17+
18+
app.use('/api/histories', questionRouter)
19+
20+
app.get("/", (req, res, next) => {
21+
console.log("Sending Greetings!");
22+
res.json({
23+
message: "Hello World from history-service",
24+
});
25+
});
26+
27+
// Handle When No Route Match Is Found
28+
app.use((req, res, next) => {
29+
const error = new Error("Route Not Found");
30+
error.status = 404;
31+
next(error);
32+
});
33+
34+
app.use((error, req, res, next) => {
35+
res.status(error.status || 500);
36+
res.json({
37+
error: {
38+
message: error.message,
39+
},
40+
});
41+
});
42+
43+
module.exports = app
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const historyRouter = require('express').Router();
2+
const HistoryModel = require('../models/History');
3+
4+
// Get all history for the logged-in user
5+
historyRouter.get("/", async (req, res) => {
6+
const userId = req.user.id; // Assuming you're using authentication middleware
7+
8+
try {
9+
// Fetch all history entries for the logged-in user
10+
const history = await HistoryModel.find({ user: userId })
11+
.populate('question matchedUser') // Populating question and matchedUser details
12+
.exec();
13+
res.status(200).json(history);
14+
} catch (error) {
15+
res.status(500).json({ error: error.message });
16+
}
17+
});
18+
19+
// Post a new history entry when a user exits a session
20+
historyRouter.post("/", async (req, res) => {
21+
const { user, matchedUser, question, duration, code } = req.body;
22+
23+
try {
24+
const newHistory = new HistoryModel({
25+
user,
26+
matchedUser,
27+
question,
28+
duration,
29+
code
30+
});
31+
32+
await newHistory.save();
33+
res.status(201).json(newHistory);
34+
} catch (error) {
35+
res.status(500).json({ error: error.message });
36+
}
37+
});
38+
39+
module.exports = historyRouter;

Backend/HistoryService/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const app = require('./app')
2+
3+
// Change the port number to listen to a different port but remember to change the port number in frontend too!
4+
app.listen(3005, () => {
5+
console.log("Server is Running")
6+
})
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const mongoose = require('mongoose');
2+
3+
const HistorySchema = new mongoose.Schema({
4+
user: {
5+
type: mongoose.Schema.Types.ObjectId,
6+
ref: 'User',
7+
required: true // The user for whom this history entry is being created
8+
},
9+
matchedUser: {
10+
type: mongoose.Schema.Types.ObjectId,
11+
ref: 'User',
12+
required: true // The other user they were matched with
13+
},
14+
question: {
15+
type: mongoose.Schema.Types.ObjectId, // Reference to the Question model
16+
ref: 'Question',
17+
required: true
18+
},
19+
datetime: {
20+
type: Date, // Time when the session started
21+
default: Date.now
22+
},
23+
duration: {
24+
type: Number, // Duration in minutes for the individual user
25+
required: true
26+
},
27+
code: {
28+
type: Buffer, // Store the code as binary file
29+
required: true
30+
}
31+
});
32+
33+
// Create the History model
34+
const HistoryModel = mongoose.model('History', HistorySchema, 'History');
35+
module.exports = HistoryModel;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "server",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "nodemon index.js"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"dependencies": {
14+
"cors": "^2.8.5",
15+
"dotenv": "^16.4.5",
16+
"express": "^4.21.0",
17+
"mongodb": "^6.9.0",
18+
"mongoose": "^8.6.3",
19+
"mongosh": "^2.3.1",
20+
"nodemon": "^3.1.7"
21+
}
22+
}
23+

0 commit comments

Comments
 (0)