Authored by 李奇

文章模块添加

... ... @@ -3,24 +3,32 @@
* @author: leo <qi.li@yoho.cn>
* @date: 23/06/2017
*/
const mysqlCli = global.yoho.utils.mysqlCli;
const TABLE_ACT_ARTICLE = 'act_article';
const Article = require('../models/article');
function all(req, res) {
mysqlCli.changeDatabase(mysqlCli.database).then(() => {
mysqlCli.query(`select * from ${TABLE_ACT_ARTICLE}`)
.then(result => {
res.json({
code: 200,
data: {
total: result.length
}
});
Article.allArticles()
.then((result) => {
res.json({
code: 200,
data: result,
message: '获取文章列表成功'
});
});
}
function add(req, res) {
const params = req.body;
Article.createArticle(params)
.then((result) => {
res.json({
code: 200,
data: result
});
});
});
}
module.exports = {
all
all,
add
};
... ...
/**
* 文章model
* @author: leo <qi.li@yoho.cn>
* @date: 28/06/2017
*/
const mysqlCli = global.yoho.utils.mysqlCli;
const TABLE_ACT_ARTICLE = 'act_article';
class Article {
static allArticles() {
return mysqlCli.query(`select * from ${TABLE_ACT_ARTICLE}`);
}
static createArticle({userName, content}) {
return mysqlCli.insert(
`insert into ${TABLE_ACT_ARTICLE} (user_name, content) values ('${userName}', '${content}');`
);
}
}
module.exports = Article;
... ...
... ... @@ -8,5 +8,6 @@ const router = express.Router(); // eslint-disable-line
const article = require('./controllers/article');
router.get('/all', article.all);
router.post('/add', article.add);
module.exports = router;
... ...
/**
* API路由分发
* @author: Leo<qi.li@yoho.cn>
* @author: Leo <qi.li@yoho.cn>
* @date: 2017/6/23
*/
const article = require('./apps/article');
... ...