From d973619f4a8474e28c3de6a4b93b6b08d4c11c95 Mon Sep 17 00:00:00 2001 From: Michael Q Larson Date: Tue, 25 Nov 2014 11:17:07 -0800 Subject: [PATCH] writes completed challenges to database --- app.js | 26 +++++++++++++++++++++++--- models/User.js | 2 +- views/partials/challenges.jade | 4 ++-- views/partials/navbar.jade | 2 +- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/app.js b/app.js index 2f027fb525..5f57bf1a4e 100644 --- a/app.js +++ b/app.js @@ -33,6 +33,10 @@ var userController = require('./controllers/user'); var apiController = require('./controllers/api'); var contactController = require('./controllers/contact'); +/** + * User model + */ +var User = require('./models/User'); /** * API keys and Passport configuration. */ @@ -163,9 +167,25 @@ app.get('/account/unlink/:provider', userController.getOauthUnlink); * API examples routes. */ app.post('/completed_challenge', function(req, res) { - req.user.challengesHash[req.body.cn] = Math.round(+new Date()/1000); - req.user.save(); - console.log(req.user.challengesHash); + console.log(req.user); + + User.findById(req.user.id, function (err, user) { + if (err) { + console.log(err); + res.status(500); + return res.send('something went wrong'); + } + user.challengesHash[parseInt(req.body.cn)] = Math.round(+new Date()/1000); + user.save(function(err, newDats) { + if (err) { + console.log(err); + res.status(500); + return res.send('something went wrong'); + } + console.log('Suer', user); + res.status(200).send('saved'); + }); + }); }); /** diff --git a/models/User.js b/models/User.js index 8add8e69e3..00d31b3365 100644 --- a/models/User.js +++ b/models/User.js @@ -14,7 +14,7 @@ var userSchema = new mongoose.Schema({ instagram: String, tokens: Array, challengesCompleted: { type: Array, default: [] }, - challengesHash: { type: {}, default: { '-1': 'void'} }, + challengesHash: {}, profile: { name: { type: String, default: '' }, diff --git a/views/partials/challenges.jade b/views/partials/challenges.jade index 78ff771a06..2d3e9c5b88 100644 --- a/views/partials/challenges.jade +++ b/views/partials/challenges.jade @@ -3,8 +3,8 @@ .panel-body ol(start='0') li - a(href="/challenges/0", class="#{ cc.hasOwnProperty('0') ? 'strikethrough' : '' }") A One-minute Introduction to Free Code Camp - |   (1 min) + //a(href="/challenges/0", class="#{ cc.hasOwnProperty('0') ? 'strikethrough' : '' }") A One-minute Introduction to Free Code Camp + //|   (1 min) //li // a(href="/challenges/1", class="#{ cc.indexOf(1) > -1 ? 'strikethrough' : '' }") Enter the Free Code Camp Chat Room // |   (5 mins) diff --git a/views/partials/navbar.jade b/views/partials/navbar.jade index 9fa4f46a35..471aceef08 100644 --- a/views/partials/navbar.jade +++ b/views/partials/navbar.jade @@ -20,7 +20,7 @@ img(src='#{user.profile.picture}') else img(src='#{user.gravatar(60)}') - | #{user.profile.name || user.email || user.id} [ #{Object.keys(user.challengesHash).length} ]  + //| #{user.profile.name || user.email || user.id} [ #{Object.keys(user.challengesHash).length} ]  i.caret ul.dropdown-menu li