Refactor story.js to use sendStatus instead of status, ensure returning on next(err)

This commit is contained in:
Nathan Leniz
2015-03-24 18:26:02 +09:00
parent 999ba14db7
commit d0a00428d5

View File

@ -1,3 +1,4 @@
/* eslint-disable no-catch-shadow, no-unused-vars */
var R = require('ramda'), var R = require('ramda'),
debug = require('debug')('freecc:cntr:story'), debug = require('debug')('freecc:cntr:story'),
Story = require('./../models/Story'), Story = require('./../models/Story'),
@ -28,7 +29,7 @@ exports.hotJSON = function(req, res) {
var story = Story.find({}).sort({'timePosted': -1}).limit(1000); var story = Story.find({}).sort({'timePosted': -1}).limit(1000);
story.exec(function(err, stories) { story.exec(function(err, stories) {
if (err) { if (err) {
return res.status(500); return res.sendStatus(500);
} }
var foundationDate = 1413298800000; var foundationDate = 1413298800000;
@ -48,36 +49,36 @@ exports.recentJSON = function(req, res, next) {
var story = Story.find({}).sort({'timePosted': -1}).limit(100); var story = Story.find({}).sort({'timePosted': -1}).limit(100);
story.exec(function(err, stories) { story.exec(function(err, stories) {
if (err) { if (err) {
res.status(500); res.sendStatus(500);
return next(err); return next(err);
} }
res.json(stories); return res.json(stories);
}); });
}; };
exports.hot = function(req, res) { exports.hot = function(req, res) {
res.render('stories/index', { return res.render('stories/index', {
title: 'Hot stories currently trending on Camper News', title: 'Hot stories currently trending on Camper News',
page: 'hot' page: 'hot'
}); });
}; };
exports.submitNew = function(req, res) { exports.submitNew = function(req, res) {
res.render('stories/index', { return res.render('stories/index', {
title: 'Submit a new story to Camper News', title: 'Submit a new story to Camper News',
page: 'submit' page: 'submit'
}); });
}; };
exports.search = function(req, res) { exports.search = function(req, res) {
res.render('stories/index', { return res.render('stories/index', {
title: 'Search the archives of Camper News', title: 'Search the archives of Camper News',
page: 'search' page: 'search'
}); });
}; };
exports.recent = function(req, res) { exports.recent = function(req, res) {
res.render('stories/index', { return res.render('stories/index', {
title: 'Recently submitted stories on Camper News', title: 'Recently submitted stories on Camper News',
page: 'recent' page: 'recent'
}); });
@ -121,7 +122,7 @@ exports.returnIndividualStory = function(req, res, next) {
Story.find({'storyLink': new RegExp(storyName, 'i')}, function(err, story) { Story.find({'storyLink': new RegExp(storyName, 'i')}, function(err, story) {
if (err) { if (err) {
next(err); return next(err);
} }
@ -171,7 +172,7 @@ exports.returnIndividualStory = function(req, res, next) {
exports.getStories = function(req, res) { exports.getStories = function(req, res) {
MongoClient.connect(secrets.db, function(err, database) { MongoClient.connect(secrets.db, function(err, database) {
if (err) { if (err) {
return res.status(500); return res.sendStatus(500);
} }
database.collection('stories').find({ database.collection('stories').find({
'$text': { '$text': {
@ -200,12 +201,12 @@ exports.getStories = function(req, res) {
} }
}).toArray(function(err, items) { }).toArray(function(err, items) {
if (err) { if (err) {
return res.status(500); return res.sendStatus(500);
} }
if (items !== null && items.length !== 0) { if (items !== null && items.length !== 0) {
return res.json(items); return res.json(items);
} }
return res.status(404); return res.sendStatus(404);
}); });
}); });
}; };
@ -214,7 +215,7 @@ exports.upvote = function(req, res, next) {
var data = req.body.data; var data = req.body.data;
Story.find({'_id': data.id}, function(err, story) { Story.find({'_id': data.id}, function(err, story) {
if (err) { if (err) {
res.status(500); res.sendStatus(500);
return next(err); return next(err);
} }
story = story.pop(); story = story.pop();
@ -244,7 +245,7 @@ exports.comments = function(req, res, next) {
var data = req.params.id; var data = req.params.id;
Comment.find({'_id': data}, function(err, comment) { Comment.find({'_id': data}, function(err, comment) {
if (err) { if (err) {
res.status(500); res.sendStatus(500);
return next(err); return next(err);
} }
comment = comment.pop(); comment = comment.pop();
@ -254,7 +255,7 @@ exports.comments = function(req, res, next) {
exports.newStory = function(req, res) { exports.newStory = function(req, res) {
if (!req.user) { if (!req.user) {
return res.status(500); return res.sendStatus(500);
} }
var url = req.body.data.url; var url = req.body.data.url;
var cleanURL = sanitizeHtml(url, { var cleanURL = sanitizeHtml(url, {
@ -276,7 +277,7 @@ exports.newStory = function(req, res) {
} }
Story.find({'link': url}, function(err, story) { Story.find({'link': url}, function(err, story) {
if (err) { if (err) {
return res.status(500); return res.sendStatus(500);
} }
if (story.length) { if (story.length) {
req.flash('errors', { req.flash('errors', {
@ -314,7 +315,7 @@ exports.newStory = function(req, res) {
exports.storySubmission = function(req, res) { exports.storySubmission = function(req, res) {
var data = req.body.data; var data = req.body.data;
if (req.user._id.toString() !== data.author.userId.toString()) { if (req.user._id.toString() !== data.author.userId.toString()) {
return res.status(500); return res.sendStatus(500);
} }
var storyLink = data.headline var storyLink = data.headline
.replace(/\'/g, '') .replace(/\'/g, '')
@ -352,7 +353,7 @@ exports.storySubmission = function(req, res) {
story.save(function(err) { story.save(function(err) {
if (err) { if (err) {
return res.status(500); return res.sendStatus(500);
} }
res.send(JSON.stringify({ res.send(JSON.stringify({
storyLink: story.storyLink.replace(/\s/g, '-').toLowerCase() storyLink: story.storyLink.replace(/\s/g, '-').toLowerCase()
@ -363,7 +364,7 @@ exports.storySubmission = function(req, res) {
exports.commentSubmit = function(req, res) { exports.commentSubmit = function(req, res) {
var data = req.body.data; var data = req.body.data;
if (req.user._id.toString() !== data.author.userId.toString()) { if (req.user._id.toString() !== data.author.userId.toString()) {
return res.status(500); return res.sendStatus(500);
} }
var sanitizedBody = sanitizeHtml(data.body, var sanitizedBody = sanitizeHtml(data.body,
{ {
@ -393,7 +394,7 @@ exports.commentOnCommentSubmit = function(req, res) {
var data = req.body.data; var data = req.body.data;
if (req.user._id.toString() !== data.author.userId.toString()) { if (req.user._id.toString() !== data.author.userId.toString()) {
return res.status(500); return res.sendStatus(500);
} }
var sanitizedBody = sanitizeHtml(data.body, var sanitizedBody = sanitizeHtml(data.body,
@ -423,19 +424,19 @@ exports.commentOnCommentSubmit = function(req, res) {
function commentSave(comment, Context, res) { function commentSave(comment, Context, res) {
comment.save(function(err, data) { comment.save(function(err, data) {
if (err) { if (err) {
return res.status(500); return res.sendStatus(500);
} }
try { try {
Context.find({'_id': comment.associatedPost}, function (err, associatedStory) { Context.find({'_id': comment.associatedPost}, function (err, associatedStory) {
if (err) { if (err) {
return res.status(500); return res.sendStatus(500);
} }
associatedStory = associatedStory.pop(); associatedStory = associatedStory.pop();
if (associatedStory) { if (associatedStory) {
associatedStory.comments.push(data._id); associatedStory.comments.push(data._id);
associatedStory.save(function (err) { associatedStory.save(function (err) {
if (err) { if (err) {
res.status(500); return res.sendStatus(500);
} }
res.send(true); res.send(true);
}); });
@ -443,7 +444,7 @@ function commentSave(comment, Context, res) {
}); });
} catch (e) { } catch (e) {
// delete comment // delete comment
return res.status(500); return res.sendStatus(500);
} }
}); });
} }