2015-03-03 01:41:32 -08:00
|
|
|
var R = require('ramda'),
|
|
|
|
debug = require('debug')('freecc:cntr:post'),
|
2015-03-03 19:23:56 +09:00
|
|
|
Story = require('./../models/Story'),
|
2015-03-03 01:41:32 -08:00
|
|
|
Comment = require('./../models/Comment'),
|
|
|
|
User = require('./../models/User'),
|
|
|
|
resources = require('./resources');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Post Controller
|
|
|
|
*/
|
|
|
|
|
2015-03-03 19:23:56 +09:00
|
|
|
exports.json = function(req, res, next) {
|
|
|
|
var story = Story.find({}).sort({'rank': -1});
|
|
|
|
story.exec(function(err, stories) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
res.json(stories);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-03-03 01:41:32 -08:00
|
|
|
exports.index = function(req, res, next) {
|
2015-03-03 19:23:56 +09:00
|
|
|
var story = Story.find({}).sort({'rank': -1});
|
|
|
|
story.exec(function(err, stories) {
|
2015-03-03 01:41:32 -08:00
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
2015-03-03 19:23:56 +09:00
|
|
|
res.render('post/index');
|
2015-03-03 01:41:32 -08:00
|
|
|
});
|
2015-03-03 19:23:56 +09:00
|
|
|
};
|
2015-03-03 19:50:16 +09:00
|
|
|
|
|
|
|
exports.returnIndividualStory = function(req, res, next) {
|
|
|
|
var dashedName = req.params.storyName;
|
|
|
|
|
|
|
|
storyName = dashedName.replace(/\-/g, ' ');
|
|
|
|
|
|
|
|
Story.find({'headline' : new RegExp(storyName, 'i')}, function(err, story) {
|
|
|
|
if (err) {
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (story.length < 1) {
|
|
|
|
req.flash('errors', {
|
|
|
|
msg: "404: We couldn't find a story with that name. Please double check the name."
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.redirect('/stories/');
|
|
|
|
}
|
|
|
|
|
|
|
|
story = story.pop();
|
|
|
|
var dashedNameFull = story.headline.toLowerCase().replace(/\s/g, '-');
|
2015-03-03 22:03:33 +09:00
|
|
|
if (dashedNameFull !== dashedName) {
|
2015-03-03 19:50:16 +09:00
|
|
|
return res.redirect('../stories/' + dashedNameFull);
|
|
|
|
}
|
2015-03-04 07:15:00 +09:00
|
|
|
debug('Story', story);
|
2015-03-03 19:50:16 +09:00
|
|
|
|
|
|
|
res.render('post/show', {
|
|
|
|
title: story.headline,
|
|
|
|
dashedName: story.link,
|
|
|
|
author: story.author,
|
|
|
|
body: story.body,
|
|
|
|
rank: story.rank,
|
|
|
|
upVotes: story.upVotes,
|
2015-03-03 22:03:33 +09:00
|
|
|
comments: story.comments,
|
2015-03-04 07:15:00 +09:00
|
|
|
id: story._id,
|
|
|
|
user: req.user
|
2015-03-03 19:50:16 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2015-03-03 22:03:33 +09:00
|
|
|
|
|
|
|
exports.upvote = function(req, res, next) {
|
2015-03-04 07:15:00 +09:00
|
|
|
var data = req.body.data;
|
|
|
|
Story.find({'_id': data.id}, function(err, story) {
|
2015-03-03 22:03:33 +09:00
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
story = story.pop();
|
|
|
|
story.rank++;
|
2015-03-04 07:15:00 +09:00
|
|
|
story.upVotes.push(
|
|
|
|
{
|
|
|
|
upVotedBy: data.upVoter._id,
|
|
|
|
upVotedByUsername: data.upVoter.profile.username
|
|
|
|
}
|
|
|
|
);
|
2015-03-03 22:03:33 +09:00
|
|
|
story.save();
|
|
|
|
return res.send(story);
|
|
|
|
});
|
|
|
|
};
|
2015-03-04 07:15:00 +09:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
};
|