Skip to content

Commit 2317ebf

Browse files
committed
added ability to edit files
1 parent d67afe7 commit 2317ebf

9 files changed

+115
-16
lines changed

app.js

+26-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var articleProvider = new ArticleProvider('localhost', 27017);
3333

3434
app.get('/', function(req, res){
3535
articleProvider.findAll( function(error,docs){
36-
res.render('index.jade', {
36+
res.render('index', {
3737
locals: {
3838
title: 'a simple blog',
3939
articles:docs
@@ -43,7 +43,7 @@ app.get('/', function(req, res){
4343
});
4444

4545
app.get('/blog/new', function(req, res) {
46-
res.render('blog_new.jade', { locals: {
46+
res.render('blog_new', { locals: {
4747
title: 'new post'
4848
}
4949
});
@@ -60,7 +60,7 @@ app.post('/blog/new', function(req, res){
6060

6161
app.get('/blog/:id', function(req, res) {
6262
articleProvider.findById(req.params.id, function(error, article) {
63-
res.render('blog_show.jade',
63+
res.render('blog_show',
6464
{ locals: {
6565
title: article.title,
6666
article: article
@@ -69,6 +69,29 @@ app.get('/blog/:id', function(req, res) {
6969
});
7070
});
7171

72+
// edit a blog post
73+
app.get('/blog/:id/edit', function(req, res) {
74+
articleProvider.findById(req.param('_id'), function(error, article) {
75+
res.render('blog_edit',
76+
{ locals: {
77+
title: article.title,
78+
article: article
79+
}
80+
});
81+
});
82+
});
83+
84+
// update/save an edited blog post
85+
app.post('/blog/:id/save', function(req, res) {
86+
articleProvider.update(req.param('_id'),{
87+
title: req.param('title'),
88+
body: req.param('body')
89+
}, function(error, docs) {
90+
res.redirect('/')
91+
});
92+
});
93+
94+
// delete a blog post
7295
app.post('/blog/:id/delete', function(req, res) {
7396
articleProvider.delete(req.param('_id'), function(error, docs) {
7497
res.redirect('/')

articleprovider-mongodb.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ ArticleProvider.prototype.getCollection= function(callback) {
1717
});
1818
};
1919

20+
// find all blog posts in database
2021
ArticleProvider.prototype.findAll = function(callback) {
2122
this.getCollection(function(error, article_collection) {
2223
if( error ) callback(error)
@@ -29,7 +30,7 @@ ArticleProvider.prototype.findAll = function(callback) {
2930
});
3031
};
3132

32-
33+
// find a blog post in the database by its object ID
3334
ArticleProvider.prototype.findById = function(id, callback) {
3435
this.getCollection(function(error, article_collection) {
3536
if( error ) callback(error)
@@ -42,6 +43,7 @@ ArticleProvider.prototype.findById = function(id, callback) {
4243
});
4344
};
4445

46+
// save a (new) blog post
4547
ArticleProvider.prototype.save = function(articles, callback) {
4648
this.getCollection(function(error, article_collection) {
4749
if( error ) callback(error)
@@ -67,6 +69,31 @@ ArticleProvider.prototype.save = function(articles, callback) {
6769
});
6870
};
6971

72+
// update a blog post
73+
ArticleProvider.prototype.update = function(articleId, articles, callback) {
74+
this.getCollection(function(error, article_collection) {
75+
if( error ) callback(error);
76+
else {
77+
for( var i =0;i< articles.length;i++ ) {
78+
article = articles[i];
79+
text_to_replace = articles[i].body;
80+
var paragraphs = article.body.split('\r\n\r\n');
81+
article.body = paragraphs;
82+
}
83+
84+
article_collection.update(
85+
{_id: article_collection.db.bson_serializer.ObjectID.createFromHexString(articleId)},
86+
articles,
87+
function(error, articles) {
88+
if(error) callback(error);
89+
else callback(null, articles)
90+
});
91+
}
92+
});
93+
};
94+
///////////////////////////////////////////////////////////////////////////////////////////////
95+
96+
// remove a blog post from the database
7097
ArticleProvider.prototype.delete = function(articleId, callback) {
7198
this.getCollection(function(error, article_collection) {
7299
if(error) callback(error);

public/javascripts/jquery-1.7.2.min.js

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/stylesheets/style.css

+22-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,18 @@ a {
3737
padding-left: 20px;
3838
background-color: #d2eced;
3939
}
40+
#articles .article .title input#edit {
41+
border: none;
42+
background: transparent;
43+
float: right;
44+
margin-top: 14px;
45+
font-size: 10px;
46+
color: #555;
47+
cursor: pointer;
48+
}
4049
#articles .article .title input#delete {
4150
float: right;
42-
margin-top: 10px;
51+
margin-top: 13px;
4352
margin-right: 10px;
4453
color: #d55b5b;
4554
border: none;
@@ -53,21 +62,31 @@ a {
5362
background-color: #fff3bf;
5463
padding: 20px;
5564
}
65+
#content {
66+
margin-top: 30px;
67+
}
5668
#newpost {
5769
width: 320px;
5870
margin: 20px auto;
5971
}
6072
input#editArticleTitle {
6173
background-color: #d2eced;
6274
margin-bottom: 10px;
75+
margin-top: 5px;
6376
}
64-
textarea#editArticleBody {
77+
#editArticleBody {
78+
margin-top: 5px;
6579
background-color: #fff3bf;
80+
height: 300px;
6681
}
6782
input#editArticleTitle,
68-
textarea#editArticleBody {
83+
#editArticleBody {
6984
border: 1px solid #f1f1f1;
7085
min-width: 320px;
86+
text-align: left;
87+
}
88+
#edit {
89+
cursor: pointer;
7190
}
7291
.article .created_at {
7392
display: none;

public/stylesheets/style.styl

+19-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,17 @@ a
2525
.created_at
2626
display none
2727
.title
28+
input#edit
29+
border none
30+
background transparent
31+
float right
32+
margin-top 14px
33+
font-size 10px
34+
color #555
35+
cursor pointer
2836
input#delete
2937
float right
30-
margin-top 10px
38+
margin-top 13px
3139
margin-right 10px
3240
color #D55B5B
3341
border none
@@ -41,18 +49,26 @@ a
4149
.body
4250
background-color #fff3bf
4351
padding 20px
52+
#content
53+
margin-top 30px
4454
#newpost
4555
width 320px
4656
margin 20px auto
4757
input#editArticleTitle
4858
background-color #D2ECED
4959
margin-bottom 10px
50-
textarea#editArticleBody
60+
margin-top 5px
61+
#editArticleBody
62+
margin-top 5px
5163
background-color #FFF3BF
64+
height 300px
5265
input#editArticleTitle
53-
textarea#editArticleBody
66+
#editArticleBody
5467
border 1px solid #f1f1f1
5568
min-width 320px
69+
text-align left
70+
#edit
71+
cursor pointer
5672
.article
5773
.created_at
5874
display none

views/blog_edit.jade

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
h1= title
2+
form( method="post", action="/blog/:id/save")
3+
div#content
4+
span title
5+
input(type="text", name="title", id="editArticleTitle", value=[title])
6+
span body
7+
textarea(name="body", id="editArticleBody")= article.body
8+
input(type="hidden", name="_id", value=article._id.toHexString())
9+
input(type="submit", value="Save")

views/blog_new.jade

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
h1= title
22
form( method="post")
3-
div
4-
div
3+
div#content
54
span title
65
input(type="text", name="title", id="editArticleTitle")
76
div

views/index.jade

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ h1= title
77
form( method="post", action="/blog/:id/delete")
88
input(name="_id", type="hidden", value=article._id.toHexString())
99
input(id="delete", value="x", type="submit")
10+
form( method="get", action="/blog/:id/edit")
11+
input(name ="_id", type="hidden", value=article._id.toHexString())
12+
input(id="edit", value="edit", type="submit")
1013
a(href="/blog/"+article._id)!= article.title
11-
div.body
12-
- each paragraph in article.body
13-
p= paragraph
14+
div.body= article.body
1415
#newpost
1516
a(href="/blog/new")!= 'new post'

views/layout.jade

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
!!!
1+
!!! 5
22
html
33
head
44
title= title
55
link(rel='stylesheet', href='/stylesheets/style.css')
66
script(src='http://code.jquery.com/jquery-1.7.2.min.js')
7+
script(src='/javascripts/jquery-1.7.2.min.js')
78
body!= body

0 commit comments

Comments
 (0)