Created
June 14, 2011 15:02
-
-
Save fwielstra/1025070 to your computer and use it in GitHub Desktop.
Our controller refactored
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(Thread, Post) { | |
return { | |
post: function(req, res) { | |
new Thread({title: req.body.title, author: req.body.author}).save(); | |
}, | |
list: function(req, res) { | |
Thread.find(function(err, threads) { | |
res.send(threads); | |
}); | |
}, | |
show: function(req, res) { | |
Thread.findOne({title: req.params.title}, function(error, thread) { | |
var posts = Post.find({thread: thread._id}, function(error, posts) { | |
res.send([{thread: thread, posts: posts}]); | |
}); | |
}); | |
} | |
}; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Partial rewrite of app.js. You can see we moved our Thread and Post model initialization code to our app.js. | |
var Thread = require('./models/thread.js'); | |
var Post = require('./models/post.js'); | |
var api = require('./controllers/api.js')(Thread, Post); | |
app.post('/thread', api.post); | |
app.get('/thread/:title.:format?', api.show); | |
app.get('/thread', api.list); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment