私信发送成功
检测到您已登录开源中国,是否 一键登录码云
Watch Star Fork

oyhk / mkfree-blogNodeJS

添加修改博客功能
oyhk 提交于   master
commit 04dab0fb09f09f8652010ab53870663f8f2e1cd5
1 parent 4f2e097652
@@ -18,15 +18,38 @@ router.post('/save', function (req, res, next) {
res.redirect("/admin/posts/add");
return;
}
- let summary = req.body.contentMarkdown.substring(0,100);
- let params = [req.body.title, req.body.contentHtml,summary, req.body.contentMarkdown, 1];
+ let contentMarkdown = req.body.contentMarkdown;
+ let summary = contentMarkdown.substring(0, 100);
+ let params = [req.body.title, req.body.contentHtml, contentMarkdown,summary , 1];
co(function *() {
var result = yield blogPost.saveByParams(params);
res.redirect(`/posts/${result.insertId}`);
});
 
+});
+
+router.get('/edit/:id', (req, res, next)=> {
+ co(function *() {
+ var result = yield blogPost.findOne(req.params.id);
+ res.render('admin/edit', {blogPost: result[0]});
+ });
 
});
 
+router.post('/update', function (req, res, next) {
+
+ if (!req.body.id || !req.body.title || !req.body.contentHtml || !req.body.contentMarkdown) {
+ res.redirect("/");
+ return;
+ }
+ let contentMarkdown = req.body.contentMarkdown;
+ let summary = contentMarkdown.substring(0, 100);
+ let params = [req.body.title, req.body.contentHtml, contentMarkdown,summary , req.body.id];
+ co(function *() {
+ yield blogPost.updateByParams(params);
+ res.redirect(`/posts/${req.body.id}`);
+ });
+
+});
 
module.exports = router;
@@ -7,15 +7,20 @@ class BlogPost extends Base {
super.tableName = 'blog_post';
}
 
- findByOldId(oldId){
- let sql = "select * from blog_post bp where bp.old_id = ?";
+ findByOldId(oldId) {
+ let sql = "select * from blog_post bp where bp.old_id = ?";
let params = [oldId];
- return super.execute(sql,params);
+ return super.execute(sql, params);
}
 
- saveByParams(params){
+ saveByParams(params) {
let sql = "insert into blog_post(title,content_html,content_markdown,summary,user_id) values (?,?,?,?,?)";
- return super.execute(sql,params)
+ return super.execute(sql, params);
+ }
+
+ updateByParams(params) {
+ let sql = "update blog_post set title = ?,content_html=?,content_markdown=?,summary=? where id = ?";
+ return super.execute(sql, params);
}
 
}
@@ -0,0 +1,33 @@
+extends ../layout
+
+block title
+ title 博客发布
+block link
+ link(rel='stylesheet', href='/plugin/editormd/css/editormd.min.css')
+
+block content
+ div(class="container-fluid show")
+ div(class="row")
+ div(class="col-lg-12 col-md-12 col-sm-12")
+ form(class="form-horizontal" role="form" action="/admin/posts/update" method="post")
+ input(type="hidden" name="id" value="#{blogPost.id}")
+ input(type="hidden" name="contentHtml" class="content-html")
+ div(class="form-group")
+ label(for="inputEmail3" class="col-lg-1 col-md-1 col-sm-1 control-label") 标题
+ div(class="col-lg-11 col-md-11 col-sm-11")
+ input(type="text" name="title" class="form-control" id="inputEmail3" placeholder="请输入标题" value="#{blogPost.title}")
+ div(class="form-group")
+ label(class="col-lg-1 col-md-1 col-sm-1 control-label") 内容
+ div(class="col-lg-11 col-md-11 col-sm-11")
+ div(id="editormd" class="editormd")
+ textarea(class="form-control content" name="contentMarkdown" rows="3" placeholder="请输入内容") #{blogPost.content_markdown}
+ div(class="form-group")
+ div(class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-lg-11 col-md-11 col-sm-11")
+ button(type="button" class="btn btn-primary save") 提交
+
+
+block script
+ script(src="/plugin/editormd/editormd.min.js")
+ script(src="/javascripts/add.js")
+
+
@@ -1,15 +0,0 @@
-var React = require('react');
-var DefaultLayout = require('./layout');
-
-class Error extends React.Component {
- render() {
- return (
- <DefaultLayout title={this.props.message}>
- <div>{this.props.error.status}</div>
- <div>{this.props.error.stack}</div>
- </DefaultLayout>
- );
- }
-}
-
-module.exports = Error;
\ No newline at end of file
@@ -51,7 +51,7 @@ block content
h3(class="title") #{focusBlogPost.title}
p(class="summary") #{focusBlogPost.summary}
p
- a(class="btn btn-primary" href='/posts/#{focusBlogPost.old_id ? item.old_id : item.id}') 详情
+ a(class="btn btn-primary" href='/posts/#{focusBlogPost.old_id ? focusBlogPost.old_id : focusBlogPost.id}') 详情
div(class="container blog-list")
div(class="row")
each item in newBlogPostList
@@ -1,14 +0,0 @@
-var React = require('react');
-var DefaultLayout = require('./layout');
-
-class Index extends React.Component {
- render() {
- return (
- <DefaultLayout title={this.props.title}>
- <div>Hello {this.props.name}</div>
- </DefaultLayout>
- );
- }
-}
-
-module.exports = Index;
\ No newline at end of file
@@ -1,15 +0,0 @@
-var React = require('react');
-
-class DefaultLayout extends React.Component {
- render() {
- return (
- <html>
- <head><title>{this.props.title}</title></head>
- <body>{this.props.children}</body>
- <script type="text/javascript" src="/plugin/jQuery/jquery-2.2.3.min.js"></script>
- </html>
- );
- }
-}
-
-module.exports = DefaultLayout;
\ No newline at end of file

登录 后才可以发表评论