Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Q Larson committed Mar 31, 2015
2 parents 8c97528 + 362959e commit 22507de
Showing 1 changed file with 42 additions and 33 deletions.
75 changes: 42 additions & 33 deletions controllers/story.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,27 +119,26 @@ exports.returnIndividualStory = function(req, res, next) {

var storyName = dashedName.replace(/\-/g, ' ');

Story.find({'storyLink' : new RegExp(storyName, 'i')}, function(err, story) {
Story.findOne({'storyLink' : new RegExp(storyName, 'i')}, function(err, story) {
if (err) {
next(err);
}


if (story.length < 1) {
if (story == null) {
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.storyLink.toLowerCase().replace(/\s/g, '-');
if (dashedNameFull !== dashedName) {
return res.redirect('../stories/' + dashedNameFull);
}

var userVoted = false;
var userVoted = false;
try {
var votedObj = story.upVotes.filter(function(a){
return a['upVotedByUsername'] === req.user['profile']['username'];
Expand All @@ -150,7 +149,7 @@ exports.returnIndividualStory = function(req, res, next) {
} catch(err){
userVoted = false;
}
res.render('stories/index', {
res.render('stories/index', {
title: story.headline,
link: story.link,
author: story.author,
Expand All @@ -163,7 +162,7 @@ exports.returnIndividualStory = function(req, res, next) {
image: story.image,
page: 'show',
storyMetaDescription: story.metaDescription,
hasUserVoted: userVoted
hasUserVoted: userVoted
});
});
};
Expand Down Expand Up @@ -308,37 +307,47 @@ exports.storySubmission = function(req, res) {
.replace(/[^a-z0-9]/gi, ' ')
.replace(/\s+/g, ' ')
.toLowerCase();
var link = data.link;
if (link.search(/^https?:\/\//g) === -1) {
link = 'http://' + link;
}
var story = new Story({
headline: sanitizeHtml(data.headline, {
allowedTags: [],
allowedAttributes: []
}).replace(/&quot;/g, '"'),
timePosted: Date.now(),
link: link,
description: sanitizeHtml(data.description, {
allowedTags: [],
allowedAttributes: []
}).replace(/&quot;/g, '"'),
rank: 1,
upVotes: data.upVotes,
author: data.author,
comments: [],
image: data.image,
storyLink: storyLink,
metaDescription: data.storyMetaDescription
});

story.save(function(err) {
Story.count({'storyLink': storyLink}, function(err, storyCount) {
if (err) {
return res.status(500);
}
res.send(JSON.stringify({
storyLink: story.storyLink.replace(/\s/g, '-').toLowerCase()
}));

// if duplicate storyLink add unique number
storyLink = (storyCount == 0) ? storyLink : storyLink + ' ' + storyCount;

var link = data.link;
if (link.search(/^https?:\/\//g) === -1) {
link = 'http://' + link;
}
var story = new Story({
headline: sanitizeHtml(data.headline, {
allowedTags: [],
allowedAttributes: []
}).replace(/&quot;/g, '"'),
timePosted: Date.now(),
link: link,
description: sanitizeHtml(data.description, {
allowedTags: [],
allowedAttributes: []
}).replace(/&quot;/g, '"'),
rank: 1,
upVotes: data.upVotes,
author: data.author,
comments: [],
image: data.image,
storyLink: storyLink,
metaDescription: data.storyMetaDescription
});

story.save(function(err) {
if (err) {
return res.status(500);
}
res.send(JSON.stringify({
storyLink: story.storyLink.replace(/\s/g, '-').toLowerCase()
}));
});
});
};

Expand Down

0 comments on commit 22507de

Please sign in to comment.