article.js 2.06 KB
/**
 * 文章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() {
        return mysqlCli.query(`select * from ${TABLE_ACT_ARTICLE}`);
    }

    /**
     * 发表文章
     * @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 act_article.id,content,good_count,
            act_article_img.img_url from ${TABLE_ACT_ARTICLE} 
            join ${TABLE_ACT_ARTICLE_IMG} 
            where act_article_img.article_id = act_article.id 
            and act_article.act_id = :actId 
            and act_article.id = :articleId;`,
            {
                actId,
                articleId
            }
        );
    }
}

module.exports = ArticleModel;