-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.js
94 lines (86 loc) · 2.57 KB
/
start.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
//Packages
const express = require("express");
const app = express();
const router = express.Router();
const winlog = require(`${__dirname}/logger.js`);
const logger = winlog.logger;
const fs = require(`fs`);
// Config
const config = require(`${__dirname}/config.json`)
app.use(express.static(`${__dirname}/public`));
app.listen(80, function () {
logger.log("info", "Webserver is running!");
});
// Timetable entry
router.post('/timetableentry', (req, res, next) => {
let newUser =
{
"name": req.body.name,
"email": req.body.email,
"schedule": {
"1": {
"1": req.body.m1,
"3": req.body.m3,
"5": req.body.m5,
"8": req.body.m8
},
"2": {
"1": req.body.d1,
"3": req.body.d3,
"5": req.body.d5,
"8": req.body.d8
},
"3": {
"1": req.body.i1,
"3": req.body.i3,
"5": req.body.i5,
"8": req.body.i8
},
"4": {
"1": req.body.o1,
"3": req.body.o3,
"5": req.body.o5,
"8": req.body.o8
},
"5": {
"1": req.body.f1,
"3": req.body.f3,
"5": req.body.f5,
"8": req.body.f8
}
}
}
addToJson(newUser);
res.status(200).json({
message: 'user processed'
})
winlog.logger.log("info", "someone" + " made a POST request to / && ");
});
// AddNewUserToJson and Check if he already exists
function addToJson(newUser) {
try {
// Unterricht.json must contain JSON Array
fs.readFile(`${__dirname}/unterricht.json`, "utf8", function (err, data) {
if (err) console.log(err);
data = JSON.parse(data);
if (data.length === 0) {
data.push(newUser);
fs.writeFile(`${__dirname}/unterricht.json`, JSON.stringify(data, null, 2), 'utf-8');
} else {
let st = true;
for (let i = 0; i < data.length; i++) {
if (data[i].email === newUser.email) {
st = false;
}
}
if (st) {
data.push(newUser);
fs.writeFile(`${__dirname}/unterricht.json`, JSON.stringify(data, null, 2), 'utf-8');
}
}
});
} catch (err) {
console.log(err);
}
}
module.exports = app;