const db = require('../config/mysqlConfig') const moment = require('moment') let ArticleDao = { // 查找所有文章 selectAll: (start, num) => { let sql = `select * from article limit ${start}, ${num}` return db.query(sql) }, // 查找 selectLatest: (start, num) => { let sql = `select * from article order by update_time desc limit ${start}, ${num}` return db.query(sql) }, selectByID: ID => { let sql = `SELECT * FROM article WHERE article_id = ${ID}` return db.query(sql) }, selectByTitle: Title => { let sql = `SELECT * FROM article WHERE article_title = ${Title}` return db.query(sql) }, selectByUserID: UserID => { let sql = `SELECT * FROM article WHERE author_id = ${UserID}` return db.query(sql) }, update: Article => { let updateAt = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss') let sql = `UPDATE article SET update_time = '${updateAt}'` if(Article.content !== undefined && Article.content !== ''){ sql += `, content = '${Article.content}'` } if(Article.description !== undefined && Article.description !== ''){ sql += `, content = '${Article.description}'` } if(Article.title !== undefined && Article.title !== ''){ sql += `, article_title = '${Article.title}'` } sql += ` where article_id = ${Article.articleID}` db.query(sql) }, insert: Article => { let createdAt = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss') let sql = `insert into article(article_title, author_id, create_time, update_time, description, content) values('${Article.title}', '${Article.authorID}', '${createdAt}', '${createdAt}', '${Article.description}', '${Article.content}')` return db.query(sql) }, like: (num, articleID) => { let sql = `update article set like_count = like_count + ${num} where article_id = ${articleID}` db.query(sql) }, dislike: (num, articleID) => { let sql = `update article set dislike_count = dislike_count + ${num} where article_id = ${articleID}` db.query(sql) }, comment: articleID => { let sql = `update article set comment_count = comment_count + 1 where article_id = ${articleID}` db.query(sql) } } module.exports = ArticleDao