From 6ac780e836609e7af4811a477d15e4093a9124ed Mon Sep 17 00:00:00 2001 From: Nathan Leniz Date: Wed, 4 Mar 2015 07:15:00 +0900 Subject: [PATCH] Rewire upvote to use post and req.body instead of req.params, add logic on click handler to reject upvotes if the user has already upvoted, coment display now works --- app.js | 8 ++++++- controllers/story.js | 26 +++++++++++++++++---- models/Comment.js | 6 ++++- public/js/main.js | 39 ++++++++++++++++++-------------- seed_data/comments.json | 14 ++++++++++++ seed_data/seed.js | 22 ++++++++++++++++-- seed_data/stories.json | 4 ++-- views/post/comments.jade | 49 ++++++++++++++++++++++++---------------- views/post/index.jade | 2 ++ views/post/show.jade | 21 ++++++++++++----- 10 files changed, 138 insertions(+), 53 deletions(-) create mode 100644 seed_data/comments.json diff --git a/app.js b/app.js index a5c3f8e4bf..bbff557451 100644 --- a/app.js +++ b/app.js @@ -288,12 +288,18 @@ app.get( '/stories/index', storyController.json ); + +app.get( + '/stories/comments/:id', + storyController.comments +); + app.get( '/stories/:storyName', storyController.returnIndividualStory ); app.post( - '/stories/upvote/:id', + '/stories/upvote/', storyController.upvote ); diff --git a/controllers/story.js b/controllers/story.js index f4cf67ed5d..e32a81aba0 100644 --- a/controllers/story.js +++ b/controllers/story.js @@ -53,7 +53,7 @@ exports.returnIndividualStory = function(req, res, next) { if (dashedNameFull !== dashedName) { return res.redirect('../stories/' + dashedNameFull); } - debug('Story id is', story._id); + debug('Story', story); res.render('post/show', { title: story.headline, @@ -63,20 +63,38 @@ exports.returnIndividualStory = function(req, res, next) { rank: story.rank, upVotes: story.upVotes, comments: story.comments, - id: story._id + id: story._id, + user: req.user }); }); }; exports.upvote = function(req, res, next) { - var data = req.params.id; - Story.find({'_id': data}, function(err, story) { + var data = req.body.data; + Story.find({'_id': data.id}, function(err, story) { if (err) { throw err; } story = story.pop(); story.rank++; + story.upVotes.push( + { + upVotedBy: data.upVoter._id, + upVotedByUsername: data.upVoter.profile.username + } + ); story.save(); return res.send(story); }); }; + +exports.comments = function(req, res, next) { + var data = req.params.id; + Comment.find({'_id': data}, function(err, comment) { + if (err) { + throw err; + } + comment = comment.pop(); + return res.send(comment); + }); +}; diff --git a/models/Comment.js b/models/Comment.js index 9f71c32591..9206b6ed75 100644 --- a/models/Comment.js +++ b/models/Comment.js @@ -6,11 +6,15 @@ var commentSchema = new mongoose.Schema({ type: String, required: true }, + body: { + type: String, + default: '' + }, rank: { type: Number, default: -Infinity }, - upVotes: { + upvotes: { type: Array, default: [] }, diff --git a/public/js/main.js b/public/js/main.js index 1613a8cfd4..8f7c26ba39 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -107,8 +107,6 @@ $(document).ready(function() { window.location = '/challenges/' + (parseInt(l[l.length - 1]) + 1); }); - - // Bonfire instructions functions $('#more-info').on('click', function() { ga('send', 'event', 'Challenge', 'more-info', challengeName); @@ -123,22 +121,29 @@ $(document).ready(function() { var upvoteHandler = function () { var _id = storyId; - - $.ajax({ - type: 'POST', - url: '/stories/upvote/' + _id, - beforeSend: function() { - $('#upvote').unbind('click'); - }, - error: function(xhr, textStatus, errorThrown) { - console.log('got error'); - $('#upvote').bind('click', upvoteHandler); - }, - success: function (data, textStatus, xhr) { - console.log(data); - $('#storyRank').text(data.rank); + $('#upvote').unbind('click'); + var alreadyUpvoted = false; + for (var i = 0; i < upVotes.length; i++) { + if (upVotes[i].upVotedBy === user._id) { + alreadyUpvoted = true; + break; } - }); + } + if (!alreadyUpvoted) { + $.post('/stories/upvote', + { + data: { + id: _id, + upVoter: user + } + }) + .fail(function (xhr, textStatus, errorThrown) { + $('#upvote').bind('click', upvoteHandler); + }) + .done(function (data, textStatus, xhr) { + $('#storyRank').text(data.rank); + }); + } }; $('#upvote').on('click', upvoteHandler); }); diff --git a/seed_data/comments.json b/seed_data/comments.json new file mode 100644 index 0000000000..5f4b1efc37 --- /dev/null +++ b/seed_data/comments.json @@ -0,0 +1,14 @@ +[ + { + "associatedPost": "54f584db16cd6570aa176eda", + "body": "cats like to sit around and be lazy as hell", + "rank": "1", + "upvotes": [], + "author": { + "username" : "terakilonoob", + "picture": "https://thedrinkingtraveler.files.wordpress.com/2015/02/24127-funny-grumpy-cat-memesvery-bad-morning-meme-0rlh4r5c-wallpaper-1024x1024.jpg", + "id" : "ade671aa0756dc61bd485f45" + }, + "comments": [] + } +] \ No newline at end of file diff --git a/seed_data/seed.js b/seed_data/seed.js index 447df778f9..cbf537efa9 100644 --- a/seed_data/seed.js +++ b/seed_data/seed.js @@ -9,12 +9,13 @@ var Challenge = require('../models/Challenge.js'), challenges = require('./challenges.json'), coursewares = require('./coursewares.json'), stories = require('./stories.json'), - bonfires = require('./bonfires.json'); + bonfires = require('./bonfires.json'), + comments = require('./comments.json'); mongoose.connect(secrets.db); var counter = 0; -var offerings = 4; +var offerings = 5; var CompletionMonitor = function() { counter++; @@ -93,4 +94,21 @@ Story.remove({}, function(err, data) { CompletionMonitor(); }); console.log('stories'); +}); + +Comment.remove({}, function(err, data) { + if (err) { + console.error(err); + } else { + console.log('Deleted ', data); + } + Comment.create(comments, function(err, data) { + if (err) { + console.log(err); + } else { + console.log('Saved ', data); + } + CompletionMonitor(); + }); + console.log('stories'); }); \ No newline at end of file diff --git a/seed_data/stories.json b/seed_data/stories.json index a38fedcdb6..fd1a3a1c4f 100644 --- a/seed_data/stories.json +++ b/seed_data/stories.json @@ -41,7 +41,7 @@ { "headline": "Cat sits on keyboard 20", "link": "http://kotaku.com/5991046/why-cats-love-sitting-on-keyboards", - "body": "cats love keyboards you know", + "body": "ipsizzle dolizzle sit amet, ghetto adipiscing elit. Nullam fo shizzle velizzle, aliquet volutpizzle, suscipizzle shiz, gravida vizzle, arcu. Pellentesque izzle tortor. Sizzle doggy. Boom shackalack izzle dolizzle dapibizzle ass tempizzle tellivizzle. Ma nizzle we gonna chung shiz izzle tellivizzle. Vestibulum dizzle tortor. Pellentesque pimpin' rhoncus you son of a bizzle. In dizzle habitasse platea dictumst. Donec dapibizzle. Curabitizzle pot yippiyo, pretizzle da bomb, mattis izzle, da bomb vitae, nunc. Ass suscipizzle. Cool sempizzle bow wow wow sed purus.", "rank": 20, "upVotes": [], "author": { @@ -49,7 +49,7 @@ "userId": "a2ad135e2aa27c14fc73ee44", "picture": "https://www.google.com/search?q=cat+photo+google+images&tbm=isch&imgil=7dFgV4OZlJObjM%253A%253BDGoqtUgH7IKDWM%253Bhttp%25253A%25252F%25252Fwww.wired.co.uk%25252Fnews%25252Farchive%25252F2012-06%25252F26%25252Fgoogle-brain-recognises-cats&source=iu&pf=m&fir=7dFgV4OZlJObjM%253A%252CDGoqtUgH7IKDWM%252C_&usg=__yxi54C0GOssHCOLnh1StLAH7KNk%3D&biw=1920&bih=981&ved=0CDYQyjc&ei=n3n1VL6ENcHZoATjvYDABQ#imgrc=7dFgV4OZlJObjM%253A%3BDGoqtUgH7IKDWM%3Bhttp%253A%252F%252Fcdni.wired.co.uk%252F620x413%252Fs_v%252Fshutterstock_65735200.jpg%3Bhttp%253A%252F%252Fwww.wired.co.uk%252Fnews%252Farchive%252F2012-06%252F26%252Fgoogle-brain-recognises-cats%3B620%3B413" }, - "comments": [] + "comments": ["54f61b0e43f0c2b90f162ec4"] }, { "headline": "Cat sits on keyboard 2", diff --git a/views/post/comments.jade b/views/post/comments.jade index e3f19430ed..0efc3dbdac 100644 --- a/views/post/comments.jade +++ b/views/post/comments.jade @@ -2,24 +2,33 @@ h6 ul#comment-list.comment-list script(src="https://cdn.jsdelivr.net/ramda/0.10.0/ramda.min.js") - h1 Super tired folks, going to call it a night - // - script. - var getLinkedName = function getLinkedName(name) { - return name.toLowerCase().replace(/\s/g, '-'); - } - $.ajax({ - url: '/stories/index', - type: 'GET' - }) - .success( - function(data) { - for (var i = 0; i < data.length; i++) { - var li = document.createElement('li'); - var linkedName = getLinkedName(data[i].headline); - var rank = data[i].rank; - $(li).html("
" + rank + "
" + data[i].author.username + "
"); - $(li).appendTo($('#story-list')); - } - }); \ No newline at end of file + script. + var commentDetails; + R.forEach(function displayComments(comment) { + console.log('this is the comment id', comment); + $.ajax({ + type: 'GET', + url: '/stories/comments/' + comment, + error: function(xhr, textStatus, errorThrown) { + console.log('got error'); + commentDetails = { + error: true, + body: 'There seems to be a problem fetching this comment.' + } + }, + success: function (data, textStatus, xhr) { + console.log(data); + commentDetails = data; + var li = document.createElement('li'); + $(li) + .html("
" + commentDetails.body + + "
" + commentDetails.rank + "
" + + commentDetails.author.username + "
") + .appendTo($('#comment-list')); + } + + + }) + + }, comments); \ No newline at end of file diff --git a/views/post/index.jade b/views/post/index.jade index 71172b1d83..19a0c21822 100644 --- a/views/post/index.jade +++ b/views/post/index.jade @@ -1,5 +1,7 @@ extends ../layout block content + script. + var challengeName = 'Camper News'; .panel.text-center h1 Camper News include ./posts \ No newline at end of file diff --git a/views/post/show.jade b/views/post/show.jade index 698701115c..3ce14c3c13 100644 --- a/views/post/show.jade +++ b/views/post/show.jade @@ -1,19 +1,23 @@ extends ../layout block content script. - var challengeName = 'Story'; + var challengeName = 'Camper News'; var storyId = !{JSON.stringify(id)}; + var comments = !{JSON.stringify(comments)}; + var upVotes = !{JSON.stringify(upVotes)}; + .jumbotron .row .col-xs-2(style='position: relative; top: 50%; -webkit-transform: translateY(50%); -ms-transform: translateY(50%);transform: translateY(50%);') - h3#storyRank= rank + h3 + a#upvote + i.ion-arrow-up-b .col-xs-10.text-center h1= title .row .col-xs-2 - h3 - a#upvote - i.ion-arrow-up-b + h3#storyRank= rank + .col-xs-10.text-center h3= body .row.negative-35 @@ -21,8 +25,13 @@ block content .row .col-xs-12.col-md-6.col-lg-5 textarea#comment-box.form-control(name="comment-box", rows=5) + if (user) + script. + var user = !{JSON.stringify(user)}; + $('#upvote').unbind('click'); .row - include ./comments + .col-xs-12.col-md-7.col-lg-4.text-left + include ./comments