From 40c229ea050204659cfedf57d8bc6da4d55f1768 Mon Sep 17 00:00:00 2001 From: Adrian Lee Date: Thu, 8 Jan 2015 23:59:19 -0800 Subject: [PATCH] Implement vote POST endpoint and auto event populate --- database.js | 10 ++++- index.js | 18 +++++--- public/app.js | 100 ++++++++++++++++++++++++++++++----------- public/views/vote.html | 4 ++ 4 files changed, 100 insertions(+), 32 deletions(-) diff --git a/database.js b/database.js index b73fa33..a59f0a1 100644 --- a/database.js +++ b/database.js @@ -4,7 +4,15 @@ mongoose.connect('mongodb://adrian:123123123@ds031108.mongolab.com:31108/quicksc module.exports.Event = mongoose.model('Event', { name: String, detail: String, - id: { type: String, default: function () { return makeid() }} + id: { type: String, default: function () { return makeid() }}, + events: [{ + title: String, + date: Date, + timestamp: { + type: Date, + default: Date.now + } + }] }); diff --git a/index.js b/index.js index d20eaea..aca84a6 100644 --- a/index.js +++ b/index.js @@ -12,8 +12,6 @@ app.use(bodyParser.urlencoded({ extended: false })) // create a new event app.post('/event', function (req, res) { - console.log(req.body); - if (!req.body.name) { return res.sendStatus(400); } @@ -44,10 +42,20 @@ app.get('/event/:id', function (req, res) { }); // create event entry -app.post('/event/:id', function (req, res) { - console.log(req); +app.post('/vote/:id', function (req, res) { + if (!req.body) { + return res.sendStatus(400); + } + + db.Event.findOneAndUpdate({ id: req.params.id }, { $pushAll: { events: req.body.events } }, { upsert: true }, function (err, doc) { + if (err) return res.sendStatus(400); - res.send(req.body); + if (doc) { + res.send(200); + } else { + res.sendStatus(404); + } + }); }); // fetch event diff --git a/public/app.js b/public/app.js index fe00e83..f7c0b4c 100644 --- a/public/app.js +++ b/public/app.js @@ -176,56 +176,100 @@ App.controller("settingsController", function ($scope, $http, $window) { } }); +App.service("recentService", function () { + var recent = []; + + this.getRecent = function () { + recent = window.localStorage.recent ? JSON.parse(window.localStorage.recent) : []; + return recent; + } + + this.addRecent = function (data) { + // Remove existing event from recent + for (var i in recent) { + if ((recent[i] && recent[i].id) == (data && data.id)) { + recent.splice(recent.indexOf(recent[i]), 1); + } + } + + // Add event to recent + data.timestamp = new Date(); + recent.unshift(data); + window.localStorage.recent = JSON.stringify(recent); + } +}); App.controller("eventController", function ($scope, $http, data, recentService) { console.log("eventController") if (data) { + console.log(data); $scope.data = data; recentService.addRecent({ name: data.name, id: data.id }); $scope.shareData = data; - + $scope.permalink = "http://" + location.host + "/#/e/" + data.id; $scope.whatsappText = data.name + " - " + $scope.permalink; } }); -App.controller("voteController", function ($scope, $http, data, recentService) { +App.factory("Dates", function () { + var Dates = {}; + + Dates.data = null; + + Dates.events = []; + + Dates.addEvent = function (newEvent) { + this.events.push(newEvent); + console.log(this.events); + } + + Dates.removeEvent = function (oldEvent) { + this.events.splice(this.events.indexOf(oldEvent), 1); + console.log(this.events); + } + + Dates.clear = function () { + this.data = null; + this.events.length = 0; + } + + Dates.setData = function (data) { + this.clear(); + this.data = data; + } + + return Dates; +}); + +App.controller("voteController", function ($scope, $http, data, Dates) { console.log("voteController") if (data) { $scope.data = data; + Dates.setData(data);; } -}); - -App.service("recentService", function () { - var recent = []; - this.getRecent = function () { - recent = window.localStorage.recent ? JSON.parse(window.localStorage.recent) : []; - return recent; - } - - this.addRecent = function (data) { - // Remove existing event from recent - for (var i in recent) { - if ((recent[i] && recent[i].id) == (data && data.id)) { - recent.splice(recent.indexOf(recent[i]), 1); - } - } - - // Add event to recent - data.timestamp = new Date(); - recent.unshift(data); - window.localStorage.recent = JSON.stringify(recent); + $scope.submit = function () { + $http.post("/vote/" + data.id, { events: Dates.events }). + success(function (data, status, headers, config) { + Dates.clear(); + console.log(data); + }). + error(function (data, status, headers, config) { + console.error(data); + }); } }); -App.controller("calendarController", function ($scope) { +App.controller("calendarController", function ($scope, Dates) { console.log("calendarController"); + console.log(Dates.data.events); + var calendar = $('#calendar').clndr({ template: $("#calendar-template").html(), startWithMonth: moment(), @@ -250,12 +294,16 @@ App.controller("calendarController", function ($scope) { calendar.removeEvents(function(event) { return event.date._i == target.date._i; }); + Dates.removeEvent({ date: target.date.toDate(), title: 'Adrian' }); } else { calendar.addEvents([{ date: target.date, title: 'Adrian' }]); + Dates.addEvent({ date: target.date.toDate(), title: 'Adrian' }); } } - } + }, + events: Dates.data && Dates.data.events }); window.calendar = calendar; -}); \ No newline at end of file +}); + diff --git a/public/views/vote.html b/public/views/vote.html index 6e72ac4..1ab1919 100644 --- a/public/views/vote.html +++ b/public/views/vote.html @@ -16,6 +16,10 @@
Step 1: Choose dates you are avaliable
+ +
+ Submit +