-
Notifications
You must be signed in to change notification settings - Fork 33
/
server.js
80 lines (71 loc) · 2.36 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
const express = require('express');
const gcal = require('./Utility/gcal.js');
const days = require('./ReqHandlers/GET-Handlers/days.js');
const timeslots = require('./ReqHandlers/GET-Handlers/timeslots.js');
const book = require('./ReqHandlers/POST-Handlers/book.js');
const app = express();
const auth = {};
// Get the OAuth2 client for making Google Calendar API requests.
gcal.initAuthorize(setAuth);
function setAuth(auth) {
this.auth = auth;
console.log('\nServer is now running... Ctrl+C to end');
}
/**
* Handles 'days' GET requests.
* @param {object} req The requests object provided by Express. See Express doc.
* @param {object} res The results object provided by Express. See Express doc.
*/
function handleGetDays(req, res) {
const year = req.query.year;
const month = req.query.month;
days.getBookableDays(this.auth, year, month)
.then(function(data) {
res.send(data);
})
.catch(function(data) {
res.send(data);
});
}
/**
* Handles 'timeslots' GET requests.
* @param {object} req The requests object provided by Express. See Express doc.
* @param {object} res The results object provided by Express. See Express doc.
*/
function handleGetTimeslots(req, res) {
const year = req.query.year;
const month = req.query.month;
const day = req.query.day;
timeslots.getAvailTimeslots(this.auth, year, month, day)
.then(function(data) {
res.send(data);
})
.catch(function(data) {
res.send(data);
});
}
/**
* Handles 'book' POST requests.
* @param {object} req The requests object provided by Express. See Express doc.
* @param {object} res The results object provided by Express. See Express doc.
*/
function handleBookAppointment(req, res) {
const year = req.query.year;
const month = req.query.month;
const day = req.query.day;
const hour = req.query.hour;
const minute = req.query.minute;
book.bookAppointment(this.auth, year, month, day, hour, minute)
.then(function(data) {
res.send(data);
})
.catch(function(data) {
res.send(data);
});
}
// Routes.
app.get('/days', handleGetDays);
app.get('/timeslots', handleGetTimeslots);
app.post('/book', handleBookAppointment);
// Listen on port 8080 for incoming requests to the server.
const server = app.listen(8080, function() {});