Extract vote collection function

This commit is contained in:
Aanand Prasad 2016-06-09 11:48:19 +01:00 committed by Ben Firshman
parent 1f1a6f97a2
commit 4b62828661

View File

@ -45,17 +45,24 @@ function getVotes(client) {
if (err) { if (err) {
console.error("Error performing query: " + err); console.error("Error performing query: " + err);
} else { } else {
var data = result.rows.reduce(function(obj, row) { var votes = collectVotesFromResult(result);
obj[row.vote] = row.count; io.sockets.emit("scores", JSON.stringify(votes));
return obj;
}, {});
io.sockets.emit("scores", JSON.stringify(data));
} }
setTimeout(function() {getVotes(client) }, 1000); setTimeout(function() {getVotes(client) }, 1000);
}); });
} }
function collectVotesFromResult(result) {
var votes = {a: 0, b: 0};
result.rows.forEach(function (row) {
votes[row.vote] = parseInt(row.count);
});
return votes;
}
app.use(cookieParser()); app.use(cookieParser());
app.use(bodyParser()); app.use(bodyParser());
app.use(methodOverride('X-HTTP-Method-Override')); app.use(methodOverride('X-HTTP-Method-Override'));