pro-blog/modules/article.js

47 lines
1.8 KiB
JavaScript

const db = require('./db'); //
// 文章模块
const router = require('express').Router();
// 用户网站模块
// 1.查询文章列表(分页|搜索)
router.get("/web/list", async function (req, res) {
const page = parseInt(req.query['page'] || 1); // 获取页码
const size = parseInt(req.query['size'] || 5); // 每天条数
// 默认每页5条
const start = (page - 1) * size;
const search = '%' + (req.query['search'] || '') + '%'; // 搜索关键字
try {
const listResult = await db.query(
`select id,title,category,publish_time,cover,description,view_count,comment_count
from article where (title like ? or content like ? ) and status = 1 limit ?,?`,
[search, search, start, size]); // 执行查询语句并等待结果出来后赋值给变量
const totalCount = await db.query(
`select count(*) as total from article where (title like ? or content like ?) and status = 1`, [search, search]); // 执行获取总条数
res.send({ total: totalCount[0]['total'], list: listResult, size: size });
} catch (e) {
console.log(e)
res.send({ count: 0, list: [] });
}
});
// 2.查询单个文章详情
router.all('/web/detail',async (req,res)=>{
let id = req.query['id'];
if(!id || id <1){
res.send([])
}else{
const article = await db.query('select * from article where id =? and status = 1',[id]);
res.send(article)
}
});
// 3.更新阅读数量
router.all('/web/update',async (req,res)=>{
let id = req.query['id'];
if(!id || id <1){
res.send({status:false,message:"参数不对"})
}else{
const article = await db.query('update article set view_count=view_count + 1 where id =?',[id]);
res.send({status:true})
}
});
// 管理后台模块
module.exports = router