유희정

add and remove event

......@@ -968,6 +968,11 @@
"resolved": "https://registry.npmjs.org/lazy-cache/-/lazy-cache-1.0.4.tgz",
"integrity": "sha1-odePw6UEdMuAhF07O24dpJpEbo4="
},
"local-iso-dt": {
"version": "1.2.2",
"resolved": "https://registry.npmjs.org/local-iso-dt/-/local-iso-dt-1.2.2.tgz",
"integrity": "sha512-/wNNXYs7uro+u0fo3/HUOriWa7rrVgZaKjjfyJ1azAWMXBCk1v/fgsVfC5CtlENQMFrPvkg02ldQFFQLETiS3w=="
},
"longest": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/longest/-/longest-1.0.1.tgz",
......@@ -1027,6 +1032,11 @@
"minimist": "0.0.8"
}
},
"moment": {
"version": "2.24.0",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz",
"integrity": "sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg=="
},
"morgan": {
"version": "1.9.1",
"resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.1.tgz",
......
......@@ -18,6 +18,8 @@
"googleapis": "^45.0.0",
"http-errors": "~1.6.3",
"jade": "~1.11.0",
"local-iso-dt": "^1.2.2",
"moment": "^2.24.0",
"morgan": "~1.9.1",
"mysql": "^2.17.1",
"needle": "^2.4.0",
......
var express = require('express');
var router = express.Router();
var gcal = require('google-calendar');
const { localISOdt } = require('local-iso-dt');
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) {
var accessToken = req.body.token;
var id = req.body.id;
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[0].start);
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');
});
});
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;
//db에서 calendar id에 해당하는 이벤트 출력
\ No newline at end of file
//timestamp(new Date(1983, 4, 9, 11, 5, 17));
\ No newline at end of file
......
......@@ -14,7 +14,8 @@ passport.use(new GoogleStrategy({
clientSecret:'N7Uh_oVQZt-almzA4DkM4bm_',
callbackURL:'http://localhost:3000/auth/google/callback',
scope:['openid','email','https://www.googleapis.com/auth/calendar.readonly',
'https://www.googleapis.com/auth/calendar']
'https://www.googleapis.com/auth/calendar',
'https://www.googleapis.com/auth/calendar.events']
},
function(accessToken,refreshToken,params,profile,done){
profile.accessToken = accessToken;
......
......@@ -2,17 +2,43 @@
<html>
<head>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1><%= id %></h1>
<p>
<% calendars.forEach(function(val){ %>
<li>summary : <%= val.summary %></li>
<li>created : <%= val.created %></li>
<li>start : <%= val.start.dateTime %></li>
<li>end : <%= val.end.dateTime %></li>
<% }) %>
</p>
<h1><%= id %></h1>
<div id="events" style="width:200px;float:left">
<% calendars.forEach(function(val){ %>
<form action="/events/remove" method="POST">
<p>summary : <%= val.summary %></p>
<p>created : <%= val.created %></p>
<p>start : <%= val.start.dateTime %></p>
<p>end : <%= val.end.dateTime %></p>
<input type="hidden" name="eId" value=<%= val.id %>>
<input type="submit" value="Delete">
</form>
<% }) %>
</div>
<div id="menu" style="width:100px;float:right">
<div id="add" style="width:100px;height:50px">
<h2>Add Event!</h2>
<form name="add" action="/events/add" method="POST">
<input type="hidden" name="id" value=<%= id %> >
Summary:<br>
<input type="text" name="summary" placeholder="Summary of event">
Location:<br>
<input type="text" name="location" placeholder="Location ot event">
Desciption:<br>
<input type="text" name="description" placeholder="Description">
Start Day:<br>
<input type="date" name="startDay">
Start Time:<br>
<input type="time" name="startTime">
End Day:<br>
<input type="date" name="endDay">
End Time:<br>
<input type="time" name="endTime">
<input type="submit" name="submit" value="Add">
</form>
</div>
</div>
</body>
</html>
\ No newline at end of file
......