blog-server/controllers/article.js
2019-12-03 14:50:37 +08:00

56 lines
2.6 KiB
JavaScript

// title 文章标题 contern 文章内容
// 获取文章列表
var articlefind = async (ctx, next) => {
let articlelist;
await dbs.find("articlelists").then((res) => {
articlelist = res.data
ctx.body=articlelist
})
next()
}
// 添加文章
var articleadd = async (ctx, next) => {
let month=parseInt(new Date(Date.now()).getMonth())+1
// await dbs.add("articletag",{articleid:ctx.query.articleid,tagid:ctx.query.tagid})
// await dbs.add("articlelists", { articletitle:ctx.request.body.title, date: new Date(Date.now()).getFullYear ()+ '/' + month+ '/' + new Date(Date.now()).getDate() + ' ' + new Date(Date.now()).getHours() + ':' + new Date(Date.now()).getMinutes() + ':' + new Date(Date.now()).getSeconds(),content:ctx.request.body.content,uid:1})
await dbs.find("idadd").then((res)=>{
console.log(res.data[res.data.length-1].num_key)
let tags=ctx.request.body.tags.split(",")
console.log(ctx.request.body.tags.split(","))
for(let i in ctx.request.body.tags.split(",")){
// console.log(ctx.request.body.tags.split(",")[i])
await dbs.add("articletag",{articleid:res.data[res.data.length-1].num_key,tagid:ctx.request.body.tags.split(",")[i]})
console.log("")
}
// await dbs.add("articletag",{articleid:res.data[res.data.length-1].num_key,tagid:ctx.query.tagid})
})
console.log(ctx.request.body)
ctx.body = "添加成功"
next()
}
// 删除文章
var articledel=async(ctx,next)=>{
await dbs.remove("articlelists",{num_key:parseInt(ctx.query.key)}).then((res)=>{
console.log(res)
console.log(ctx.query)
})
ctx.body="删除成功"
next()
}
// 修改文章
var articleupdate=async (ctx,next)=>{
let month=parseInt(new Date(Date.now()).getMonth())+1
await dbs.update("articlelists",{num_key:parseInt(ctx.query.key)},{articletitle:ctx.query.title, udate: new Date(Date.now()).getFullYear ()+ '/' + month+ '/' + new Date(Date.now()).getDate() + ' ' + new Date(Date.now()).getHours() + ':' + new Date(Date.now()).getMinutes() + ':' + new Date(Date.now()).getSeconds()})
ctx.body="修改成功"
next()
// console.log(new Date(Date.now()).getFullYear ()+ '/' + month+ '/' + new Date(Date.now()).getDate() + ' ' + new Date(Date.now()).getHours() + ':' + new Date(Date.now()).getMinutes())
// console.log(new Date(Date.now()).getDate())
}
module.exports = {
'POST /articleadd': articleadd,
'GET /articlefind': articlefind,
'GET /articledel': articledel,
'POST /articleupdate': articleupdate
};