-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
466 lines (373 loc) · 14 KB
/
index.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
/*
* Projektaufgabe, Abgabeterim: 21.09.2020, Geosoft 1, SoSe 2020
* @author {Timo Lietmeyer} matr.Nr.: {459169}
* @author {Judith Bresser} matr.Nr.: {459956}
*/
//**various jshint configs**
// jshint esversion: 8
// jshint browser: true
// jshint node: true
// jshint -W117
// jshint -W083
"use strict";
const express = require('express');
const mongodb = require('mongodb');
const port = 3000;
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
/**
* function which creates a Connection to MongoDB. Retries every 3 seconds if not connection could be established.
*/
async function connectMongoDB() {
try {
//Connect to database server
try
{
app.locals.dbConnection = await mongodb.MongoClient.connect("mongodb://localhost:27017", {useNewUrlParser: true});
}
catch(error)
{
app.locals.dbConnection = await mongodb.MongoClient.connect("mongodb://mongodbservice:27017", {useNewUrlParser: true});
}
//connect do database "MainDB"
app.locals.db = await app.locals.dbConnection.db("MainDB");
console.log("Using db: " + app.locals.db.databaseName);
app.locals.db.collection("User").drop((err, delOK) => { if (delOK) console.log("collection User cleared") });
app.locals.db.collection("all_busstops_and_departures").drop((err, delOK) => { if (delOK) console.log("collection all_busstops_and_departures cleared") });
app.locals.db.collection("logged_User").drop((err, delOK) => { if (delOK) console.log("collection logged_User cleared") });
app.locals.db.collection("selected_departures").drop((err, delOK) => { if (delOK) console.log("collection selected departures cleared") });
}
catch (error) {
console.dir(error)
setTimeout(connectMongoDb, 3000)
}
}
//Start connecting
connectMongoDB()
//Make all Files stored in Folder "__dirname" accessible over localhost:3000/src
app.use('/src', express.static(__dirname + ''));
//Make Leaflet accessible over localhost:3000/leaflet
app.use('/leaflet', express.static(__dirname + '/node_modules/leaflet/dist'));
//Make jquery accessible over localhost:3000/jquery
app.use('/jquery', express.static(__dirname + '/node_modules/jquery/dist'));
//Make bootstrap accessible voer localhost:3000/bootstrap
app.use('/bootstrap', express.static(__dirname + '/node_modules/bootstrap/dist'));
//Make leafleatheat accessible voer localhost:3000/leaflet-heap
app.use('/leaflet-heat', express.static(__dirname + '/node_modules/leaflet.heat/dist'));
//Make leafleatheat accessible voer localhost:3000/leaflet-draw
app.use('/leaflet-draw', express.static(__dirname + '/node_modules/leaflet-draw/dist'));
//Send index.html on request to "/"
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index_Login.html')
})
//Send index_Doc.html on request to "/index_Doc"
app.get('/index_Doc', (req, res) => {
res.sendFile(__dirname + '/index_Doc.html')
})
//Send index_Login.html on request to "/index_Login"
app.get('/index_Login', (req, res) => {
res.sendFile(__dirname + '/index_Login.html')
})
//Send index_Login.html on request to "/index_A"
app.get('/index_A', (req, res) => {
res.sendFile(__dirname + '/index_A.html')
})
//Send index_Login.html on request to "/index_B"
app.get('/index_B', (req, res) => {
res.sendFile(__dirname + '/index_B.html')
})
//Send index_Login.html on request to "/index_B"
app.get('/test', (req, res) => {
res.sendFile(__dirname + '/index_Test.html')
})
// listen on port 3000
app.listen(port,
() => console.log(`HTML Site listening at http://localhost:${port}`)
)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////USER Functions////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
*@desc insert a User in Collection "User"
*@param req.query = Object with Parameters
*
*
*
*/
app.post("/User", (req, res) => {
// insert User
console.log("insert User " + JSON.stringify(req.body));
app.locals.db.collection('User').insertOne(req.body, (error, result) => {
if (error) {
console.dir(error);
}
res.json(result);
});
});
/**
*@desc insert one User in Collection "logged_User"
*@param req.query = Object with Parameters
*
*
*
*/
app.post("/logged_User", (req, res) => {
// insert User
app.locals.db.collection('logged_User').insertOne(req.body, (error, result) => {
if (error) {
console.dir(error);
}
res.json(result);
});
});
/**
*@desc search for a User by Users Password and Username, if they found no User with Password and Username they request all Users
*in Collection "User"
*@param req.query = Object with Parameters
*
*
*
*/
app.get("/User", (req, res) => {
if (req.query.Username != undefined && req.query.Password != undefined) {
app.locals.db.collection('User').find({Username:req.query.Username, Password:req.query.Password}).toArray((error, result) => {
// console.log("gefunden");
if (error) {
console.dir(error);
}
res.json(result);
});
}
else {
app.locals.db.collection('User').find().toArray((error, result) => {
if (error) {
console.dir(error);
}
res.json(result);
});
}
});
/**
*@desc search for a User by UserID in Collection "User"
*@param req.query = Object with Parameters
*
*
*
*/
app.get("/search_by_UserID", (req, res) => {
if (req.query.UserID != undefined) {
app.locals.db.collection('User').find({ userID: req.query.UserID }).toArray((error, result) => {
if (error) {
console.dir(error);
}
res.json(result);
});
}
});
/**
*@desc request the logged User from Collection "logged_User"
*
*/
app.get("/logged_User", (req, res) => {
app.locals.db.collection('logged_User').find().toArray((error, result) => {
if (error) {
console.dir(error);
}
res.json(result);
});
});
/**
*@desc deleted the logged User from Collection "logged_User"
*
*/
app.delete("/logged_User", (req, res) => {
app.locals.db.collection('logged_User').deleteOne(req.body, (error, result) => {
if (error) {
console.dir(error);
}
res.json(result);
});
});
/**
*@desc request all Users from Collection "Users" as Array
*
*/
app.get("/User", (req, res) => {
app.locals.db.collection('User').find().toArray((error, result) => {
if (error) {
console.dir(error);
}
res.json(result);
});
});
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////Selected Stops/////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
*@desc add selected Departure to Collection "selected_departures" with Stop ID, User ID and Departure ID
*@param req.query = Object with Parameters
*
*
*
*/
app.post("/selected_departures", (req, res) => {
app.locals.db.collection('selected_departures')
.find({ departure_id: req.body.departure_id, stop_id: req.body.stop_id, user: req.body.user }).toArray((error, result) => {
if (error) {
console.dir(error);
}
if (result.length==0){
app.locals.db.collection('selected_departures').insertOne(req.body, (error, result) => {
if (error) {
console.dir(error);
}
res.json(result);
});
}
})
});
/**
*@desc search at Collection "selected_departures" by UserID and request all taken departures from User with this UserID,
*if they are no departures with this UserID, they request all selected departures
*@param req.query = Object with Parameters
*
*
*
*/
app.get("/selected_departures", (req, res) => {
if (req.query != undefined) {
app.locals.db.collection('selected_departures').find({ user: req.query.id }).toArray((error, result) => {
if (error) {
console.dir(error);
}
res.json(result);
});
}
else {
app.locals.db.collection('selected_departures').find().toArray((error, result) => {
if (error) {
console.dir(error);
}
res.json(result);
});
//console.log("else");
}
});
/**
*@desc update at Collection "selected_departures" by StopID and DepartureID the selected Values with infections_risk, begin_time and end_time,
*if they are no departures with this UserID, they request all selected departures
*@param req.query = Object with identifier Parameters
*@param newvalues = Object with to new Parameters
*
*
*
*/
app.put("/selected_departures", (req, res) => {
req.query = {
Stopid: req.body.StopID,
DepID: req.body.DepID
}
var newvalues = {
$set: {
infection_risk: req.body.infection_risk,
begin_time: req.body.begin_time,
end_time: req.body.end_time
}
}
app.locals.db.collection('selected_departures').updateMany({ departure_id: req.query.DepID, stop_id: req.query.Stopid }, newvalues, function (err, res) {
if (err) throw err;
console.log("all selected Departures Updatet");
});
});
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////All Busstops and Departures///////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
*@desc add all Departure to Collection "all_busstops_and_departures" only when data not exist
*@param req.query = Object with Parameters
*
*
*
*/
app.post("/all_busstops_and_departures", (req, res) => {
console.log("POST");
app.locals.db.collection('all_busstops_and_departures')
.find({name: req.body.name, id: req.body.id, departures: req.body.departures}).toArray((error, result) => {
if (error) {
console.dir(error);
}
if (result.length==0){
console.log("haltestelle nicht gefunden");
app.locals.db.collection('all_busstops_and_departures').insertOne(req.body, (error, result) => {
if (error) {
console.dir(error);
}
res.json(result);
});
} else {console.log("haltestelle gefunden");}
})
});
/**
*@desc search one Stop by StopID
*@param req.query = Object with Parameters
*
*
*
*/
app.get("/all_busstops_and_departures", (req, res) => {
if (req.query != undefined) {
app.locals.db.collection('all_busstops_and_departures').find({ id: req.query.id }).toArray((error, result) => {
if (error) {
console.dir(error);
}
res.json(result);
});
}
else {
app.locals.db.collection('all_busstops_and_departures').find().toArray((error, result) => {
if (error) {
console.dir(error);
}
res.json(result);
});
}
});
/**
*@desc Check if Collection "all_busstops_and_departures" is Empty and request True or False
*
*
*
*/
app.get("/is_empty_all_busstops_and_departures", (req, res) => {
app.locals.db.collection("all_busstops_and_departures").count(function (err, count) {
if (!err && count === 0) {
result = true;
}
else {
result = false;
}
return res.json(result);
});
});