move to dashedName lookup
This commit is contained in:
@ -171,40 +171,24 @@ module.exports = function(app) {
|
|||||||
function returnIndividualChallenge(req, res, next) {
|
function returnIndividualChallenge(req, res, next) {
|
||||||
var dashedName = req.params.challengeName;
|
var dashedName = req.params.challengeName;
|
||||||
|
|
||||||
var challengeName =
|
Challenge.findOne(
|
||||||
(/^(bonfire|waypoint|zipline|basejump)/i).test(dashedName) ?
|
{ dashedName: challengeName },
|
||||||
dashedName
|
function(err, challenge) {
|
||||||
.replace(/\-/g, ' ')
|
|
||||||
.split(' ')
|
|
||||||
.slice(1)
|
|
||||||
.join(' ') :
|
|
||||||
dashedName.replace(/\-/g, ' ');
|
|
||||||
|
|
||||||
Challenge.find(
|
|
||||||
{ where: { name: new RegExp(challengeName, 'i') } },
|
|
||||||
function(err, challengeFromMongo) {
|
|
||||||
if (err) { return next(err); }
|
if (err) { return next(err); }
|
||||||
|
|
||||||
// Handle not found
|
// Handle not found
|
||||||
if (challengeFromMongo.length < 1) {
|
if (!challengeFromMongo) {
|
||||||
req.flash('errors', {
|
req.flash('errors', {
|
||||||
msg: '404: We couldn\'t find a challenge with that name. ' +
|
msg: '404: We couldn\'t find a challenge with that name. ' +
|
||||||
'Please double check the name.'
|
'Please double check the name.'
|
||||||
});
|
});
|
||||||
return res.redirect('/challenges');
|
return res.redirect('/challenges');
|
||||||
}
|
}
|
||||||
var challenge = challengeFromMongo.pop();
|
|
||||||
// Redirect to full name if the user only entered a partial
|
// Redirect to full name if the user only entered a partial
|
||||||
var dashedNameFull = challenge.name
|
if (req.user) {
|
||||||
.toLowerCase()
|
|
||||||
.replace(/\s/g, '-')
|
|
||||||
.replace(/[^a-z0-9\-\.]/gi, '');
|
|
||||||
if (dashedNameFull !== dashedName) {
|
|
||||||
return res.redirect('../challenges/' + dashedNameFull);
|
|
||||||
} else if (req.user) {
|
|
||||||
req.user.currentChallenge = {
|
req.user.currentChallenge = {
|
||||||
challengeId: challenge.id,
|
challengeId: challenge.id,
|
||||||
challengeName: challenge.name,
|
challengeName: challenge.dashedName,
|
||||||
challengeBlock: R.head(R.flatten(Object.keys(challengeMapWithIds).
|
challengeBlock: R.head(R.flatten(Object.keys(challengeMapWithIds).
|
||||||
map(function (key) {
|
map(function (key) {
|
||||||
return challengeMapWithIds[key]
|
return challengeMapWithIds[key]
|
||||||
|
Reference in New Issue
Block a user