move to dashedName lookup

This commit is contained in:
terakilobyte
2015-06-15 14:50:54 -04:00
parent 40ce1d8162
commit a82a8fb044

View File

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