-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (71 loc) · 3.03 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
const chalk = require('chalk');
const express = require('express');
const bodyParser = require('body-parser');
const JsonDB = require('node-json-db');
const config = require('./config.js');
const core = require('./core.js');
const app = express();
const db = new JsonDB("schedule", true, true);
app.use(express.static('public'));
app.use(bodyParser.json());
/* Set year week day if not exist */
try {
db.getData(`/${core.getYearNumber()}/${core.getWeekNumber()}/startDate`);
db.getData(`/${core.getYearNumber()}/${core.getWeekNumber()}/1`);
db.getData(`/${core.getYearNumber()}/${core.getWeekNumber()}/2`);
db.getData(`/${core.getYearNumber()}/${core.getWeekNumber()}/3`);
db.getData(`/${core.getYearNumber()}/${core.getWeekNumber()}/4`);
db.getData(`/${core.getYearNumber()}/${core.getWeekNumber()}/5`);
db.getData(`/${core.getYearNumber()}/${core.getWeekNumber()}/6`);
db.getData(`/${core.getYearNumber()}/${core.getWeekNumber()}/7`);
} catch(e) {
db.push(`/${core.getYearNumber()}/${core.getWeekNumber()}/startDate`, "");
db.push(`/${core.getYearNumber()}/${core.getWeekNumber()}/1`, []);
db.push(`/${core.getYearNumber()}/${core.getWeekNumber()}/2`, []);
db.push(`/${core.getYearNumber()}/${core.getWeekNumber()}/3`, []);
db.push(`/${core.getYearNumber()}/${core.getWeekNumber()}/4`, []);
db.push(`/${core.getYearNumber()}/${core.getWeekNumber()}/5`, []);
db.push(`/${core.getYearNumber()}/${core.getWeekNumber()}/6`, []);
db.push(`/${core.getYearNumber()}/${core.getWeekNumber()}/7`, []);
}
/* Get custom schedule */
app.get('/api/getSchedule/:year/:week', function (req, res) {
let year = parseInt(req.params.year);
let week = parseInt(req.params.week);
if(isNaN(year)) {
res.json({
status: "error",
message: "Year is not a valid number"
});
} else {
if(isNaN(week)) {
res.json({
status: "error",
message: "Week is not a valid number"
});
} else {
try {
let getScheduleData = db.getData(`/${parseInt(year)}/${parseInt(week)}`);
res.json(getScheduleData);
} catch(e) {
db.push(`/${parseInt(year)}/${parseInt(week)}/startDate`, "");
db.push(`/${parseInt(year)}/${parseInt(week)}/1`, []);
db.push(`/${parseInt(year)}/${parseInt(week)}/2`, []);
db.push(`/${parseInt(year)}/${parseInt(week)}/3`, []);
db.push(`/${parseInt(year)}/${parseInt(week)}/4`, []);
db.push(`/${parseInt(year)}/${parseInt(week)}/5`, []);
db.push(`/${parseInt(year)}/${parseInt(week)}/6`, []);
db.push(`/${parseInt(year)}/${parseInt(week)}/7`, []);
let getScheduleData = db.getData(`/${parseInt(year)}/${parseInt(week)}`);
res.json(getScheduleData);
console.log(`Week (${parseInt(week)}) and year (${parseInt(year)}) combination does not exist, creating data...`);
}
}
}
});
/* Get current schedule */
app.get('/api/getSchedule/', function (req, res) {
res.json(db.getData(`/${core.getYearNumber()}/${core.getWeekNumber()}`));
});
/* Start HTTP & API server */
app.listen(config.http_port);