article.js
2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* 文章model
* @author: leo <qi.li@yoho.cn>
* @date: 28/06/2017
*/
const _ = require('lodash');
const mysqlCli = global.yoho.utils.mysqlCli;
const TABLE_ACT_ARTICLE = 'act_article';
const TABLE_ACT_ARTICLE_IMG = 'act_article_img';
// const TABLE_ACT_ARTICLE_GOOD = 'act_article_good';
class ArticleModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* 获取文章列表
* @returns {*}
*/
articleList({pageNo, pageSize, isPopular, orderBy, order}) {
const orderMapping = {
goodCount: 'good_count',
createTime: 'create_time'
};
let limitSql;
let orderSql;
let whereSql = '';
orderSql = `ORDER BY AA.${orderMapping[orderBy]} ${order}`;
if (isPopular) {
whereSql = 'WHERE AA.good_count > 10';
}
limitSql = `LIMIT ${(pageNo - 1) * pageSize}, ${pageSize}`;
return mysqlCli.query(
`SELECT AA.id, AA.good_count goodCount,
AAI.img_url imgUrl, AA.content, DATE_FORMAT(AA.create_time, '%Y-%m-%d %H:%i:%S') createTime
FROM ${TABLE_ACT_ARTICLE} AS AA
INNER JOIN ${TABLE_ACT_ARTICLE_IMG} AS AAI
ON AA.id = AAI.article_id
${whereSql} ${orderSql} ${limitSql};`
);
}
/**
* 发表文章
* @param actId 活动ID
* @param content 文章内容
* @param imgUrl 图片链接
* @returns {*}
*/
createArticle({actId, content, imgUrl}) {
const session = this.ctx.req.session;
const userId = _.get(session, 'user.id');
return mysqlCli.insert(
`insert into ${TABLE_ACT_ARTICLE} (act_id, user_id, content) values (:actId, :userId, :content);`,
{
actId,
userId,
imgUrl,
content
}
);
}
/**
* 文章图片插入
* @param articleId
* @param imgUrl
* @returns {*}
*/
insertArticleImg(articleId, imgUrl) {
return mysqlCli.insert(
`insert into ${TABLE_ACT_ARTICLE_IMG} (article_id, img_url) values (:articleId, :imgUrl);`,
{
imgUrl,
articleId
}
);
}
/**
* 获取单个文章详情
* @param articleId
* @returns {*}
*/
getSingleArticle(articleId, actId) {
return mysqlCli.query(`SELECT AA.id AS id,AA.content AS content,AA.good_count AS good_count,
DATE_FORMAT(AA.create_time, '%Y-%m-%d %H:%i:%S') AS create_time,
AAI.img_url
FROM ${TABLE_ACT_ARTICLE} AS AA
INNER JOIN ${TABLE_ACT_ARTICLE_IMG} AS AAI
WHERE AAI.article_id = AA.id
AND AA.act_id = :actId
AND AA.id = :articleId;`,
{
actId,
articleId
}
);
}
}
module.exports = ArticleModel;