Skip to content

Commit

Permalink
added .addTask
Browse files Browse the repository at this point in the history
  • Loading branch information
abarnhard committed Aug 26, 2014
1 parent 88ac357 commit 0c5ad73
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 11 deletions.
6 changes: 6 additions & 0 deletions app/controllers/goals.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ exports.show = function(req, res){
});
};

exports.addTask = function(req, res){
Goal.addTask(req.body, req.params.id, res.locals.user._id, function(){
res.redirect('/goals/' + req.params.id);
});
};

19 changes: 18 additions & 1 deletion app/models/goal.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
'use strict';

var Mongo = require('mongodb');
var Mongo = require('mongodb'),
Task = require('./task');

function Goal(o){
this.name = o.name;
this.due = new Date(o.due);
this.tags = o.tags.split(',').map(function(tag){return tag.trim();});
this.userId = o.userId;
this.tasks = [];
}

Object.defineProperty(Goal, 'collection', {
Expand All @@ -27,5 +29,20 @@ Goal.findByIdForUser = function(goalId, userId, cb){
Goal.collection.findOne({_id:goalId, userId:userId}, cb);
};

Goal.addTask = function(data, goalId, userId, cb){
Goal.findByIdForUser(goalId, userId, function(err, goal){
// console.log('*********', goal);
if(!goal){return cb();}

var t = new Task(data);
// console.log('*********', t);
goal.tasks.push(t);
Goal.collection.save(goal, function(){
cb(null, goal);
});

});
};

module.exports = Goal;

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

function Task(o){
this.name = o.name;
this.difficulty = o.difficulty;
this.description = o.description;
this.rank = o.rank * 1;
this.isComplete = false;
}

module.exports = Task;

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

console.log('Express: Routes Loaded');
};
Expand Down
42 changes: 33 additions & 9 deletions app/views/goals/show.jade
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,39 @@ 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
.row
.col-xs-12
table.table
thead
tr
th Due
tbody
tr
td= moment(goal.due).format('ll')
.row
.col-xs-12
| Tasks will go here
.row
.col-xs-2
.col-xs-8
form(role='form', method='post', action='/goals/#{goal._id}/tasks')
.form-group
label Name
input.form-control(name='name', type='text', placeholder='Step 1')
.form-group
label Description
input.form-control(name='description', type='text', placeholder='How to do step 1')
.form-group
label Difficulty
select.form-control(name='difficulty')
option Easy
option Medium
option Hard
.form-group
label Rank
input.form-control(name='rank', type='number')
button.btn.btn-primary(type='submit') Submit
.col-xs-2

block scripts

3 changes: 3 additions & 0 deletions db/goals.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
"name" : "Be a plumber",
"due" : {"$date":1422856800000},
"tags" : ["job", "career"],
"tasks" : [],
"userId" : {"$oid":"000000000000000000000001"}
}
{
"_id" : {"$oid":"a00000000000000000000002"},
"name" : "Run marathon",
"due" : {"$date":1422856800000},
"tags" : ["fitness", "health"],
"tasks" : [],
"userId" : {"$oid":"000000000000000000000001"}
}
{
"_id" : {"$oid":"a00000000000000000000003"},
"name" : "Run marathon",
"due" : {"$date":1422856800000},
"tags" : ["fitness", "health"],
"tasks" : [],
"userId" : {"$oid":"000000000000000000000002"}
}

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

describe('post /goals/3/tasks', function(){
it('should create a new task for a specific goal', function(done){
request(app).post('/goals/a00000000000000000000002/tasks').set('cookie', cookie).send('name=Get+overalls&description=go+buy+overalls&difficulty=Medium&rank=5').end(function(err, res){
// console.log(res);
expect(res.status).to.equal(302);
done();
});
});
});

});
25 changes: 24 additions & 1 deletion test/unit/goal.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('Goal', function(){
expect(g.name).to.equal('Be a Plumber');
expect(g.due).to.be.instanceof(Date);
expect(g.tags).to.have.length(2);
expect(g.tasks).to.have.length(0);
done();
});
});
Expand All @@ -57,7 +58,7 @@ describe('Goal', function(){
done();
});
});
it('should return null (user IDs don\'t match', function(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){
Expand All @@ -67,5 +68,27 @@ describe('Goal', function(){
});
});

describe('.addTask', function(){
it('should add a task to users goal', function(done){
var goalId = 'a00000000000000000000002',
body = {name:'Buy Coveralls', difficulty:'Hard', description:'Go to store & buy', rank:'4'},
userId = Mongo.ObjectID('000000000000000000000001');
Goal.addTask(body, goalId, userId, function(err, goal){
expect(goal.tasks).to.have.length(1);
expect(goal.tasks[0].rank).to.equal(4);
done();
});
});
it('should not add a task to non-logged in users goal', function(done){
var goalId = 'a00000000000000000000003',
body = {name:'Buy Coveralls', difficulty:'Hard', description:'Go to store & buy', rank:'4'},
userId = Mongo.ObjectID('000000000000000000000001');
Goal.addTask(body, goalId, userId, function(err, goal){
expect(goal).to.be.undefined;
done();
});
});
});

});

0 comments on commit 0c5ad73

Please sign in to comment.