events.js
2.74 KB
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
var express = require('express');
var router = express.Router();
var gcal = require('google-calendar');
var accessToken;
var id;
var accessToken;
router.get('/', function(req, res, next) {
console.log(accessToken);
console.log(id);
gcal(accessToken).events.list(id,{maxResult:1},function(err,data){
if(err) return console.log(err);
else {
console.log(data.items);
res.render("events",{id:id,calendars:data.items});
}
});
});
router.post('/', function(req, res, next) {
accessToken = req.body.token;
id = req.body.id;
gcal(accessToken).events.list(id,{maxResult:1},function(err,data){
if(err) return console.log(err);
else {
console.log(data.items);
res.render("events",{id:id,calendars:data.items});
}
});
});
router.post('/add', function(req, res, next) {
var sday = req.body.startDay.split('-');
var stime = req.body.startTime.split(':');
var start= timestamp(new Date(sday[0],sday[1],sday[2],stime[0],stime[1],0));
var eday = req.body.endDay.split('-');
var etime = req.body.endTime.split(':');
var end = timestamp(new Date(eday[0],eday[1],eday[2],etime[0],etime[1],0));
var event={
'summary':req.body.summary,
'location':req.body.location,
'description':req.body.description,
'start':{
'dateTime':start
},
'end':{
'dateTime':end,
}
}
gcal(accessToken).events.insert(id,event,function(err,data){
if(err) return console.log(err);
else res.redirect('/events');
});
});
router.post('/remove', function(req, res, next) {
var eId = req.body.eId;
console.log(id);
console.log(eId);
gcal(accessToken).events.delete(id, eId, function(err, data) {
if(err) return res.send(err);
return res.redirect('/events');
});
});
router.get('/kakao', function(req,res,next){
console.log("success");
});
var timestamp = function (date) {
var pad = function (amount, width) {
var padding = "";
while (padding.length < width - 1 && amount < Math.pow(10, width - padding.length - 1))
padding += "0";
return padding + amount.toString();
}
date = date ? date : new Date();
var offset = date.getTimezoneOffset();
return pad(date.getFullYear(), 4)
+ "-" + pad(date.getMonth() + 1, 2)
+ "-" + pad(date.getDate(), 2)
+ "T" + pad(date.getHours(), 2)
+ ":" + pad(date.getMinutes(), 2)
+ ":" + pad(date.getSeconds(), 2)
+ (offset > 0 ? "-" : "+")
+ pad(Math.floor(Math.abs(offset) / 60), 2)
+ ":" + pad(Math.abs(offset) % 60, 2);
}
module.exports = router;
//timestamp(new Date(1983, 4, 9, 11, 5, 17));