move to dashedName lookup

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

View File

@ -7,15 +7,15 @@
modification, are permitted provided that the following conditions are met: modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, 1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer. this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, 2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution. and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors 3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software may be used to endorse or promote products derived from this software
without specific prior written permission. without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
@ -31,9 +31,9 @@
*/ */
var R = require('ramda'), var R = require('ramda'),
utils = require('../utils'), utils = require('../utils'),
userMigration = require('../utils/middleware').userMigration, userMigration = require('../utils/middleware').userMigration,
MDNlinks = require('../../seed/bonfireMDNlinks'); MDNlinks = require('../../seed/bonfireMDNlinks');
var challengeMapWithNames = utils.getChallengeMapWithNames(); var challengeMapWithNames = utils.getChallengeMapWithNames();
var challengeMapWithIds = utils.getChallengeMapWithIds(); var challengeMapWithIds = utils.getChallengeMapWithIds();
@ -79,7 +79,7 @@ module.exports = function(app) {
function returnNextChallenge(req, res, next) { function returnNextChallenge(req, res, next) {
if (!req.user) { if (!req.user) {
return res.redirect('../challenges/learn-how-free-code-camp-works'); return res.redirect('../challenges/learn-how-free-code-camp-works');
} }
var completed = req.user.completedChallenges.map(function (elem) { var completed = req.user.completedChallenges.map(function (elem) {
return elem.id; return elem.id;
@ -133,7 +133,7 @@ module.exports = function(app) {
function returnCurrentChallenge(req, res, next) { function returnCurrentChallenge(req, res, next) {
if (!req.user) { if (!req.user) {
return res.redirect('../challenges/learn-how-free-code-camp-works'); return res.redirect('../challenges/learn-how-free-code-camp-works');
} }
var completed = req.user.completedChallenges.map(function (elem) { var completed = req.user.completedChallenges.map(function (elem) {
return elem.id; return elem.id;
@ -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]