refactor challenge and challengeMap

This commit is contained in:
Berkeley Martinez
2015-06-02 19:02:54 -07:00
parent 956a2b915e
commit e7d01424e9
4 changed files with 441 additions and 452 deletions

View File

@@ -57,6 +57,9 @@
"less": "~1.7.5", "less": "~1.7.5",
"less-middleware": "~2.0.1", "less-middleware": "~2.0.1",
"lodash": "~2.4.1", "lodash": "~2.4.1",
"loopback": "^2.18.0",
"loopback-boot": "^2.8.0",
"loopback-connector-mongodb": "^1.10.0",
"lusca": "~1.0.2", "lusca": "~1.0.2",
"method-override": "~2.3.0", "method-override": "~2.3.0",
"moment": "~2.10.2", "moment": "~2.10.2",

View File

@@ -31,33 +31,13 @@
*/ */
var R = require('ramda'), var R = require('ramda'),
express = require('express'),
Challenge = require('../../common/models/Challenge'),
User = require('../../common/models/User'),
resources = require('../resources/resources'), resources = require('../resources/resources'),
userMigration = require('../resources/middleware').userMigration, userMigration = require('../resources/middleware').userMigration,
MDNlinks = require('../../seed_data/bonfireMDNlinks'); MDNlinks = require('../../seed_data/bonfireMDNlinks');
var router = express.Router();
var challengeMapWithNames = resources.getChallengeMapWithNames(); var challengeMapWithNames = resources.getChallengeMapWithNames();
var challengeMapWithIds = resources.getChallengeMapWithIds(); var challengeMapWithIds = resources.getChallengeMapWithIds();
router.get(
'/challenges/next-challenge',
userMigration,
returnNextChallenge
);
router.get(
'/challenges/:challengeName',
userMigration,
returnIndividualChallenge
);
router.get('/challenges/', userMigration, returnCurrentChallenge);
router.post('/completed-challenge/', completedChallenge);
router.post('/completed-zipline-or-basejump', completedZiplineOrBasejump);
router.post('/completed-bonfire', completedBonfire);
function getMDNlinks(links) { function getMDNlinks(links) {
// takes in an array of links, which are strings // takes in an array of links, which are strings
@@ -73,7 +53,29 @@ function getMDNlinks(links) {
return populatedLinks; return populatedLinks;
} }
function returnNextChallenge(req, res, next) { module.exports = function(app) {
var router = app.Router();
var Challenge = app.models.Challenge;
var User = app.models.User;
router.get(
'/challenges/next-challenge',
userMigration,
returnNextChallenge
);
router.get(
'/challenges/:challengeName',
userMigration,
returnIndividualChallenge
);
router.get('/challenges/', userMigration, returnCurrentChallenge);
router.post('/completed-challenge/', completedChallenge);
router.post('/completed-zipline-or-basejump', completedZiplineOrBasejump);
router.post('/completed-bonfire', completedBonfire);
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');
} }
@@ -125,9 +127,9 @@ function returnNextChallenge(req, res, next) {
} }
return res.redirect('../challenges/' + nameString); return res.redirect('../challenges/' + nameString);
}); });
} }
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');
} }
@@ -162,9 +164,9 @@ function returnCurrentChallenge(req, res, next) {
} }
return res.redirect('../challenges/' + nameString); return res.redirect('../challenges/' + nameString);
}); });
} }
function returnIndividualChallenge(req, res, next) { function returnIndividualChallenge(req, res, next) {
var dashedName = req.params.challengeName; var dashedName = req.params.challengeName;
var challengeName = var challengeName =
@@ -176,11 +178,11 @@ function returnIndividualChallenge(req, res, next) {
.join(' ') : .join(' ') :
dashedName.replace(/\-/g, ' '); dashedName.replace(/\-/g, ' ');
Challenge.find({'name': new RegExp(challengeName, 'i')}, Challenge.find(
{ where: { name: new RegExp(challengeName, 'i') } },
function(err, challengeFromMongo) { function(err, challengeFromMongo) {
if (err) { if (err) { return next(err); }
return next(err);
}
// Handle not found // Handle not found
if (challengeFromMongo.length < 1) { if (challengeFromMongo.length < 1) {
req.flash('errors', { req.flash('errors', {
@@ -329,9 +331,9 @@ function returnIndividualChallenge(req, res, next) {
return challengeType[challenge.challengeType](); return challengeType[challenge.challengeType]();
} }
}); });
} }
function completedBonfire(req, res, next) { function completedBonfire(req, res, next) {
var isCompletedWith = req.body.challengeInfo.completedWith || ''; var isCompletedWith = req.body.challengeInfo.completedWith || '';
var isCompletedDate = Math.round(+new Date()); var isCompletedDate = Math.round(+new Date());
var challengeId = req.body.challengeInfo.challengeId; var challengeId = req.body.challengeInfo.challengeId;
@@ -339,12 +341,12 @@ function completedBonfire(req, res, next) {
var challengeName = req.body.challengeInfo.challengeName; var challengeName = req.body.challengeInfo.challengeName;
if (isCompletedWith) { if (isCompletedWith) {
var paired = User.find({'profile.username': isCompletedWith.toLowerCase()}) User.find({
.limit(1); where: { 'profile.username': isCompletedWith.toLowerCase() },
paired.exec(function (err, pairedWith) { limit: 1
if (err) { }, function (err, pairedWith) {
return next(err); if (err) { return next(err); }
} else {
var index = req.user.uncompletedChallenges.indexOf(challengeId); var index = req.user.uncompletedChallenges.indexOf(challengeId);
if (index > -1) { if (index > -1) {
req.user.progressTimestamps.push(Date.now() || 0); req.user.progressTimestamps.push(Date.now() || 0);
@@ -388,11 +390,9 @@ function completedBonfire(req, res, next) {
challengeType: 5 challengeType: 5
}); });
req.user.save(function (err, user) { req.user.save(function (err, user) {
if (err) { if (err) { return next(err); }
return next(err);
}
if (pairedWith) { if (pairedWith) {
pairedWith.save(function (err, paired) { pairedWith.save(function (err, paired) {
if (err) { if (err) {
@@ -406,7 +406,6 @@ function completedBonfire(req, res, next) {
res.send(true); res.send(true);
} }
}); });
}
}); });
} else { } else {
req.user.completedChallenges.push({ req.user.completedChallenges.push({
@@ -425,19 +424,14 @@ function completedBonfire(req, res, next) {
req.user.uncompletedChallenges.splice(index, 1); req.user.uncompletedChallenges.splice(index, 1);
} }
req.user.save(function (err, user) { req.user.save(function (err) {
if (err) { if (err) { return next(err); }
return next(err);
}
// NOTE(berks): Under certain conditions the res is never ended
if (user) {
res.send(true); res.send(true);
}
}); });
} }
} }
function completedChallenge(req, res, next) { function completedChallenge(req, res, next) {
var isCompletedDate = Math.round(+new Date()); var isCompletedDate = Math.round(+new Date());
var challengeId = req.body.challengeInfo.challengeId; var challengeId = req.body.challengeInfo.challengeId;
@@ -465,9 +459,9 @@ function completedChallenge(req, res, next) {
res.sendStatus(200); res.sendStatus(200);
} }
}); });
} }
function completedZiplineOrBasejump(req, res, next) { function completedZiplineOrBasejump(req, res, next) {
var isCompletedWith = req.body.challengeInfo.completedWith || false; var isCompletedWith = req.body.challengeInfo.completedWith || false;
var isCompletedDate = Math.round(+new Date()); var isCompletedDate = Math.round(+new Date());
@@ -486,13 +480,11 @@ function completedZiplineOrBasejump(req, res, next) {
} }
if (isCompletedWith) { if (isCompletedWith) {
var paired = User.find({'profile.username': isCompletedWith.toLowerCase()}) User.find({
.limit(1); where: { 'profile.username': isCompletedWith.toLowerCase() },
limit: 1
paired.exec(function (err, pairedWithFromMongo) { }, function (err, pairedWithFromMongo) {
if (err) { if (err) { return next(err); }
return next(err);
} else {
var index = req.user.uncompletedChallenges.indexOf(challengeId); var index = req.user.uncompletedChallenges.indexOf(challengeId);
if (index > -1) { if (index > -1) {
req.user.progressTimestamps.push(Date.now() || 0); req.user.progressTimestamps.push(Date.now() || 0);
@@ -512,9 +504,7 @@ function completedZiplineOrBasejump(req, res, next) {
}); });
req.user.save(function (err, user) { req.user.save(function (err, user) {
if (err) { if (err) { return next(err); }
return next(err);
}
if (req.user._id.toString() === pairedWith._id.toString()) { if (req.user._id.toString() === pairedWith._id.toString()) {
return res.sendStatus(200); return res.sendStatus(200);
@@ -545,7 +535,6 @@ function completedZiplineOrBasejump(req, res, next) {
} }
}); });
}); });
}
}); });
} else { } else {
@@ -570,12 +559,12 @@ function completedZiplineOrBasejump(req, res, next) {
if (err) { if (err) {
return next(err); return next(err);
} }
// NOTE(berks): under certain conditions this will not close the response. // NOTE(berks): under certain conditions this will not close
// the response.
if (user) { if (user) {
return res.sendStatus(200); return res.sendStatus(200);
} }
}); });
} }
} }
};
module.exports = router;

View File

@@ -1,22 +1,22 @@
var R = require('ramda'), var R = require('ramda'),
express = require('express'),
// debug = require('debug')('freecc:cntr:challengeMap'), // debug = require('debug')('freecc:cntr:challengeMap'),
User = require('../../common/models/User'),
resources = require('./../resources/resources'), resources = require('./../resources/resources'),
middleware = require('../resources/middleware'), middleware = require('../resources/middleware');
router = express.Router();
router.get('/map', middleware.userMigration, challengeMap);
router.get('/learn-to-code', function(req, res) { module.exports = function(app) {
var User = app.models.User;
var router = app.Router();
router.get('/map', middleware.userMigration, challengeMap);
router.get('/learn-to-code', function(req, res) {
res.redirect(301, '/map'); res.redirect(301, '/map');
}); });
router.get('/about', function(req, res) {
router.get('/about', function(req, res) {
res.redirect(301, '/map'); res.redirect(301, '/map');
}); });
function challengeMap(req, res, next) { function challengeMap(req, res, next) {
var completedList = []; var completedList = [];
if (req.user) { if (req.user) {
@@ -48,10 +48,9 @@ function challengeMap(req, res, next) {
var timeDiff = Math.abs(date2.getTime() - date1.getTime()); var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var daysRunning = Math.ceil(timeDiff / (1000 * 3600 * 24)); var daysRunning = Math.ceil(timeDiff / (1000 * 3600 * 24));
User.count({}, function (err, camperCount) { User.count(function(err, camperCount) {
if (err) { if (err) { return next(err); }
return next(err);
}
res.render('challengeMap/show', { res.render('challengeMap/show', {
daysRunning: daysRunning, daysRunning: daysRunning,
camperCount: numberWithCommas(camperCount), camperCount: numberWithCommas(camperCount),
@@ -60,6 +59,5 @@ function challengeMap(req, res, next) {
completedChallengeList: completedChallengeList completedChallengeList: completedChallengeList
}); });
}); });
} }
};
module.exports = router;

View File

@@ -1,10 +1,10 @@
var express = require('express'); module.exports = function(app) {
var Job = require('../../common/models/Job'); var Job = app.models.Job;
var router = express.Router(); var router = app.Router();
router.get('/jobs', jobsDirectory); router.get('/jobs', jobsDirectory);
function jobsDirectory(req, res, next) { function jobsDirectory(req, res, next) {
Job.find({}, function(err, jobs) { Job.find({}, function(err, jobs) {
if (err) { return next(err); } if (err) { return next(err); }
@@ -13,6 +13,5 @@ function jobsDirectory(req, res, next) {
jobs: jobs jobs: jobs
}); });
}); });
} }
};
module.exports = router;