|
1 | 1 | const historyRouter = require('express').Router();
|
2 |
| -const HistoryModel = require('../models/History'); |
| 2 | +const HistoryModel = require('../models/Histories'); |
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; |
8 | 22 | 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); |
14 | 25 | } catch (error) {
|
15 |
| - res.status(500).json({ error: error.message }); |
| 26 | + res.status(500).json({ error: 'Failed to fetch attempt history' }); |
16 | 27 | }
|
17 | 28 | });
|
18 | 29 |
|
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 | 30 |
|
| 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) => { |
23 | 52 | 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' }); |
34 | 56 | } catch (error) {
|
35 |
| - res.status(500).json({ error: error.message }); |
| 57 | + res.status(500).json({ error: 'Failed to save session data' }); |
36 | 58 | }
|
37 |
| -}); |
| 59 | +}); |
38 | 60 |
|
39 | 61 | module.exports = historyRouter;
|
0 commit comments