nfp_sites/api/article/routes.mjs

87 lines
1.9 KiB
JavaScript
Raw Normal View History

2022-07-20 00:33:06 +00:00
import { parseFiles } from '../file/util.mjs'
import { upload } from '../media/upload.mjs'
export default class ArticleRoutes {
constructor(opts = {}) {
Object.assign(this, {
upload: upload,
})
}
/** GET: /api/articles/[path] */
async getArticle(ctx) {
let res = await ctx.db.safeCallProc('article_get_single', [ctx.params.path])
let out = {
article: res.results[0][0] || null,
files: parseFiles(res.results[1]),
}
ctx.body = out
}
/** GET: /api/auth/articles */
async auth_getAllArticles(ctx) {
let res = await ctx.db.safeCallProc('article_auth_get_all', [
ctx.state.auth_token,
Math.max(ctx.query.get('page') || 1, 1),
Math.min(ctx.query.get('per_page') || 10, 25)
])
let out = {
articles: res.results[0],
total_articles: res.results[0][0].total_articles,
}
ctx.body = out
}
/** GET: /api/auth/articles/:path */
async auth_getSingleArticle(ctx) {
let res = await ctx.db.safeCallProc('article_auth_get_update_create', [
ctx.state.auth_token,
ctx.params.path
])
let out = {
article: res.results[0][0] || null,
files: parseFiles(res.results[1]),
staff: res.results[2],
}
ctx.body = out
}
/** PUT: /api/auth/articles/:path */
async auth_updateCreateSingleArticle(ctx) {
console.log(ctx.req.files)
console.log(ctx.req.body)
let newBanner = null
let newMedia = null
let promises = []
if (ctx.req.files.banner) {
promises.push(
this.upload(ctx.req.files.banner)
.then(res => { newBanner = res })
)
}
2022-07-21 07:06:16 +00:00
if (ctx.req.files.media) {
2022-07-20 00:33:06 +00:00
promises.push(
this.upload(ctx.req.files.media)
.then(res => { newMedia = res })
)
}
await Promise.all(promises)
console.log(newBanner)
console.log(newMedia)
ctx.body = {}
}
}