导语:
暑假在家闲着无事,就琢磨着做一个web博客练练手,现在已经做完了,把过程分享出来给大家看看,分享一下学习经验。这是第九篇,开始编写后台接口,为前端接口提供提供基础,主要讲后台sql语句和数据库的结构。
微信搜索 【web小馆】,回复 ”全栈博客项目“,即可获取 项目源码和后续的实战文章教程。
const newarticle = async (articleData = {}) => {
// articleData 是一个文章对象,包含 title content lei 属性
const title = xss(articleData.title)
// console.log('title is', title)
const content = xss(articleData.content)
const lei = articleData.type
const createTime = Date.now()
const pic = articleData.pic
const sort = articleData.sort
const show = articleData.state
let sql = `
insert into article (title, content, createtime, lei, pic, sort, showdata)
values ('${title}', '${content}', ${createTime}, '${lei}', '${pic}', '${sort}', '${show}');
`
let insertData = await exec(sql)
sql = `
insert into comments (content, articleid)values('[]', ${insertData.insertId});
`
insertData.comments = await exec(sql)
return {
id: insertData.insertId
}
}
const updatearticle = async (articleData = {}) => {
// id 就是要更新博客的 id
// articleData 是一个博客对象,包含 title content 属性
const id = xss(articleData.id)
const title = xss(articleData.title)
const content = articleData.content
const lei = xss(articleData.type)
const pic = xss(articleData.pic)
const sort = xss(articleData.sort)
const show = xss(articleData.state)
const sql = `
update article set title='${title}', content='${content}', lei='${lei}', pic='${pic}', sort='${sort}', showdata='${show}' where id=${id}
`
console.log(sql)
const updateData = await exec(sql)
if (updateData.affectedRows > 0) {
return true
}
return false
}
const getList = async (lei, keyword, num) => {
let mixnum = num - 10
let sql = `select id, title, createtime, lei, pic, sort, showdata, hits, likes from article where 1=1 `
if (lei) {
sql += `and lei='${lei}' `
}
if (keyword) {
sql += `and title like '%${keyword}%' `
}
sql += `order by createtime desc `
if (num) {
sql += `limit ${mixnum}, ${num};`
}
return await exec(sql)
}
const getDetail = async (id) => {
const sql = `select * from article where id='${id}'`
const rows = await exec(sql)
return rows[0]
}
const getComment = async (id) => {
const sql = `select * from comments where articleid='${id}'`
const rows = await exec(sql)
return rows[0]
}
const postComment = async (comments, id) => {
const sql = `update comments set content='${comments}' where id=${id};`
// console.log('sql',sql)
const rows = await exec(sql)
// console.log(rows)
return rows
}
const addHit = async (id, num) => {
const sql = `update article set hits='${Number(num) + 1}' where id=${id};`
// console.log('sql',sql)
const rows = await exec(sql)
// console.log(rows)
return rows
}
const addGood = async (id, num) => {
const sql = `update article set goods='${Number(num) + 1}' where id=${id};`
console.log('sql',sql)
const rows = await exec(sql)
// console.log(rows)
return rows
}
const subGood = async (id, num) => {
const sql = `update article set goods='${num - 1}' where id=${id};`
// console.log('sql',sql)
const rows = await exec(sql)
// console.log(rows)
return rows
}
const addLike = async (id, num) => {
const sql = `update article set likes='${Number(num) + 1}' where id=${id};`
console.log('sql',sql)
const rows = await exec(sql)
// console.log(rows)
return rows
}
const subLike = async (id, num) => {
const sql = `update article set likes='${num - 1}' where id=${id};`
console.log('sql',sql)
const rows = await exec(sql)
// console.log(rows)
return rows
}
const getBanerslist = async () => {
const sql = `select * from baners;`
const rows = await exec(sql)
return rows
}
你们的赞就是对我最大的鼓励。谢谢~
微信搜索【web小馆】,回复全栈博客项目,即可获取项目源码和后续的实战文章教程。每天用最简单朴实的语言,潜移默化的提升你的计算机基础知识和前端技术。小米粥,一个专注的web全栈工程师,我们下期再见!