-
Notifications
You must be signed in to change notification settings - Fork 7
/
api.js
68 lines (58 loc) · 1.88 KB
/
api.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
const {Timers, timerEvents} = require('./lib/timers');
module.exports = [
{
method: 'GET',
path: '/timers/',
fn: async (args, callback) => {
try {
return callback(null, Timers.getAllTimers());
} catch (err) {
return callback(err);
}
}
},
{
method: 'POST',
path: '/pause_timer/',
fn: async (args, callback) => {
try {
let timerName = args.body.timername;
// If counting
if (Timers.isCounting(timerName)) {
// Pause it
if (Timers.pause(timerName)) {
return callback(null, true);
}
} else {
// Not counting, start it
if (Timers.resume(timerName)) {
return callback(null, true);
}
}
return callback(null, false);
} catch (err) {
return callback(err);
}
}
},
{
method: 'POST',
path: '/update_timer/',
fn: async (args, callback) => {
try {
let timerName = args.body.timername;
let timerSeconds = args.body.seconds;
if (!Timers.validateName(timerName)) {
return callback('Invalid timername, use: a-z, 0-9, space, min or underscore');
}
// Add a new timer
if (Timers.add(timerName) && Timers.setTime(timerName, timerSeconds) && Timers.start(timerName)) {
return callback(null, true);
}
return callback('Someting went wrong when adding the timer');
} catch (err) {
return callback(err);
}
}
},
];