Created
September 21, 2011 15:01
-
-
Save nrstott/1232272 to your computer and use it in GitHub Desktop.
Bogart CouchDB Blog code translated to use MongoDB
This file contains 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
var bogart = require('bogart'); | |
var Q = require('promised-io/lib/promise'); | |
var mongoose = require('mongoose'); | |
var PostSchema = new mongoose.Schema({ | |
title: String, | |
body: String, | |
comments: [ CommentSchema ] | |
}); | |
var CommentSchema = new mongoose.Schema({ | |
author: String, | |
body: String | |
}); | |
var Post = mongoose.model('PostSchema', PostSchema); | |
var app = bogart.router(function(get, post, put, destroy) { | |
var client = mongoose.connect('mongodb://localhost/test') | |
, viewEngine = bogart.viewEngine('mustache'); | |
get('/posts', function(req) { | |
return Q.execute(Post.find.bind(Post)).then(function(docs) { | |
return viewEngine.respond('posts.html', { | |
locals: { | |
posts: docs, | |
title: 'Blog Home' | |
} | |
}) | |
}); | |
}); | |
get('/posts/new', function(req) { | |
return viewEngine.respond('new-post.html', { | |
locals: { | |
title: 'New Post' | |
} | |
}); | |
}); | |
post('/posts', function(req) { | |
var post = new Post(req.params); | |
return Q.execute(post.save.bind(post)).then(function() { | |
return bogart.redirect('/posts'); | |
}); | |
}); | |
get('/posts/:id', function(req) { | |
console.log('getting post'); | |
return Q.execute(Post.findById.bind(Post), req.params.id).then(function(post) { | |
return viewEngine.respond('post.html', { locals: post }); | |
}); | |
}); | |
post('/posts/:id/comments', function(req) { | |
var comment = req.params; | |
return Q.execute(Post.findById.bind(Post), req.params.id).then(function(post) { | |
post.comments = post.comments || []; | |
post.comments.push(comment); | |
return Q.execute(post.save.bind(post)).then(function() { | |
return bogart.redirect('/posts/'+req.params.id); | |
}); | |
}); | |
}); | |
}); | |
// Add ParseForm middleware to automatically process the parameters | |
// of form submissions into a JSON object that is easily usable (req.body, req.params). | |
app = bogart.middleware.ParseForm(app); | |
// Start the JSGI server. | |
bogart.start(app); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment