Last active
December 16, 2015 11:58
-
-
Save BinRoot/5430764 to your computer and use it in GitHub Desktop.
I'm not proud of this code. I should have use asyncjs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| app.get('/api/vote', function(req, res, next) { | |
| ensureAuthenticated(req, res, next, '/publish'); | |
| }, function(req, res) { | |
| var cid = req.query["cid"]; | |
| var u1id = req.user.id; // voter | |
| // ASYNC CALL 1, HERE WE GOOOOO!! | |
| db.findUser({id: u1id}, function(user1Obj) { // get user1 object (mongo) | |
| var previouslyVoted = false; | |
| var votes = user1Obj.votes; | |
| if(votes) { | |
| if(votes.indexOf(cid) != -1) { | |
| previouslyVoted = true; | |
| res.send('ERR 0: User '+u1id+' has already voted on code '+cid); | |
| } | |
| } | |
| if(!previouslyVoted) { | |
| // ASYNC CALL 2, TIME TO GO DEEPER! | |
| getSolrCode(cid, function(blah, ret) { // get code object (solr) | |
| if(ret) { | |
| console.log('getSolrCode results: '+JSON.stringify(ret)); | |
| var oldVotes = ret.votes; | |
| var u2id = ret.uid; // votee | |
| // ASYNC CALL 3, HEEELPPPP MEEEEEEE!! | |
| db.upvoteUser(u2id, function() { // upvote user (mongodb) | |
| var postData = [ {id:cid, votes:{"set":(ret.votes+1)}} ]; | |
| var options = { | |
| uri: aws + 'update/json?commit=true', | |
| method: 'POST', | |
| json: postData | |
| }; | |
| // ASYNC CALL 4, OH GOD WHYY???? | |
| request(options, function (error, response, body) { // upvote code (solr) | |
| if (!error && response.statusCode == 200) { | |
| // ASYNC CALL 5, IT'S BEEN AN HONOR SERVING WITH YOU. | |
| db.votesUpdateUser(u1id, cid, function() { // add cid to user's votes (mongodb) | |
| res.send('Done.'); | |
| }); | |
| } | |
| }); | |
| }); | |
| } | |
| }); | |
| } | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment