-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
290 lines (253 loc) · 8.44 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
var express = require("express");
var session = require("express-session");
var mysql = require('mysql');
// Creating mysql connection
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'parkingMS',
multipleStatements: true,
timezone: 'utc'
});
connection.connect(function (err) {
if (!err) {
console.log("Database is connected ... \n\n");
} else {
console.log("Error connecting database ... \n\n");
// throw err;
}
});
var app = express();
// Start Server
app.listen(8888, function () {
console.log("Started on PORT 8888: http://localhost:8888");
});
// To read post data from client
// - parse json encoded bodies
app.use(express.json());
// - parse application/x-www-form-urlencoded
app.use(express.urlencoded({
extended: true
}));
// Secret key is a random string and visible for the project purpose
app.use(session({ secret: "nd89yer897e89rfhsdfbn", resave: false, saveUninitialized: true }));
// To serve static files in 'public' folder with '/static' endpoint
app.use('/static', express.static(__dirname + '/app/public'));
/* Routes */
// Home
app.get('/', function (req, res) {
if (req.session.user) {
res.redirect('/dashboard');
} else {
res.sendFile('login.html', { root: __dirname + '/app' });
}
});
// Dashboard
app.get('/dashboard', function (req, res) {
if (!req.session.user) {
return res.status(401).send('Unauthorized');
} else {
return res.status(200).sendFile('app.html', { root: __dirname + '/app' });
}
});
/* API */
// Login
app.post('/api/login', function (req, res) {
console.log(req.body);
if (req.body.username == "admin" && req.body.password == "admin") {
req.session.user = req.body.username;
res.redirect('/dashboard');
} else {
res.redirect('back'); // Back 1 page
}
});
// Logout
app.get('/api/logout', function (req, res) {
req.session.destroy();
res.redirect('/');
});
// Vehicle Entry
app.post('/api/vehicleEntry', function (req, res) {
if (!req.session.user) {
return res.status(401).send('Unauthorized');
}
// Check if this customer or vehicle exists in the DB
var customerExists = "SELECT COUNT(*) FROM CUSTOMER WHERE PHONE = \"" + req.body.phone + "\";";
var vehicleExists = "SELECT COUNT(*) FROM VEHICLE WHERE REGISTRATION = \"" + req.body.registration + "\";";
connection.query(customerExists + vehicleExists, function (err, result) {
if (err) {
return res.sendStatus(500);
}
// If customer record not found in the DB, add it
if (result[0][0]['COUNT(*)'] == 0) {
var newCustomerSQL = "INSERT INTO CUSTOMER VALUES ('" +
req.body.cusFName + "', '" +
req.body.cusLName + "', " +
req.body.phone + ");";
connection.query(newCustomerSQL, function (err, result) {
if (err) {
console.log(err);
return res.sendStatus(500);
}
console.log("New customer added");
});
}
// If Vehicle record not found in the DB, add it
if (result[1][0]['COUNT(*)'] == 0) {
var newVehicleSQL = "INSERT INTO VEHICLE VALUES (\"" +
req.body.registration + "\", \"" +
req.body.brand + "\", \"" +
req.body.model + "\", \"" +
req.body.color + "\", " +
req.body.type + ", " +
req.body.phone + ")";
connection.query(newVehicleSQL, function (err, result) {
if (err) {
console.log(err);
return res.sendStatus(500);
}
console.log("New vehicle added");
});
}
// Query available parking slots
connection.query("SELECT * FROM `PARKING_SPACE` WHERE `SLOT_STATUS` = 'AV'", function (err, result) {
if (err) {
return res.sendStatus(500);
}
// If a parking slot is available, generate a new token and occupy a parking slot
if (result[0]) {
var parkingArea = result[0].AREA;
var parkingSlot = result[0].SLOT_NUMBER;
var entryTime = new Date().toISOString().slice(0, 19).replace('T', ' ');
var billAmount = 20; // base charge
var newTokenSQL = "INSERT INTO TOKEN (ENTRY_TIME, BILL_AMOUNT, VEHICLE_REG, PARKING_AREA, PARKING_SLOT) VALUES(\"" +
entryTime + "\", " +
billAmount + ", \"" +
req.body.registration + "\", \"" +
parkingArea + "\", " +
parkingSlot + ");";
var occupyParkingSlotSQL = "UPDATE `PARKING_SPACE` SET `SLOT_STATUS` = 'OC' WHERE `AREA` = '" +
parkingArea + "' AND `SLOT_NUMBER` = " +
parkingSlot + ";";
connection.query(newTokenSQL + occupyParkingSlotSQL, function (err, result) {
if (err) {
console.log(err);
return res.sendStatus(500);
}
console.log("token number " + result[0].insertId + " generated for " + req.body.registration + " and parked at " + parkingArea + parkingSlot);
res.status(200).json({number: result[0].insertId, area: parkingArea, slot: parkingSlot});
});
} else {
return res.status(500).send("Parking Full!");
}
});
});
});
// View Vehicles
app.get('/api/viewParkedVehicles', function (req, res) {
if (!req.session.user) {
return res.status(401).send('Unauthorized');
}
var viewParkedVehiclesSQL = "SELECT `NUMBER`, `ENTRY_TIME`, `VEHICLE_REG`, `PARKING_AREA`, `PARKING_SLOT` FROM `TOKEN` WHERE `EXIT_TIME` IS NULL ORDER BY `NUMBER`";
connection.query(viewParkedVehiclesSQL, function (err, result) {
if (err) {
console.log("Error retrieving parked vehicles: " + err.sqlMessage);
return res.sendStatus(500);
}
res.status(200).json(result);
});
});
// Vehicle Exit
app.post('/api/vehicleExit', function (req, res) {
if (!req.session.user) {
return res.status(401).send('Unauthorized');
}
// Query parking slot to be cleared
connection.query("SELECT `PARKING_AREA`, `PARKING_SLOT` FROM `TOKEN` WHERE `NUMBER` = ?", req.body.token, function (err, result) {
if (err) {
return res.sendStatus(500);
}
var parkingArea = result[0].PARKING_AREA;
var parkingSlot = result[0].PARKING_SLOT;
var exitTime = new Date().toISOString().slice(0, 19).replace('T', ' ');
var exitTokenUpdateSQL = "UPDATE `TOKEN` SET `EXIT_TIME` = '" +
exitTime + "' WHERE `NUMBER` = " + req.body.token + ";";
var clearParkingSlotSQL = "UPDATE `PARKING_SPACE` SET `SLOT_STATUS` = 'AV' WHERE `AREA` = '" +
parkingArea + "' AND `SLOT_NUMBER` = " +
parkingSlot + ";";
var getBillAmountSQL = "SELECT `BILL_AMOUNT` FROM `TOKEN` WHERE `NUMBER` = " + req.body.token + ";";
connection.query(exitTokenUpdateSQL + clearParkingSlotSQL + getBillAmountSQL, function (err, result) {
if (err) {
console.log("Error exiting vehicle: " + err.sqlMessage);
return res.sendStatus(500);
}
res.status(200).json({ 'billAmount': result[2][0].BILL_AMOUNT });
});
});
});
// Stats - Parking Space Status
app.get('/api/viewParkingSpace', function (req, res) {
if (!req.session.user) {
return res.status(401).send('Unauthorized');
}
connection.query("SELECT * FROM `PARKING_SPACE` ORDER BY `SLOT_STATUS`, `AREA`, `SLOT_NUMBER`", function (err, result) {
if (err) {
return res.sendStatus(500);
}
res.status(200).json(result);
});
});
// Manage - Employee Addition
app.post('/api/empAdd', function (req, res) {
if (!req.session.user) {
return res.status(401).send('Unauthorized');
}
var newEmployeeSQL = "INSERT INTO EMPLOYEE VALUES (" +
req.body.ssn + ", '" +
req.body.firstName + "', '" +
req.body.lastName + "', '" +
req.body.sex + "', " +
req.body.phone + ");";
connection.query(newEmployeeSQL, function (err, result) {
if (err) {
console.log("Error adding employee: " + err.sqlMessage);
return res.sendStatus(500);
}
console.log("employee added");
res.sendStatus(200);
});
});
// Manage - Get employee list
app.get('/api/getEmployeeList', function (req, res) {
if (!req.session.user) {
return res.status(401).send('Unauthorized');
}
var getEmployeeListSQL = "SELECT `SSN`, `FIRST_NAME`, `LAST_NAME` FROM `EMPLOYEE` ORDER BY `SSN`";
connection.query(getEmployeeListSQL, function (err, result) {
if (err) {
console.log("Error getting employee list : " + err.sqlMessage);
return res.sendStatus(500);
}
return res.status(200).json(result);
});
});
// Manage - Parking Space Addition
app.post('/api/parkingSpaceAdd', function (req, res) {
if (!req.session.user) {
return res.status(401).send('Unauthorized');
}
var parkingSpaceSQL = "";
for (var i = 1; i <= req.body.slots; i++) {
parkingSpaceSQL += "INSERT INTO `PARKING_SPACE` VALUES ('" +
req.body.area + "', " + i + ", 'AV', " +
req.body.ssn + ");";
}
connection.query(parkingSpaceSQL, function (err, result) {
if (err) {
console.log("Error adding parking slot " + req.body.area + " : " + err.sqlMessage);
return res.sendStatus(500);
}
return res.sendStatus(200);
});
});