2015-03-03 09:41:32 +00:00
|
|
|
var R = require('ramda'),
|
2015-03-04 03:55:04 +00:00
|
|
|
debug = require('debug')('freecc:cntr:story'),
|
2015-03-03 10:23:56 +00:00
|
|
|
Story = require('./../models/Story'),
|
2015-03-03 09:41:32 +00:00
|
|
|
Comment = require('./../models/Comment'),
|
|
|
|
User = require('./../models/User'),
|
2015-03-04 03:55:04 +00:00
|
|
|
moment = require('../public/js/lib/moment/moment.js'),
|
2015-03-03 09:41:32 +00:00
|
|
|
resources = require('./resources');
|
|
|
|
|
2015-03-03 10:23:56 +00: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 09:41:32 +00:00
|
|
|
exports.index = function(req, res, next) {
|
2015-03-03 10:23:56 +00:00
|
|
|
var story = Story.find({}).sort({'rank': -1});
|
|
|
|
story.exec(function(err, stories) {
|
2015-03-03 09:41:32 +00:00
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
2015-03-04 03:55:04 +00:00
|
|
|
res.render('stories/index');
|
2015-03-03 09:41:32 +00:00
|
|
|
});
|
2015-03-03 10:23:56 +00:00
|
|
|
};
|
2015-03-03 10:50:16 +00: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 13:03:33 +00:00
|
|
|
if (dashedNameFull !== dashedName) {
|
2015-03-03 10:50:16 +00:00
|
|
|
return res.redirect('../stories/' + dashedNameFull);
|
|
|
|
}
|
2015-03-03 22:15:00 +00:00
|
|
|
debug('Story', story);
|
2015-03-03 10:50:16 +00:00
|
|
|
|
2015-03-04 03:55:04 +00:00
|
|
|
res.render('stories/show', {
|
2015-03-03 10:50:16 +00:00
|
|
|
title: story.headline,
|
2015-03-04 03:55:04 +00:00
|
|
|
link: story.link,
|
2015-03-03 10:50:16 +00:00
|
|
|
author: story.author,
|
|
|
|
body: story.body,
|
|
|
|
rank: story.rank,
|
|
|
|
upVotes: story.upVotes,
|
2015-03-03 13:03:33 +00:00
|
|
|
comments: story.comments,
|
2015-03-03 22:15:00 +00:00
|
|
|
id: story._id,
|
2015-03-04 03:55:04 +00:00
|
|
|
user: req.user,
|
|
|
|
timeAgo: moment(story.timePosted).fromNow()
|
2015-03-03 10:50:16 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2015-03-03 13:03:33 +00:00
|
|
|
|
|
|
|
exports.upvote = function(req, res, next) {
|
2015-03-03 22:15:00 +00:00
|
|
|
var data = req.body.data;
|
|
|
|
Story.find({'_id': data.id}, function(err, story) {
|
2015-03-03 13:03:33 +00:00
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
story = story.pop();
|
|
|
|
story.rank++;
|
2015-03-03 22:15:00 +00:00
|
|
|
story.upVotes.push(
|
|
|
|
{
|
|
|
|
upVotedBy: data.upVoter._id,
|
|
|
|
upVotedByUsername: data.upVoter.profile.username
|
|
|
|
}
|
|
|
|
);
|
2015-03-03 13:03:33 +00:00
|
|
|
story.save();
|
|
|
|
return res.send(story);
|
|
|
|
});
|
|
|
|
};
|
2015-03-03 22:15:00 +00: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);
|
|
|
|
});
|
|
|
|
};
|