Skip to content

Commit

Permalink
Add Comments support.
Browse files Browse the repository at this point in the history
  • Loading branch information
loarabia committed Jul 23, 2011
1 parent 838ee05 commit 1c26559
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 70 deletions.
6 changes: 3 additions & 3 deletions articles/express-mongodb.markdown
Expand Up @@ -271,15 +271,15 @@ We'll also need a new route to allow the article to be referenced by a URL and w
We need to update the index page's view:

<express-mongodb/views/blogs_index-final.html.haml>
<express-mongodb/views/blogs_index-final.jade>

The page that shows a single blog entry:

<express-mongodb/views/blog_show-final.html.haml>
<express-mongodb/views/blog_show-final.jade>

The stylesheet that renders these pages:

<express-mongodb/views/style-final.css.sass>
<express-mongodb/views/style-final.styl>

We also need to add a new rule to `app.js` for serving these view requests:

Expand Down
135 changes: 68 additions & 67 deletions articles/express-mongodb/app-final.js
@@ -1,84 +1,85 @@
var kiwi= require('kiwi')
kiwi.require('express');
kiwi.seed('mongodb-native')

require('express/plugins')
var ArticleProvider= require('./articleprovider-mongodb').ArticleProvider;
/**
* Module dependencies.
*/

configure(function(){
use(MethodOverride);
use(ContentLength);
use(Logger);
set('root', __dirname);
})
var express = require('express');
var Articles = require('./ViewModel/Article').Article;

var articleProvider= new ArticleProvider('localhost', 27017);

//getRoot
get('/', function(){
var self = this;
articleProvider.findAll(function(error, docs){
self.render('blogs_index.html.haml', {
locals: {
title: 'Blog',
articles: docs
}
});
})
})
var app = module.exports = express.createServer();

//getCss
get('/*.css', function(file){
this.render(file + '.css.sass', { layout: false });
});
// Configuration

//getBlogs
get('/blog/*', function(id){
var self = this;
articleProvider.findById(id, function(error, article) {
self.render('blog_show.html.haml', {
locals: {
title: article.title,
article:article
}
});
});
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(require('stylus').middleware({ src: __dirname + '/public' }));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});

//addNewBlog
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

get('/blog/new', function(){
this.render('blog_new.html.haml', {
locals: {
title: 'New Post'
}
});
app.configure('production', function(){
app.use(express.errorHandler());
});

//addComment
var articles = new Articles('localhost', 27017);
// Routes

app.get('/', function(req, res){
articles.findAll( function(error,docs){
res.render('blog_index.jade', {
locals: {
title: 'Blog',
articles:docs
}
});
})
});

post('/blog/addComment', function() {
var self = this;
articleProvider.addCommentToArticle(this.param('_id'), {
person: self.param('person'),
comment: self.param('comment'),
created_at: new Date()
}, function(error, docs) {
self.redirect('/blog/' + self.param('_id'))
});
app.get('/blog/new', function(req, res) {
res.render('blog_new.jade', { locals: {
title: 'New Post'
}
});
});

//postNewBlog
app.post('/blog/addComment', function(req, res) {
articles.addCommentToArticle(req.param('_id'), {
person: req.param('person'),
comment: req.param('comment'),
created_at: new Date()
} , function( error, docs) {
res.redirect('/blog/' + req.param('_id'))
});
});

post('/blog/new', function(){
var self = this;
articleProvider.save({
title: this.param('title'),
body: this.param('body')
}, function(error, docs) {
self.redirect('/')
});
});
app.post('/blog/new', function(req, res){
articles.save({
title: req.param('title'),
body: req.param('body')
}, function( error, docs) {
res.redirect('/')
});
});

app.get('/blog/:id', function(req, res) {
articles.findById(req.params.id, function(error, article) {
res.render('blog_show.jade',
{ locals: {
title: article.title,
article:article
}
});
});
});


run();
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
8 changes: 8 additions & 0 deletions articles/express-mongodb/views/blogs_index-final.jade
@@ -0,0 +1,8 @@
h1= title
#articles
- each article in articles
div.article
div.created_at= article.created_at
div.title
a(href="/blog/"+article._id.toHexString())!= article.title
div.body= article.body
20 changes: 20 additions & 0 deletions articles/express-mongodb/views/blogs_show-final.jade
@@ -0,0 +1,20 @@
h1= title
div.article
div.created_at= article.created_at
div.title= article.title
div.body= article.body
- each comment in article.comments
div.comment
div.person= comment.person
div.comment= comment.comment
div
form( method="post", action="/blog/addComment")
input( type="hidden", name="_id", value=article._id.toHexString())
div
span Author :
input( type="text", name="person", id="addCommentPerson")
div
span Comment :
textarea( name="comment", rows=5, id="addCommentComment")
div#editArticleSubmit
input(type="submit", value="Send")
29 changes: 29 additions & 0 deletions articles/express-mongodb/views/style-final.styl
@@ -0,0 +1,29 @@
body
font-family "Helvetica Neue", "Lucida Grande", "Arial"
font-size 13px
text-align center
text-stroke 1px rgba(255, 255, 255, 0.1)
color #555
h1, h2
margin 0
font-size 22px
color #343434
h1
text-shadow 1px 2px 2px #ddd
font-size 60px
#articles
text-align left
margin-left auto
margin-right auto
width 320px
color #340000
.article
margin 20px
.created_at
display none
.title
font-weight bold
text-decoration underline
background-color #eee
.body
background-color #ffa

0 comments on commit 1c26559

Please sign in to comment.