diff --git a/app.js b/app.js index 21b596fcab..a4365992f8 100644 --- a/app.js +++ b/app.js @@ -288,6 +288,10 @@ app.get( '/stories/index', storyController.json ); +app.get( + '/stories/:storyName', + storyController.returnIndividualStory +); /** * Challenge related routes diff --git a/controllers/story.js b/controllers/story.js index 79c9ffd113..e454a91ea2 100644 --- a/controllers/story.js +++ b/controllers/story.js @@ -28,3 +28,40 @@ exports.index = function(req, res, next) { res.render('post/index'); }); }; + +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, '-'); + if (dashedNameFull != dashedName) { + return res.redirect('../stories/' + dashedNameFull); + } + + res.render('post/show', { + title: story.headline, + dashedName: story.link, + author: story.author, + body: story.body, + rank: story.rank, + upVotes: story.upVotes, + comments: story.comments + }); + }); +}; diff --git a/views/post/show.jade b/views/post/show.jade new file mode 100644 index 0000000000..f8fdb58a61 --- /dev/null +++ b/views/post/show.jade @@ -0,0 +1,17 @@ +extends ../layout +block content + h1= title + h3= body + h5= rank + + + + +// + title: story.headline, + dashedName: story.link, + author: story.author, + body: story.body, + rank: story.rank, + upVotes: story.upVotes, + comments: story.comments \ No newline at end of file