diff --git a/controllers/story.js b/controllers/story.js index 82032ba7524..1ec16fc9d2a 100644 --- a/controllers/story.js +++ b/controllers/story.js @@ -119,13 +119,13 @@ 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." }); @@ -133,13 +133,12 @@ exports.returnIndividualStory = function(req, res, next) { 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']; @@ -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, @@ -163,7 +162,7 @@ exports.returnIndividualStory = function(req, res, next) { image: story.image, page: 'show', storyMetaDescription: story.metaDescription, - hasUserVoted: userVoted + hasUserVoted: userVoted }); }); }; @@ -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(/"/g, '"'), - timePosted: Date.now(), - link: link, - description: sanitizeHtml(data.description, { - allowedTags: [], - allowedAttributes: [] - }).replace(/"/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(/"/g, '"'), + timePosted: Date.now(), + link: link, + description: sanitizeHtml(data.description, { + allowedTags: [], + allowedAttributes: [] + }).replace(/"/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() + })); + }); }); };