On story submission create unique story URL if slug already exists

pull/291/head
jameskopacz 2015-03-30 23:14:39 -05:00
parent b6dc82805d
commit f5d6abdfef
1 changed files with 42 additions and 33 deletions

View File

@ -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,7 +133,6 @@ 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);
@ -308,6 +307,15 @@ exports.storySubmission = function(req, res) {
.replace(/[^a-z0-9]/gi, ' ')
.replace(/\s+/g, ' ')
.toLowerCase();
Story.count({'storyLink': storyLink}, function(err, storyCount) {
if (err) {
return res.status(500);
}
// 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;
@ -340,6 +348,7 @@ exports.storySubmission = function(req, res) {
storyLink: story.storyLink.replace(/\s/g, '-').toLowerCase()
}));
});
});
};
exports.commentSubmit = function(req, res) {