Authored by 李奇

article model 修改

... ... @@ -3,27 +3,31 @@
* @author: leo <qi.li@yoho.cn>
* @date: 23/06/2017
*/
const Article = require('../models/article');
const ArticleModel = require('../models/article');
const GET_ARTICLES_SUCCESS = '获取文章列表成功';
const ADD_ARTICLE_SUCCESS = '文章发表成功';
function all(req, res) {
Article.allArticles()
.then((result) => {
req.ctx(ArticleModel).allArticles()
.then(result => {
res.json({
code: 200,
data: result,
message: '获取文章列表成功'
message: GET_ARTICLES_SUCCESS
});
});
}
function add(req, res) {
const params = req.body;
Article.createArticle(params)
.then((result) => {
req.ctx(ArticleModel).createArticle(params)
.then(() => {
res.json({
code: 200,
data: result
message: ADD_ARTICLE_SUCCESS
});
});
}
... ...
... ... @@ -8,16 +8,33 @@ const mysqlCli = global.yoho.utils.mysqlCli;
const TABLE_ACT_ARTICLE = 'act_article';
class Article {
static allArticles() {
class ArticleModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* 获取所有文章
* @returns {*}
*/
allArticles() {
return mysqlCli.query(`select * from ${TABLE_ACT_ARTICLE}`);
}
static createArticle({userName, content}) {
/**
* 发表文章
* @param userName
* @param content
* @returns {*}
*/
createArticle({userName, content}) {
return mysqlCli.insert(
`insert into ${TABLE_ACT_ARTICLE} (user_name, content) values ('${userName}', '${content}');`
);
}
}
module.exports = Article;
module.exports = ArticleModel;
... ...