|
1 | 1 | const historyRouter = require('express').Router();
|
2 |
| -const HistoryModel = require('../models/Histories'); |
| 2 | +const HistoryModel = require('../models/History'); |
3 | 3 |
|
4 | 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 |
| 5 | +historyRouter.get("/", async (req, res) => { |
| 6 | + const userId = req.user.id; // Assuming you're using authentication middleware |
7 | 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 |
| -// Assuming Express.js |
20 |
| -historyRouter.get('/user/:userId', async (req, res) => { |
21 |
| - const { userId } = req.params; |
22 | 8 | 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); |
25 | 14 | } catch (error) {
|
26 |
| - res.status(500).json({ error: 'Failed to fetch attempt history' }); |
| 15 | + res.status(500).json({ error: error.message }); |
27 | 16 | }
|
28 | 17 | });
|
29 | 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; |
30 | 22 |
|
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) => { |
52 | 23 | 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); |
56 | 34 | } catch (error) {
|
57 |
| - res.status(500).json({ error: 'Failed to save session data' }); |
| 35 | + res.status(500).json({ error: error.message }); |
58 | 36 | }
|
59 |
| -}); |
| 37 | +}); |
60 | 38 |
|
61 | 39 | module.exports = historyRouter;
|
0 commit comments