Skip to content

Commit c88fafa

Browse files
committed
Add base code for History Service
1 parent 994deba commit c88fafa

File tree

2 files changed

+26
-48
lines changed

2 files changed

+26
-48
lines changed
Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,39 @@
11
const historyRouter = require('express').Router();
2-
const HistoryModel = require('../models/Histories');
2+
const HistoryModel = require('../models/History');
33

44
// 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
5+
historyRouter.get("/", async (req, res) => {
6+
const userId = req.user.id; // Assuming you're using authentication middleware
77

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-
// Assuming Express.js
20-
historyRouter.get('/user/:userId', async (req, res) => {
21-
const { userId } = req.params;
228
try {
23-
const attempts = await HistoryModel.find({ user: userId }); // Fetch by user object ID
24-
res.json(attempts);
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);
2514
} catch (error) {
26-
res.status(500).json({ error: 'Failed to fetch attempt history' });
15+
res.status(500).json({ error: error.message });
2716
}
2817
});
2918

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;
3022

31-
// // Post a new history entry when a user exits a session
32-
// historyRouter.post("/", async (req, res) => {
33-
// const { user, matchedUser, question, duration, code } = req.body;
34-
35-
// try {
36-
// const newHistory = new HistoryModel({
37-
// user,
38-
// matchedUser,
39-
// question,
40-
// duration,
41-
// code
42-
// });
43-
44-
// await newHistory.save();
45-
// res.status(201).json(newHistory);
46-
// } catch (error) {
47-
// res.status(500).json({ error: error.message });
48-
// }
49-
// });
50-
51-
historyRouter.post('/', async (req, res) => {
5223
try {
53-
const newSession = new HistoryModel(req.body);
54-
await newSession.save();
55-
res.status(201).json({ message: 'Session saved successfully' });
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);
5634
} catch (error) {
57-
res.status(500).json({ error: 'Failed to save session data' });
35+
res.status(500).json({ error: error.message });
5836
}
59-
});
37+
});
6038

6139
module.exports = historyRouter;

Backend/HistoryService/models/Histories.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ const HistorySchema = new mongoose.Schema({
2525
required: true
2626
},
2727
code: {
28-
type: Buffer, // Store the code as binary file
29-
required: true
28+
type: Buffer, // Store the code as binary file
29+
required: true
3030
}
3131
});
3232

0 commit comments

Comments
 (0)