-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
296 lines (246 loc) · 8.34 KB
/
server.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
require('dotenv').config();
const https = require('https');
const fs = require('fs');
const express = require('express');
const cors = require('cors');
const mysql = require('mysql2');
const qr = require('qr-image');
const jwt = require('jsonwebtoken');
const app = express();
app.use(cors());
app.use(express.json());
const sslOptions = {
key: fs.readFileSync(process.env.SSL_KEY_FILE),
cert: fs.readFileSync(process.env.SSL_CRT_FILE)
};
const server = https.createServer(sslOptions, app);
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'dell123',
database: 'college'
});
db.connect(err => {
if (err) {
console.error('Error connecting to database:', err);
} else {
console.log('Connected to database');
}
});
server.listen(5000, '0.0.0.0', () => {
console.log('Server is running on port 5000');
});
app.get('/student-attendance/:registrationNo', (req, res) => {
const studentRegistrationNo = req.params.registrationNo;
const { date, month, subject } = req.query; // Use req.query to access query parameters
console.log('date:', date ,'month:',month, 'subject',subject);
let query = `SELECT * FROM scanned_qr WHERE registrationNo = ?`;
const queryParams = [studentRegistrationNo];
if (date) {
query += ` AND DATE(ScannedTime) = ?`
queryParams.push(date);
}
if (month) {
query += ` AND YEAR(ScannedTime) = ? AND MONTH(ScannedTime) = ?`;
const [year, monthValue] = month.split('-');
queryParams.push(year, monthValue);
}
if (subject) {
query += ` AND subject = ?`;
queryParams.push(subject);
}
db.query(query, queryParams, (error, results) => {
if (error) {
console.error('Error fetching student attendance:', error);
res.status(500).json({ error: 'Internal server error' });
} else {
//console.log('studentAtenndanceQuery',query);
res.json(results);
}
});
});
app.get('/api/scanned-qr',(req, res)=>{
const query = 'SELECT * FROM scanned_qr';
db.query(query, (err, results) => {
if (err) {
res.status(500).json({ error: err.message });
} else {
res.status(200).json(results);
}
});
});
app.get('/api/getRegistrationNosWithNames',(req,res)=>{
const query = 'SELECT * FROM students';
db.query(query, (err, results) => {
if (err) {
res.status(500).json({ error: err.message });
} else {
// console.log(results);
res.status(200).json(results);
}
});
});
// API to fetch attendance records based on filters
app.post('/api/getAttendance', (req, res) => {
const { registrationNo, subject, year, month, date } = req.body;
let query = `SELECT * FROM scanned_qr WHERE 1`;
if (registrationNo) {
query += ` AND registrationNo = '${registrationNo}'`;
}
if (subject) {
query += ` AND subject = '${subject}'`;
}
if (year) {
query += ` AND YEAR(ScannedTime) = ${year}`;
}
if (month) {
query += ` AND MONTH(ScannedTime) = ${month}`;
}
if (date) {
query += ` AND DATE(ScannedTime) = ?`; // Use parameter placeholder
}
db.query(query, [date], (err, results) => {
if (err) {
console.error('Error fetching attendance:', err);
res.status(500).json({ error: 'Error fetching attendance' });
return;
}
console.log('Attendance query:', query); // Add this line
console.log(results);
res.json(results);
});
});
app.post('/api/student/login', (req, res) => {
const { registrationNo, password } = req.body;
const selectQuery = 'SELECT * FROM students WHERE RegNo = ?';
db.query(selectQuery, [registrationNo], (err, results) => {
if (err) {
res.status(500).json({ error: err.message });
return;
}
if (results.length === 0 || password !== results[0].Password) {
res.status(401).json({ message: 'Invalid credentials' });
return;
}
// Store user's session data
// Generate and sign a JWT token
const token = jwt.sign({ registrationNo }, process.env.JWT_SECRET, { expiresIn: '1h' });
// Send the token as response
res.json({ message: 'Logged in successfully', token });
});
});
// Authentication for teacher login
app.post('/api/teacher/login', (req, res) => {
const { username, password } = req.body;
const selectQuery = 'SELECT * FROM teachers WHERE username = ?';
db.query(selectQuery, [username], (err, results) => {
if (err) {
res.status(500).json({ error: err.message });
return;
}
if (results.length === 0 || password !== results[0].password) {
res.status(401).json({ message: 'Invalid credentials' });
return;
}
// Generate and sign a JWT token
const token = jwt.sign({ username }, process.env.JWT_SECRET, { expiresIn: '1h' });
// Send the token as response
res.json({ message: 'Logged in successfully', token });
});
});
// Middleware to verify JWT token
function verifyToken(req, res, next) {
const token = req.headers['authorization'];
if (!token) {
return res.status(403).json({ message: 'No token provided' });
}
jwt.verify(token, process.env.JWT_SECRET, (err, decoded) => {
if (err) {
return res.status(401).json({ message: 'Invalid token' });
}
req.username = decoded.username;
next();
});
}
// Route to check session validity
app.get('/api/check-session', verifyToken, (req, res) => {
res.status(200).json({ message: 'Session active' });
});
app.get('/api/data', (req, res) => {
const query = 'SELECT * FROM student';
db.query(query, (err, results) => {
if (err) {
res.status(500).json({ error: err.message });
} else {
res.status(200).json(results);
}
});
});
app.post('/api/save-scanned-data', (req, res) => {
const { registrationNo, subject, date } = req.body;
// Check if the same data already exists in the database
const checkQuery = `
SELECT * FROM scanned_qr
WHERE registrationNo = ? AND subject = ? AND date = ?
`;
db.query(checkQuery, [registrationNo, subject, date], (err, results) => {
if (err) {
console.error('Error checking data:', err);
res.status(500).json({ error: 'An error occurred while checking data' });
return;
}
if (results.length > 0) {
res.status(409).json({ message: 'Data already exists' });
return;
} else {
// Data doesn't exist, proceed to insert
const insertQuery = `
INSERT INTO scanned_qr (registrationNo, subject, date)
VALUES (?, ?, ?)
`;
db.query(insertQuery, [registrationNo, subject, date], (err, result) => {
if (err) {
console.error('Error inserting data:', err);
res.status(500).json({ error: 'An error occurred while saving data' });
return;
}
console.log('Data inserted into MySQL table');
res.json({ message: 'Data saved successfully' });
});
}
});
});
// Add this endpoint to your existing server code
app.post('/api/addStudent', (req, res) => {
const { name, course, dob, address, mobileNo } = req.body;
// Insert the employee data into the database
const insertQuery = 'INSERT INTO student (name, course, dob, address ,mobileNo) VALUES (?, ?, ?, ?, ?)';
db.query(insertQuery, [name, course, dob, address,mobileNo], (err, result) => {
if (err) {
res.status(500).json({ error: err.message });
} else {
res.status(200).json({ message: 'Employee data added successfully' });
}
});
});
app.post('/api/generate-qr/:regno', (req, res) => {
const regno = req.params.regno;
const qrCodeData = req.body.qrCodeData;
const qrCode = qr.image(qrCodeData, { type: 'png' });
const imageBuffer = [];
qrCode.on('data', chunk => {
imageBuffer.push(chunk);
});
qrCode.on('end', () => {
const qrCodeImageData = Buffer.concat(imageBuffer).toString('base64');
const updateQuery = 'UPDATE student SET qr_code = ? WHERE regno = ?';
db.query(updateQuery, [qrCodeImageData, regno], (err, result) => {
if (err) {
res.status(500).json({ error: err.message });
} else {
const qrCodeDataURL = `data:image/png;base64,${qrCodeImageData}`;
res.status(200).json({ qrCodeDataURL });
}
});
});
});