Authored by 李奇

文章模块添加

@@ -3,24 +3,32 @@ @@ -3,24 +3,32 @@
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 mysqlCli = global.yoho.utils.mysqlCli;  
7 -  
8 -const TABLE_ACT_ARTICLE = 'act_article'; 6 +const Article = require('../models/article');
9 7
10 function all(req, res) { 8 function all(req, res) {
11 - mysqlCli.changeDatabase(mysqlCli.database).then(() => {  
12 - mysqlCli.query(`select * from ${TABLE_ACT_ARTICLE}`)  
13 - .then(result => {  
14 - res.json({  
15 - code: 200,  
16 - data: {  
17 - total: result.length  
18 - }  
19 - }); 9 + Article.allArticles()
  10 + .then((result) => {
  11 + res.json({
  12 + code: 200,
  13 + data: result,
  14 + message: '获取文章列表成功'
  15 + });
  16 + });
  17 +}
  18 +
  19 +function add(req, res) {
  20 + const params = req.body;
  21 +
  22 + Article.createArticle(params)
  23 + .then((result) => {
  24 + res.json({
  25 + code: 200,
  26 + data: result
20 }); 27 });
21 - }); 28 + });
22 } 29 }
23 30
24 module.exports = { 31 module.exports = {
25 - all 32 + all,
  33 + add
26 }; 34 };
  1 +/**
  2 + * 文章model
  3 + * @author: leo <qi.li@yoho.cn>
  4 + * @date: 28/06/2017
  5 + */
  6 +
  7 +const mysqlCli = global.yoho.utils.mysqlCli;
  8 +
  9 +const TABLE_ACT_ARTICLE = 'act_article';
  10 +
  11 +class Article {
  12 + static allArticles() {
  13 + return mysqlCli.query(`select * from ${TABLE_ACT_ARTICLE}`);
  14 + }
  15 +
  16 + static createArticle({userName, content}) {
  17 + return mysqlCli.insert(
  18 + `insert into ${TABLE_ACT_ARTICLE} (user_name, content) values ('${userName}', '${content}');`
  19 + );
  20 + }
  21 +}
  22 +
  23 +module.exports = Article;
@@ -8,5 +8,6 @@ const router = express.Router(); // eslint-disable-line @@ -8,5 +8,6 @@ const router = express.Router(); // eslint-disable-line
8 const article = require('./controllers/article'); 8 const article = require('./controllers/article');
9 9
10 router.get('/all', article.all); 10 router.get('/all', article.all);
  11 +router.post('/add', article.add);
11 12
12 module.exports = router; 13 module.exports = router;
1 /** 1 /**
2 * API路由分发 2 * API路由分发
3 - * @author: Leo<qi.li@yoho.cn> 3 + * @author: Leo <qi.li@yoho.cn>
4 * @date: 2017/6/23 4 * @date: 2017/6/23
5 */ 5 */
6 const article = require('./apps/article'); 6 const article = require('./apps/article');