-
Notifications
You must be signed in to change notification settings - Fork 0
/
individualProjects.js
138 lines (108 loc) · 5.06 KB
/
individualProjects.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const express = require('express');
const mysql = require('mysql2');
const multer = require('multer');
const app = express();
const port = 3000;
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "login_details"
});
app.use(express.static('public'));
app.use(express.json());
// Helper function to identify user type (mentor or mentee)
const identifyUserType = (req) => {
const isMentor = req.headers['user-type'] === 'mentor';
return isMentor ? 'mentor' : 'mentee';
};
// Submit project endpoint
app.post('/submitProject', upload.single('codeFile'), (req, res) => {
const userType = identifyUserType(req); // Identify user type
const { abstract, projectLink } = req.body;
const codeFile = req.file.buffer;
const tableName = `projects_${userType}`; // Determine table based on user type
const insertQuery = `INSERT INTO ${tableName} (project_id, ${userType}_aac_id, abstract, codeFile, projectLink) VALUES (NULL, ?, ?, ?, ?)`;
db.query(insertQuery, [req.headers['user-id'], abstract, codeFile, projectLink], (err, result) => {
if (err) {
console.error('Error submitting project:', err);
res.status(500).json({ error: 'Internal Server Error' });
} else {
const selectQuery = `SELECT * FROM ${tableName} WHERE ${userType}_aac_id = ?`;
db.query(selectQuery, [req.headers['user-id']], (err, results) => {
if (err) {
console.error('Error fetching projects:', err);
res.status(500).json({ error: 'Internal Server Error' });
} else {
// After inserting, send the HTML file again to display the updated projects
res.sendFile(__dirname + '/individualProjects.html');
}
});
}
});
});
app.get('/', (req, res) => {
res.sendFile(__dirname + '/individualProjects.html'); // Replace 'index.html' with your desired file
});
// Fetch projects for a specific user
app.get('/individualProjects.html', (req, res) => {
const userType = identifyUserType(req); // Identify user type
const userId = req.headers['user-id']; // Get user's unique ID from headers
const tableName = `projects_${userType}`; // Determine table based on user type
const selectQuery = `SELECT * FROM ${tableName} WHERE ${userType}_aac_id = ?`;
db.query(selectQuery, [userId], (err, results) => {
if (err) {
console.error('Error fetching projects:', err);
res.status(500).json({ error: 'Internal Server Error' });
} else {
res.status(200).json(results);
}
});
});
// ... (other code remains the same)
// Submit project endpoint
app.post('/submitProject', upload.single('codeFile'), (req, res) => {
const userType = identifyUserType(req); // Identify user type
const { abstract, projectLink } = req.body;
const codeFile = req.file.buffer;
const tableName = `projects_${userType}`; // Determine table based on user type
const insertQuery = `INSERT INTO ${tableName} (${userType}_aac_id, abstract, codeFile, projectLink) VALUES (?, ?, ?, ?)`;
db.query(insertQuery, [req.headers['user-id'], abstract, codeFile, projectLink], (err, result) => {
if (err) {
console.error('Error submitting project:', err);
res.status(500).json({ error: 'Internal Server Error' });
} else {
const selectQuery = `SELECT * FROM ${tableName} WHERE ${userType}_aac_id = ?`;
db.query(selectQuery, [req.headers['user-id']], (err, results) => {
if (err) {
console.error('Error fetching projects:', err);
res.status(500).json({ error: 'Internal Server Error' });
} else {
res.status(200).sendFile(__dirname + '/individualProjects.html');
}
});
}
});
});
// Fetch and display projects for a specific user
app.get('/individualProjects.html', (req, res) => {
const userType = identifyUserType(req); // Identify user type
const userId = req.headers['user-id']; // Get user's unique ID from headers
const tableName = `projects_${userType}`; // Determine table based on user type
const selectQuery = `SELECT * FROM ${tableName} WHERE ${userType}_aac_id = ?`;
db.query(selectQuery, [userId], (err, results) => {
if (err) {
console.error('Error fetching projects:', err);
res.status(500).json({ error: 'Internal Server Error' });
} else {
// Modify this part to render the HTML with the retrieved projects data
res.sendFile(__dirname + '/individualProjects.html');
}
});
});
// ... (rest of the code remains the same)
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});