Showing
2 changed files
with
32 additions
and
11 deletions
@@ -3,27 +3,31 @@ | @@ -3,27 +3,31 @@ | ||
3 | * @author: leo <qi.li@yoho.cn> | 3 | * @author: leo <qi.li@yoho.cn> |
4 | * @date: 23/06/2017 | 4 | * @date: 23/06/2017 |
5 | */ | 5 | */ |
6 | -const Article = require('../models/article'); | 6 | +const ArticleModel = require('../models/article'); |
7 | + | ||
8 | +const GET_ARTICLES_SUCCESS = '获取文章列表成功'; | ||
9 | +const ADD_ARTICLE_SUCCESS = '文章发表成功'; | ||
7 | 10 | ||
8 | function all(req, res) { | 11 | function all(req, res) { |
9 | - Article.allArticles() | ||
10 | - .then((result) => { | 12 | + req.ctx(ArticleModel).allArticles() |
13 | + .then(result => { | ||
11 | res.json({ | 14 | res.json({ |
12 | code: 200, | 15 | code: 200, |
13 | data: result, | 16 | data: result, |
14 | - message: '获取文章列表成功' | 17 | + message: GET_ARTICLES_SUCCESS |
15 | }); | 18 | }); |
16 | }); | 19 | }); |
20 | + | ||
17 | } | 21 | } |
18 | 22 | ||
19 | function add(req, res) { | 23 | function add(req, res) { |
20 | const params = req.body; | 24 | const params = req.body; |
21 | 25 | ||
22 | - Article.createArticle(params) | ||
23 | - .then((result) => { | 26 | + req.ctx(ArticleModel).createArticle(params) |
27 | + .then(() => { | ||
24 | res.json({ | 28 | res.json({ |
25 | code: 200, | 29 | code: 200, |
26 | - data: result | 30 | + message: ADD_ARTICLE_SUCCESS |
27 | }); | 31 | }); |
28 | }); | 32 | }); |
29 | } | 33 | } |
@@ -8,16 +8,33 @@ const mysqlCli = global.yoho.utils.mysqlCli; | @@ -8,16 +8,33 @@ const mysqlCli = global.yoho.utils.mysqlCli; | ||
8 | 8 | ||
9 | const TABLE_ACT_ARTICLE = 'act_article'; | 9 | const TABLE_ACT_ARTICLE = 'act_article'; |
10 | 10 | ||
11 | -class Article { | ||
12 | - static allArticles() { | 11 | +class ArticleModel extends global.yoho.BaseModel { |
12 | + constructor(ctx) { | ||
13 | + super(ctx); | ||
14 | + } | ||
15 | + | ||
16 | + /** | ||
17 | + * 获取所有文章 | ||
18 | + * @returns {*} | ||
19 | + */ | ||
20 | + allArticles() { | ||
13 | return mysqlCli.query(`select * from ${TABLE_ACT_ARTICLE}`); | 21 | return mysqlCli.query(`select * from ${TABLE_ACT_ARTICLE}`); |
14 | } | 22 | } |
15 | 23 | ||
16 | - static createArticle({userName, content}) { | 24 | + /** |
25 | + * 发表文章 | ||
26 | + * @param userName | ||
27 | + * @param content | ||
28 | + * @returns {*} | ||
29 | + */ | ||
30 | + createArticle({userName, content}) { | ||
17 | return mysqlCli.insert( | 31 | return mysqlCli.insert( |
18 | `insert into ${TABLE_ACT_ARTICLE} (user_name, content) values ('${userName}', '${content}');` | 32 | `insert into ${TABLE_ACT_ARTICLE} (user_name, content) values ('${userName}', '${content}');` |
19 | ); | 33 | ); |
20 | } | 34 | } |
21 | } | 35 | } |
22 | 36 | ||
23 | -module.exports = Article; | 37 | +module.exports = ArticleModel; |
38 | + | ||
39 | + | ||
40 | + |
-
Please register or login to post a comment