events.js 2.72 KB
var express = require('express');
var router = express.Router();
var gcal = require('google-calendar');

var accessToken;
var id;


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));