Skip to content

Commit d67afe7

Browse files
committedJun 26, 2012
added ability to delete posts
1 parent 38af10d commit d67afe7

File tree

6 files changed

+48
-2
lines changed

6 files changed

+48
-2
lines changed
 

‎app.js

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ app.get('/blog/:id', function(req, res) {
6969
});
7070
});
7171

72+
app.post('/blog/:id/delete', function(req, res) {
73+
articleProvider.delete(req.param('_id'), function(error, docs) {
74+
res.redirect('/')
75+
});
76+
});
77+
7278
app.post('/blog/addComment', function(req, res) {
7379
articleProvider.addCommentToArticle(req.param('_id'), {
7480
person: req.param('person'),

‎articleprovider-mongodb.js

+17
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ ArticleProvider.prototype.save = function(articles, callback) {
5050
articles = [articles];
5151
for( var i =0;i< articles.length;i++ ) {
5252
article = articles[i];
53+
text_to_replace = articles[i].body;
54+
var paragraphs = article.body.split('\r\n\r\n');
55+
article.body = paragraphs;
5356
article.created_at = new Date();
5457
if( article.comments === undefined ) article.comments = [];
5558
for(var j =0;j< article.comments.length; j++) {
@@ -64,6 +67,20 @@ ArticleProvider.prototype.save = function(articles, callback) {
6467
});
6568
};
6669

70+
ArticleProvider.prototype.delete = function(articleId, callback) {
71+
this.getCollection(function(error, article_collection) {
72+
if(error) callback(error);
73+
else {
74+
article_collection.remove(
75+
{_id: article_collection.db.bson_serializer.ObjectID.createFromHexString(articleId)},
76+
function(error, article){
77+
if(error) callback(error);
78+
else callback(null, article)
79+
});
80+
}
81+
});
82+
};
83+
6784
ArticleProvider.prototype.addCommentToArticle = function(articleId, comment, callback) {
6885
this.getCollection(function(error, article_collection) {
6986
if( error ) callback( error );

‎public/stylesheets/style.css

+9
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ a {
3737
padding-left: 20px;
3838
background-color: #d2eced;
3939
}
40+
#articles .article .title input#delete {
41+
float: right;
42+
margin-top: 10px;
43+
margin-right: 10px;
44+
color: #d55b5b;
45+
border: none;
46+
background: transparent;
47+
cursor: pointer;
48+
}
4049
#articles .article .title a {
4150
line-height: 40px;
4251
}

‎public/stylesheets/style.styl

+8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ a
2525
.created_at
2626
display none
2727
.title
28+
input#delete
29+
float right
30+
margin-top 10px
31+
margin-right 10px
32+
color #D55B5B
33+
border none
34+
background transparent
35+
cursor pointer
2836
a
2937
line-height 40px
3038
font-weight bold

‎views/index.jade

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ h1= title
33
- each article in articles
44
div.article
55
div.created_at= article.created_at
6-
div.title
6+
div.title
7+
form( method="post", action="/blog/:id/delete")
8+
input(name="_id", type="hidden", value=article._id.toHexString())
9+
input(id="delete", value="x", type="submit")
710
a(href="/blog/"+article._id)!= article.title
8-
div.body= article.body
11+
div.body
12+
- each paragraph in article.body
13+
p= paragraph
914
#newpost
1015
a(href="/blog/new")!= 'new post'

‎views/layout.jade

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ html
33
head
44
title= title
55
link(rel='stylesheet', href='/stylesheets/style.css')
6+
script(src='http://code.jquery.com/jquery-1.7.2.min.js')
67
body!= body

0 commit comments

Comments
 (0)