-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
578 lines (499 loc) · 19 KB
/
app.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
const express = require('express');
const path=require('path')
const mysql = require('mysql2');
const bodyParser = require('body-parser');
const session = require('express-session');
const bcrypt = require('bcrypt');
const crypto = require('crypto');
const app = express();
const port = 3000;
//create connection pool
const pool =mysql.createPool({
host: 'localhost',
user: 'root',
password: 'chiru',
database: 'prs'
});
app.use(bodyParser.urlencoded({ extended: true }));
// Generate a secure secret key
const secretKey = crypto.randomBytes(32).toString('hex');
// Configure session middleware
app.use(session({
secret: secretKey,
resave: false,
saveUninitialized: true,
}));
app.set("view engine","ejs");
app.set('views',path.join(__dirname,'html'));
// Serve static files
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
// Fetch the slots data from the database
pool.query('SELECT * FROM Slots', (err, results) => {
if (err) {
console.error('Error retrieving slots data from the database:', err);
// Handle the error condition and send an appropriate response
res.status(500).send('Internal Server Error');
return;
}
// Generate the HTML for the slot list dynamically
const slotListHtml = generateSlotListHtml(results);
// Render the index view and pass the slotListHtml to it
res.render('index', { slotListHtml });
});
});
function generateSlotListHtml(slots) {
let html = '<table style="border: 1px solid black; padding: 5px;">';
html += '<tr><th style="border: 1px solid black; padding: 5px;">Slot ID</th><th style="border: 1px solid black; padding: 5px;">Slot Number</th><th style="border: 1px solid black; padding: 5px;">Vehicle Type</th><th style="border: 1px solid black; padding: 5px;">Price per Hour</th><th style="border: 1px solid black; padding: 5px;">Availability</th><th style="border: 1px solid black; padding: 5px;">Details</th></tr>';
slots.forEach((slot) => {
html += `<tr>
<td style="border: 1px solid black; padding: 5px;">${slot.sid}</td>
<td style="border: 1px solid black; padding: 5px;">${slot.sno}</td>
<td style="border: 1px solid black; padding: 5px;">${slot.vehicle_type}</td>
<td style="border: 1px solid black; padding: 5px;">${slot.price_per_hour}</td>
<td style="border: 1px solid black; padding: 5px;">${slot.is_available ? 'Available' : 'Not Available'}</td>
<td style="border: 1px solid black; padding: 5px;">${slot.details}</td>
</tr>`;
});
html += '</table>';
return html;
}
// Route for the new user registration page
app.get('/new', (req, res) => {
res.render('new');
});
app.post('/register', (req, res) => {
// Extract the form data from the request body
const { name, uname, pass, email } = req.body;
// Create a new user object with the extracted data
const user = {
uname: uname,
upass: pass,
uemail: email,
name: name
};
// Insert the user object into the user table in the database
pool.query('INSERT INTO user SET ?', user, (err, result) => {
if (err) {
console.error('Error inserting user into the database:', err);
// Handle the error condition and send an appropriate response
res.status(500).send('Internal Server Error');
return;
}
// User successfully inserted, display a pop-up box or alert
const message = 'Registration successful!'; // Change the message as desired
res.send(`<script>alert('${message}'); window.location.href='/';</script>`);
});
});
app.get('/user', (req, res) => {
// Handle the user login page rendering
res.render('user');
});
// Route for the admin login page
app.get('/admin', (req, res) => {
// Handle the admin login page rendering
res.render('admin');
});
app.post('/admin/login', (req, res) => {
// Extract the username and password from the request body
const { username, password } = req.body;
pool.query('SELECT * FROM admin WHERE aname = ? AND apass = ?', [username, password], (err, results) => {
if (err) {
console.error('Error retrieving admin data from the database:', err);
// Handle the error condition and send an appropriate response
res.status(500).send('Internal Server Error');
return;
}
if (results.length === 1) {
// Authentication successful
res.render('admindashboard');
} else {
// Authentication failed
res.send('Admin login failed!');
}
});
});
app.get('/about', (req, res) => {
// Fetch the reviews data from the database
pool.query('SELECT * FROM reviews', (err, results) => {
if (err) {
console.error('Error retrieving reviews data from the database:', err);
// Handle the error condition and send an appropriate response
res.status(500).send('Internal Server Error');
return;
}
// Generate the HTML for the review list dynamically
const reviewListHtml = generateReviewListHtml(results);
// Render the about view and pass the reviewListHtml to it
res.render('about', { reviewListHtml });
});
});
function generateReviewListHtml(reviews) {
let html = '';
reviews.forEach((review) => {
html += `<div class="review-item">
<h4>Rating: ${review.rating}</h4>
<p>${review.message}</p>
<p>Posted by: ${review.name}</p>
<p>Date: ${review.date}</p>
</div>`;
});
return html;
}
// admindashboard
// Route to render the admin dashboard
const router = express.Router();
// Route handler for the dashboard
// Export the router
module.exports = router;
// Handle adding a new admin
app.post('/admin/add', (req, res) => {
const { adminName, adminPassword } = req.body;
// Perform the necessary logic to add a new admin to the database
const sql = 'INSERT INTO Admin (aname, apass) VALUES (?, ?)';
const values = [adminName, adminPassword];
// Execute the SQL query to insert the new admin
pool.query(sql, values, (error, result) => {
if (error) {
// Handle the error
console.error(error);
res.send('Error adding admin');
} else {
// Redirect to the admin dashboard or show a success message
res.redirect('/admindashboard');
// res.redirect('/admin_dashboard');
}
});
});
// Handle deleting a user
app.post('/admin/delete/user', (req, res) => {
const { userId } = req.body;
// Perform the necessary logic to delete the user from the database
const sql = 'DELETE FROM User WHERE uid = ?';
const values = [userId];
// Execute the SQL query to delete the user
pool.query(sql, values, (error, result) => {
if (error) {
// Handle the error
console.error(error);
res.send('Error deleting user');
} else {
// Redirect to the admin dashboard or show a success message
res.redirect('/admindashboard');
}
});
});
// Handle adding a new slot
app.post('/admin/add/slot', (req, res) => {
const { slotNumber, vehicleType, pricePerHour, isAvailable, slotDetails } = req.body;
// Perform the necessary logic to add a new slot to the database
const sql = 'INSERT INTO Slots (sno, vehicle_type, price_per_hour, is_available, details) VALUES (?, ?, ?, ?, ?)';
const values = [slotNumber, vehicleType, pricePerHour, isAvailable, slotDetails];
// Execute the SQL query to insert the new slot
pool.query(sql, values, (error, result) => {
if (error) {
// Handle the error
console.error(error);
res.send('Error adding slot');
} else {
// Redirect to the admin dashboard or show a success message
res.redirect('/admindashboard');
}
});
});
// Handle deleting a slot
app.post('/admin/delete/slot', (req, res) => {
const { slotId } = req.body;
// Perform the necessary logic to delete the slot from the database
const sql = 'DELETE FROM Slots WHERE sid = ?';
const values = [slotId];
// Execute the SQL query to delete the slot
pool.query(sql, values, (error, result) => {
if (error) {
// Handle the error
console.error(error);
res.send('Error deleting slot');
} else {
// Redirect to the admin dashboard or show a success message
res.redirect('/admindashboard');
}
});
});
// Handle updating pricing
app.post('/admin/update/pricing', (req, res) => {
const { slotId, newPricePerHour } = req.body;
// Perform the necessary logic to update the pricing of a slot in the database
const sql = 'UPDATE Slots SET price_per_hour = ? WHERE sid = ?';
const values = [newPricePerHour, slotId];
// Execute the SQL query to update the pricing
pool.query(sql, values, (error, result) => {
if (error) {
// Handle the error
console.error(error);
res.send('Error updating pricing');
} else {
// Redirect to the admin dashboard or show a success message
res.redirect('/admindashboard');
}
});
});
// Handle canceling a reservation
app.post('/admin/cancel/reservation', (req, res) => {
const { reservationId } = req.body;
// Perform the necessary logic to cancel the reservation in the database
const sql = 'UPDATE Reservations SET cancel_status = true WHERE resid = ?';
const values = [reservationId];
// Execute the SQL query to update the cancellation status
pool.query(sql, values, (error, result) => {
if (error) {
// Handle the error
console.error(error);
res.send('Error canceling reservation');
} else {
// Redirect to the admin dashboard or show a success message
res.redirect('/admindashboard');
}
});
});
// admindashboard end
app.get('/admindashboard',(req,res)=>{
res.render('admindashboard');
});
app.get('/rate',(req,res)=>{
res.render('rate');
});
app.post('/rate/send', (req, res) => {
// Extract the rating, message, and name from the request body
const { rating, message, name } = req.body;
// Insert the review into the 'reviews' table
pool.query(
'INSERT INTO reviews (rating, message, date, name) VALUES (?, ?, CURDATE(), ?)',
[rating, message, name],
(error, results) => {
if (error) {
// Handle the database error
console.error(error);
res.send('Error saving the review.');
} else {
// Review saved successfully
res.send('Review saved successfully!');
}
}
);
});
// ...
app.post('/user/login', (req, res) => {
const { username, password } = req.body;
// Perform the necessary logic to validate the user's credentials
pool.query('SELECT * FROM user WHERE uname = ? AND upass = ?', [username, password], (err, results) => {
if (err) {
console.error('Error retrieving user data from the database:', err);
// Handle the error condition and send an appropriate response
res.status(500).send('Internal Server Error');
return;
}
if (results.length === 1) {
// Authentication successful
// Create a session and store the user ID in it
req.session.userId = results[0].uid;
// Redirect the user to the user dashboard
res.redirect('/user/dashboard');
} else {
// Authentication failed
res.send('User login failed!');
}
});
});
app.get('/user/dashboard', (req, res) => {
// Check if the user is logged in by checking the session
if (req.session.userId) {
const user = getUserById(req.session.userId);
// User is logged in, render the user dashboard with the user information
user.then((user) => {
res.render('userdashboard', { user });
}).catch((err) => {
console.error('Error retrieving user data:', err);
res.status(500).send('Internal Server Error');
});
} else {
// User is not logged in, redirect to the user login page
res.redirect('/user');
}
});
function getUserById(userId) {
return new Promise((resolve, reject) => {
pool.query('SELECT * FROM user WHERE uid = ?', [userId], (err, userResults) => {
if (err) {
console.error('Error retrieving user data from the database:', err);
reject(err);
} else {
if (userResults.length === 1) {
const user = userResults[0];
pool.query('SELECT * FROM reservations WHERE userid = ?', [userId], (err, reservationsResults) => {
if (err) {
console.error('Error retrieving reservations data from the database:', err);
reject(err);
} else {
user.reservations = reservationsResults;
resolve(user);
}
});
} else {
reject(new Error('User not found'));
}
}
});
});
}
// Route handler for user logout
app.post('/user/logout', (req, res) => {
req.session.destroy((err) => {
if (err) {
console.error('Error destroying session:', err);
res.status(500).send('Internal Server Error');
return;
}
res.redirect('/');
});
});
app.post('/user/reserve', (req, res) => {
const { slotId, vehiclePlate, reserveDate } = req.body;
const userId = req.session.userId;
// Check if the slot exists
pool.query('SELECT sid FROM slots WHERE sid = ?', [slotId], (err, result) => {
if (err) {
console.error('Error checking slot existence:', err);
// Handle the error condition and send an appropriate response
res.status(500).send('Internal Server Error');
} else if (result.length === 0) {
// Slot not found, reservation unsuccessful
res.send('Reservation unsuccessful. Slot does not exist.');
} else {
// Check if there is a reservation for the slot on the given date
pool.query(
'SELECT resid, cancel_status FROM reservations WHERE slotid = ? AND reserve_date = ?',
[slotId, reserveDate],
(err, result) => {
if (err) {
console.error('Error checking reservation:', err);
// Handle the error condition and send an appropriate response
res.status(500).send('Internal Server Error');
} else if (result.length === 0) {
// No reservation found, allow for reservation
const reservationData = {
userid: userId,
slotid: slotId,
vehicle_plate_number: vehiclePlate,
reserve_date: reserveDate,
cancel_status: 0, // Assuming 0 represents an active reservation
};
pool.query('INSERT INTO reservations SET ?', reservationData, (err, result) => {
if (err) {
console.error('Error making a reservation:', err);
// Handle the error condition and send an appropriate response
res.status(500).send('Internal Server Error');
} else {
// Reservation successful
res.send('Reservation successful!');
}
});
} else {
// Reservation found, check the cancellation status
const cancellationStatus = result[0].cancel_status;
if (cancellationStatus === 1) {
// Reservation was canceled, allow for reservation
const reservationData = {
userid: userId,
slotid: slotId,
vehicle_plate_number: vehiclePlate,
reserve_date: reserveDate,
cancel_status: 0, // Assuming 0 represents an active reservation
};
pool.query('INSERT INTO reservations SET ?', reservationData, (err, result) => {
if (err) {
console.error('Error making a reservation:', err);
// Handle the error condition and send an appropriate response
res.status(500).send('Internal Server Error');
} else {
// Reservation successful
res.send('Reservation successful!');
}
});
} else {
// Reservation is active, reservation unsuccessful
res.send('Reservation unsuccessful. Slot is already reserved on the specified date.');
}
}
}
);
}
});
});
app.post('/user/cancel', (req, res) => {
const { reservationId } = req.body;
// Retrieve the slot ID associated with the canceled reservation
pool.query(
'SELECT slotid FROM reservations WHERE resid = ?',
[reservationId],
(err, result) => {
if (err) {
console.error('Error retrieving slot ID:', err);
// Handle the error condition and send an appropriate response
res.status(500).send('Internal Server Error');
} else {
const slotId = result[0].slotid;
// Perform the necessary logic to cancel a reservation
pool.query(
'UPDATE reservations SET cancel_status = 1 WHERE resid = ?',
[reservationId],
(err, result) => {
if (err) {
console.error('Error canceling a reservation:', err);
// Handle the error condition and send an appropriate response
res.status(500).send('Internal Server Error');
} else {
// Reservation canceled successfully
// Update the availability of the slot in the slots table
pool.query(
'UPDATE slots SET is_available = 1 WHERE sid = ?',
[slotId],
(err, result) => {
if (err) {
console.error('Error updating slot availability:', err);
// Handle the error condition and send an appropriate response
res.status(500).send('Internal Server Error');
} else {
// Slot availability updated successfully
res.send('Reservation canceled!');
}
}
);
}
}
);
}
}
);
});
app.post('/user/add-vehicle', (req, res) => {
const { vehicleType, plateNumber, details } = req.body;
const userId = req.session.userId;
// Perform the necessary logic to add a vehicle
const vehicleData = {
userid: userId,
vehicle_type: vehicleType,
plate_number: plateNumber,
details: details,
};
pool.query('INSERT INTO vehicle SET ?', vehicleData, (err, result) => {
if (err) {
console.error('Error adding a vehicle:', err);
// Handle the error condition and send an appropriate response
res.status(500).send('Internal Server Error');
} else {
// Vehicle added successfully
res.send('Vehicle added successfully!');
}
});
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`))