Skip to content

Commit 39cd9e1

Browse files
committed
Update History Backend (incomplete)
1 parent 2d887d4 commit 39cd9e1

File tree

5 files changed

+10837
-27
lines changed

5 files changed

+10837
-27
lines changed

Backend/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ QuestionService/node_modules
1111
user-service/node_modules
1212
MatchingService/node_modules
1313
CollabService/node_modules
14+
HistoryService/node_modules
1415
/.pnp
1516
.pnp.js
1617

Backend/HistoryService/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mongoose.connect(mongoURI)
1515
.then(() => console.log('MongoDB connected'))
1616
.catch(err => console.error('MongoDB connection error:', err));
1717

18-
app.use('/api/histories', questionRouter)
18+
app.use('/api/histories', historyRouter)
1919

2020
app.get("/", (req, res, next) => {
2121
console.log("Sending Greetings!");
Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,61 @@
11
const historyRouter = require('express').Router();
2-
const HistoryModel = require('../models/History');
2+
const HistoryModel = require('../models/Histories');
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;
822
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);
23+
const attempts = await HistoryModel.find({ user: userId }); // Fetch by user object ID
24+
res.json(attempts);
1425
} catch (error) {
15-
res.status(500).json({ error: error.message });
26+
res.status(500).json({ error: 'Failed to fetch attempt history' });
1627
}
1728
});
1829

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

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) => {
2352
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);
53+
const newSession = new HistoryModel(req.body);
54+
await newSession.save();
55+
res.status(201).json({ message: 'Session saved successfully' });
3456
} catch (error) {
35-
res.status(500).json({ error: error.message });
57+
res.status(500).json({ error: 'Failed to save session data' });
3658
}
37-
});
59+
});
3860

3961
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)