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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| router.get('/search', async(req, res) => {
let {keyword,categoryId,page,pageSize} = req.query
page = page == null ? 1:page; pageSize = pageSize == null ? 10:pageSize; categoryId = categoryId == null ? 0:categoryId; keyword = keyword == null ? "":keyword;
let params = [] let whereSqls = [] if (categoryId != 0){ whereSqls.push(" `category_id` = ? ") params.push(categoryId) } if(keyword != ""){ whereSqls.push(" (`title` LIKE ? OR `content` LIKE ?) ") params.push("%" + keyword + "%") params.push("%" + keyword + "%") } let whereSqlStr = "" if(whereSqls.length > 0){ whereSqlStr = " WHERE " + whereSqls.join(" AND ") } let searchSql = " SELECT * FROM `blog` " + whereSqlStr + " ORDER BY `create_time` DESC LIMIT ?,? " let searchSqlParams = params.concat([(page-1)*pageSize,pageSize])
let searchCountSql = " SELECT COUNT(*) AS count FROM `blog` " + whereSqlStr let searchCountParams = params
let searchResult = await db.async.all(searchSql,searchSqlParams) let countResult = await db.async.all(searchCountSql,searchCountParams)
console.log(searchSql,countResult);
if(searchResult.err == null && countResult.err == null){ res.send({ code: 200, msg:"查询成功", data:{ keyword, categoryId, page, pageSize, rows:searchResult.rows, count:countResult.rows[0].count, } }) } else{ res.send({ code:500, msg:"查询失败" }) } })
router.post('/add', async(req, res) => { let {title,categoryId,content} = req.body; let id = genid.NextId(); let create_time = new Date().getTime();
const insert_sql = "INSERT INTO `blog`(`id`,`title`,`category_id`,`content`,`create_time`) VALUES (?,?,?,?,?)" let params = [id,title,categoryId,content,create_time];
let {err,rows} = await db.async.run(insert_sql,params);
if(err == null) { res.send({ code:200, msg:"添加成功", }) }else { res.send({ code:500, msg:"添加失败", err }) } })
router.put('/update', async(req, res) => { let {id,title,categoryId,content} = req.body; let create_time = new Date().getTime();
const update_sql = "UPDATE `blog` SET `title` = ?,`content` = ?,`category_id` = ? WHERE `id` = ?"; let params = [title,content,categoryId,id];
let {err,rows} = await db.async.run(update_sql,params);
if(err == null) { res.send({ code:200, msg:"修改成功", }) }else { res.send({ code:500, msg:"修改失败", err, }) } })
router.delete('/delete', async (req, res) => { let id = req.query.id; const delete_sql = "DELETE FROM `blog` WHERE `id` = ?"; let {err,rows} = await db.async.run(delete_sql,[id]);
if(err == null) { res.send({ code:200, msg:"删除成功", }) }else { res.send({ code:500, msg:"删除失败" }) } })
|