Skip to content

Commit

Permalink
Implement vote POST endpoint and auto event populate
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianlee committed Jan 9, 2015
1 parent 9bd87a9 commit 40c229e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 32 deletions.
10 changes: 9 additions & 1 deletion database.js
Expand Up @@ -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
}
}]
});


Expand Down
18 changes: 13 additions & 5 deletions index.js
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down
100 changes: 74 additions & 26 deletions public/app.js
Expand Up @@ -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(),
Expand All @@ -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;
});
});

4 changes: 4 additions & 0 deletions public/views/vote.html
Expand Up @@ -16,6 +16,10 @@ <h5>Step 1: Choose dates you are avaliable</h5>
<div class="row">
<div ng-controller="calendarController" id="calendar" class="noselect"></div>
</div>

<div class="row">
<a ng-click="submit()" class="button u-full-width">Submit</a>
</div>
</div>

<script type="text/template" id="calendar-template">
Expand Down

0 comments on commit 40c229e

Please sign in to comment.