diff --git a/app.js b/app.js index 552e0d6e0c..237cd1f65a 100644 --- a/app.js +++ b/app.js @@ -289,6 +289,11 @@ app.get( storyController.json ); +app.post( + '/stories/comment/submit', + storyController.commentSubmit +); + app.get( '/stories/comments/:id', storyController.comments diff --git a/controllers/story.js b/controllers/story.js index 83c64ec94b..8ea25044ca 100644 --- a/controllers/story.js +++ b/controllers/story.js @@ -100,16 +100,6 @@ exports.comments = function(req, res, next) { }); }; -/* - - author: {}, - comments: { - type: Array, - default: [] - }, - image: - */ - exports.storySubmission = function(req, res, next) { var data = req.body.data; var storyLink = data.headline @@ -147,3 +137,37 @@ exports.storySubmission = function(req, res, next) { })); }); }; + +exports.commentSubmit = function(req, res, next) { + var data = req.body.data; + var comment = new Comment({ + associatedPost: data.associatedPost, + body: data.body, + rank: 0, + upvotes: 0, + author: data.author, + comments: [] + }); + + comment.save(function(err, data) { + if (err) { + return res.status(500); + } + debug('this is data from save', data); + Story.find({'_id': comment.associatedPost}, function(err, associatedStory) { + if (err) { + return res.status(500); + } + associatedStory = associatedStory.pop(); + if (associatedStory) { + associatedStory.comments.push(data._id); + associatedStory.save(function(err, data) { + if (err) { + res.status(500); + } + res.send(true); + }); + } + }); + }); +}; diff --git a/public/js/main.js b/public/js/main.js index 8a5f8216c3..da5163c3e3 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -183,6 +183,33 @@ $(document).ready(function() { }; $('#story-submit').on('click', storySubmitButtonHandler); + + var commentSubmitButtonHandler = function commentSubmitButtonHandler() { + var data = $('#comment-box').val(); + + $('#comment-button').unbind('click'); + $.post('/stories/comment/submit', + { + data: { + associatedPost: storyId, + body: data, + author: { + picture: user.profile.picture, + userId: user._id, + username: user.profile.username + } + } + }) + .fail(function (xhr, textStatus, errorThrown) { + $('#comment-button').bind('click', commentSubmitButtonHandler); + }) + .done(function (data, textStatus, xhr) { + //window.location = '/stories/' + JSON.parse(data).storyLink; + }); + + }; + + $('#comment-button').on('click', commentSubmitButtonHandler); }); var profileValidation = angular.module('profileValidation',['ui.bootstrap']);