Skip to content

Commit

Permalink
added .findByIdForUser
Browse files Browse the repository at this point in the history
  • Loading branch information
abarnhard committed Aug 26, 2014
1 parent 977a051 commit 86e3ec2
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 1 deletion.
10 changes: 10 additions & 0 deletions app/controllers/goals.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ exports.index = function(req, res){
});
};

exports.show = function(req, res){
Goal.findByIdForUser(req.params.id, res.locals.user._id, function(err, goal){
if(goal){
res.render('goals/show', {goal:goal, moment:moment});
}else{
res.redirect('/');
}
});
};

13 changes: 13 additions & 0 deletions app/models/goal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var Mongo = require('mongodb');

function Goal(o){
this.name = o.name;
this.due = new Date(o.due);
Expand All @@ -20,5 +22,16 @@ Goal.findAllByUserId = function(id, cb){
Goal.collection.find({userId:id}).toArray(cb);
};

Goal.findByIdForUser = function(goalId, userId, cb){
goalId = Mongo.ObjectID(goalId);
Goal.collection.findOne({_id:goalId}, function(err, goal){
if(goal.userId.toString() === userId.toString()){
cb(err, goal);
}else{
cb('ERROR:INVALID USER ID', null);
}
});
};

module.exports = Goal;

1 change: 1 addition & 0 deletions app/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports = function(app, express){
app.get('/goals/new', goals.new);
app.post('/goals', goals.create);
app.get('/goals', goals.index);
app.get('/goals/:id', goals.show);

console.log('Express: Routes Loaded');
};
Expand Down
2 changes: 1 addition & 1 deletion app/views/goals/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ block content
tbody
each goal in goals
tr
td= goal.name
td: a(href='/goals/#{goal._id}')= goal.name
td= moment(goal.due).format('ll')
td= goal.tags

Expand Down
17 changes: 17 additions & 0 deletions app/views/goals/show.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
extends ../shared/template
block content
h2= goal.name
.panel.panel-default
.panel-body
table.table
thead
tr
th Due
th Tags
tbody
tr
td= moment(goal.due).format('ll')
td= goal.tags

block scripts

18 changes: 18 additions & 0 deletions test/acceptance/goals.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,22 @@ describe('goals', function(){
});
});

describe('get /goals/3', function(){
it('should show a specific goals page', function(done){
request(app).get('/goals/a00000000000000000000002').set('cookie', cookie).end(function(err, res){
// console.log(res);
expect(res.status).to.equal(200);
expect(res.text).to.include('marathon');
done();
});
});
it('should expect to get a 302 (not logged in users goal)', function(done){
request(app).get('/goals/a00000000000000000000003').set('cookie', cookie).end(function(err, res){
// console.log(res);
expect(res.status).to.equal(302);
done();
});
});
});

});
19 changes: 19 additions & 0 deletions test/unit/goal.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,24 @@ describe('Goal', function(){
});
});

describe('.findByIdForUser', function(){
it('should return goal (user IDs match)', function(done){
var goalId = 'a00000000000000000000002',
userId = Mongo.ObjectID('000000000000000000000001');
Goal.findByIdForUser(goalId, userId, function(err, goal){
expect(goal).to.be.ok;
done();
});
});
it('should return null (user IDs don\'t match', function(done){
var goalId = 'a00000000000000000000003',
userId = Mongo.ObjectID('000000000000000000000001');
Goal.findByIdForUser(goalId, userId, function(err, goal){
expect(goal).to.be.null;
done();
});
});
});

});

0 comments on commit 86e3ec2

Please sign in to comment.