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

This commit is contained in:
Nathan Leniz
2015-03-04 07:15:00 +09:00
parent 968832ae52
commit 6ac780e836
10 changed files with 138 additions and 53 deletions

8
app.js
View File

@ -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
);

View File

@ -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);
});
};

View File

@ -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: []
},

View File

@ -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);
});

14
seed_data/comments.json Normal file
View File

@ -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": []
}
]

View File

@ -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');
});

View File

@ -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",

View File

@ -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("<div class='row text-center'><div class='col-xs-3 text-right'><i class='ion-arrow-up-b'></i></div><div class='col-xs-6'><a href='/stories/" + linkedName + "'>" + data[i].headline + "</a></div><div class='col-xs-3'></div></div><div class='row text-center'><div class='col-xs-3 text-right'>" + rank + "</div><div class='col-xs-6'>" + data[i].author.username + "</div><div class='col-xs-3'></div></div></li>");
$(li).appendTo($('#story-list'));
}
});
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("<div class='row text-left'><div class='col-xs-3 text-right'><i class='ion-arrow-up-b'></i></div><div class='col-xs-6'>" + commentDetails.body +
"</div><div class='col-xs-3'></div></div><div class='row text-left'><div class='col-xs-3 text-right'>" + commentDetails.rank + "</div><div class='col-xs-6'>"
+ commentDetails.author.username + "</div><div class='col-xs-3'></div></div></li>")
.appendTo($('#comment-list'));
}
})
}, comments);

View File

@ -1,5 +1,7 @@
extends ../layout
block content
script.
var challengeName = 'Camper News';
.panel.text-center
h1 Camper News
include ./posts

View File

@ -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