유희정

add and remove event

...@@ -968,6 +968,11 @@ ...@@ -968,6 +968,11 @@
968 "resolved": "https://registry.npmjs.org/lazy-cache/-/lazy-cache-1.0.4.tgz", 968 "resolved": "https://registry.npmjs.org/lazy-cache/-/lazy-cache-1.0.4.tgz",
969 "integrity": "sha1-odePw6UEdMuAhF07O24dpJpEbo4=" 969 "integrity": "sha1-odePw6UEdMuAhF07O24dpJpEbo4="
970 }, 970 },
971 + "local-iso-dt": {
972 + "version": "1.2.2",
973 + "resolved": "https://registry.npmjs.org/local-iso-dt/-/local-iso-dt-1.2.2.tgz",
974 + "integrity": "sha512-/wNNXYs7uro+u0fo3/HUOriWa7rrVgZaKjjfyJ1azAWMXBCk1v/fgsVfC5CtlENQMFrPvkg02ldQFFQLETiS3w=="
975 + },
971 "longest": { 976 "longest": {
972 "version": "1.0.1", 977 "version": "1.0.1",
973 "resolved": "https://registry.npmjs.org/longest/-/longest-1.0.1.tgz", 978 "resolved": "https://registry.npmjs.org/longest/-/longest-1.0.1.tgz",
...@@ -1027,6 +1032,11 @@ ...@@ -1027,6 +1032,11 @@
1027 "minimist": "0.0.8" 1032 "minimist": "0.0.8"
1028 } 1033 }
1029 }, 1034 },
1035 + "moment": {
1036 + "version": "2.24.0",
1037 + "resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz",
1038 + "integrity": "sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg=="
1039 + },
1030 "morgan": { 1040 "morgan": {
1031 "version": "1.9.1", 1041 "version": "1.9.1",
1032 "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.1.tgz", 1042 "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.1.tgz",
......
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
18 "googleapis": "^45.0.0", 18 "googleapis": "^45.0.0",
19 "http-errors": "~1.6.3", 19 "http-errors": "~1.6.3",
20 "jade": "~1.11.0", 20 "jade": "~1.11.0",
21 + "local-iso-dt": "^1.2.2",
22 + "moment": "^2.24.0",
21 "morgan": "~1.9.1", 23 "morgan": "~1.9.1",
22 "mysql": "^2.17.1", 24 "mysql": "^2.17.1",
23 "needle": "^2.4.0", 25 "needle": "^2.4.0",
......
1 var express = require('express'); 1 var express = require('express');
2 var router = express.Router(); 2 var router = express.Router();
3 var gcal = require('google-calendar'); 3 var gcal = require('google-calendar');
4 +const { localISOdt } = require('local-iso-dt');
5 +var accessToken;
4 6
7 +var id;
8 +var accessToken;
9 +
10 +router.get('/', function(req, res, next) {
11 + console.log(accessToken);
12 + console.log(id);
13 + gcal(accessToken).events.list(id,{maxResult:1},function(err,data){
14 + if(err) return console.log(err);
15 + else {
16 + console.log(data.items);
17 + res.render("events",{id:id,calendars:data.items});
18 + }
19 + });
20 +});
5 router.post('/', function(req, res, next) { 21 router.post('/', function(req, res, next) {
6 - var accessToken = req.body.token; 22 + accessToken = req.body.token;
7 - var id = req.body.id; 23 + id = req.body.id;
8 gcal(accessToken).events.list(id,{maxResult:1},function(err,data){ 24 gcal(accessToken).events.list(id,{maxResult:1},function(err,data){
9 if(err) return console.log(err); 25 if(err) return console.log(err);
10 else { 26 else {
11 - console.log(data.items[0].start); 27 + console.log(data.items);
12 res.render("events",{id:id,calendars:data.items}); 28 res.render("events",{id:id,calendars:data.items});
13 } 29 }
14 }); 30 });
15 }); 31 });
32 +router.post('/add', function(req, res, next) {
33 + var sday = req.body.startDay.split('-');
34 + var stime = req.body.startTime.split(':');
35 + var start= timestamp(new Date(sday[0],sday[1],sday[2],stime[0],stime[1],0));
36 + var eday = req.body.endDay.split('-');
37 + var etime = req.body.endTime.split(':');
38 + var end = timestamp(new Date(eday[0],eday[1],eday[2],etime[0],etime[1],0));
39 + var event={
40 + 'summary':req.body.summary,
41 + 'location':req.body.location,
42 + 'description':req.body.description,
43 + 'start':{
44 + 'dateTime':start
45 + },
46 + 'end':{
47 + 'dateTime':end,
48 + }
49 + }
50 + gcal(accessToken).events.insert(id,event,function(err,data){
51 + if(err) return console.log(err);
52 + else res.redirect('/events');
53 + });
54 +
55 +});
56 +
57 +router.post('/remove', function(req, res, next) {
58 + var eId = req.body.eId;
59 + console.log(id);
60 + console.log(eId);
61 + gcal(accessToken).events.delete(id, eId, function(err, data) {
62 + if(err) return res.send(err);
63 + return res.redirect('/events');
64 + });
65 +
66 +});
16 67
68 +var timestamp = function (date) {
69 + var pad = function (amount, width) {
70 + var padding = "";
71 + while (padding.length < width - 1 && amount < Math.pow(10, width - padding.length - 1))
72 + padding += "0";
73 + return padding + amount.toString();
74 + }
75 + date = date ? date : new Date();
76 + var offset = date.getTimezoneOffset();
77 + return pad(date.getFullYear(), 4)
78 + + "-" + pad(date.getMonth() + 1, 2)
79 + + "-" + pad(date.getDate(), 2)
80 + + "T" + pad(date.getHours(), 2)
81 + + ":" + pad(date.getMinutes(), 2)
82 + + ":" + pad(date.getSeconds(), 2)
83 + + (offset > 0 ? "-" : "+")
84 + + pad(Math.floor(Math.abs(offset) / 60), 2)
85 + + ":" + pad(Math.abs(offset) % 60, 2);
86 + }
17 87
18 module.exports = router; 88 module.exports = router;
19 89
20 -//db에서 calendar id에 해당하는 이벤트 출력
...\ No newline at end of file ...\ No newline at end of file
90 +//timestamp(new Date(1983, 4, 9, 11, 5, 17));
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -14,7 +14,8 @@ passport.use(new GoogleStrategy({ ...@@ -14,7 +14,8 @@ passport.use(new GoogleStrategy({
14 clientSecret:'N7Uh_oVQZt-almzA4DkM4bm_', 14 clientSecret:'N7Uh_oVQZt-almzA4DkM4bm_',
15 callbackURL:'http://localhost:3000/auth/google/callback', 15 callbackURL:'http://localhost:3000/auth/google/callback',
16 scope:['openid','email','https://www.googleapis.com/auth/calendar.readonly', 16 scope:['openid','email','https://www.googleapis.com/auth/calendar.readonly',
17 - 'https://www.googleapis.com/auth/calendar'] 17 + 'https://www.googleapis.com/auth/calendar',
18 + 'https://www.googleapis.com/auth/calendar.events']
18 }, 19 },
19 function(accessToken,refreshToken,params,profile,done){ 20 function(accessToken,refreshToken,params,profile,done){
20 profile.accessToken = accessToken; 21 profile.accessToken = accessToken;
......
...@@ -2,17 +2,43 @@ ...@@ -2,17 +2,43 @@
2 <html> 2 <html>
3 <head> 3 <head>
4 <link rel='stylesheet' href='/stylesheets/style.css' /> 4 <link rel='stylesheet' href='/stylesheets/style.css' />
5 -
6 </head> 5 </head>
7 <body> 6 <body>
8 <h1><%= id %></h1> 7 <h1><%= id %></h1>
9 - <p> 8 + <div id="events" style="width:200px;float:left">
10 <% calendars.forEach(function(val){ %> 9 <% calendars.forEach(function(val){ %>
11 - <li>summary : <%= val.summary %></li> 10 + <form action="/events/remove" method="POST">
12 - <li>created : <%= val.created %></li> 11 + <p>summary : <%= val.summary %></p>
13 - <li>start : <%= val.start.dateTime %></li> 12 + <p>created : <%= val.created %></p>
14 - <li>end : <%= val.end.dateTime %></li> 13 + <p>start : <%= val.start.dateTime %></p>
14 + <p>end : <%= val.end.dateTime %></p>
15 + <input type="hidden" name="eId" value=<%= val.id %>>
16 + <input type="submit" value="Delete">
17 + </form>
15 <% }) %> 18 <% }) %>
16 - </p> 19 + </div>
20 + <div id="menu" style="width:100px;float:right">
21 + <div id="add" style="width:100px;height:50px">
22 + <h2>Add Event!</h2>
23 + <form name="add" action="/events/add" method="POST">
24 + <input type="hidden" name="id" value=<%= id %> >
25 + Summary:<br>
26 + <input type="text" name="summary" placeholder="Summary of event">
27 + Location:<br>
28 + <input type="text" name="location" placeholder="Location ot event">
29 + Desciption:<br>
30 + <input type="text" name="description" placeholder="Description">
31 + Start Day:<br>
32 + <input type="date" name="startDay">
33 + Start Time:<br>
34 + <input type="time" name="startTime">
35 + End Day:<br>
36 + <input type="date" name="endDay">
37 + End Time:<br>
38 + <input type="time" name="endTime">
39 + <input type="submit" name="submit" value="Add">
40 + </form>
41 + </div>
42 + </div>
17 </body> 43 </body>
18 </html> 44 </html>
...\ No newline at end of file ...\ No newline at end of file
......