nfp_sites/base/article/routes.mjs

140 lines
3.8 KiB
JavaScript
Raw Normal View History

import { FormidableHandler } from 'flaska'
2022-07-20 00:33:06 +00:00
import { parseFiles } from '../file/util.mjs'
2022-07-22 11:18:33 +00:00
import { parseArticles, parseArticle } from './util.mjs'
2022-07-20 00:33:06 +00:00
import { upload } from '../media/upload.mjs'
2022-07-27 08:41:18 +00:00
import { mediaToDatabase } from '../media/util.mjs'
2022-07-20 00:33:06 +00:00
export default class ArticleRoutes {
constructor(opts = {}) {
Object.assign(this, {
upload: upload,
})
}
register(server) {
server.flaska.get('/api/articles/:path', this.getArticle.bind(this))
server.flaska.get('/api/auth/articles', server.authenticate(), this.auth_getAllArticles.bind(this))
server.flaska.get('/api/auth/articles/:id', server.authenticate(), this.auth_getSingleArticle.bind(this))
server.flaska.put('/api/auth/articles/:id', [
server.authenticate(),
server.formidable({ maxFileSize: 20 * 1024 * 1024, }),
], this.auth_updateCreateSingleArticle.bind(this))
server.flaska.delete('/api/auth/articles/:id', server.authenticate(), this.auth_removeSingleArticle.bind(this))
}
2022-07-20 00:33:06 +00:00
/** GET: /api/articles/[path] */
async getArticle(ctx) {
2022-07-27 08:41:18 +00:00
let res = await ctx.db.safeCallProc('article_get_single', [ctx.params.path])
2022-07-20 00:33:06 +00:00
let out = {
2022-07-22 11:18:33 +00:00
article: parseArticle(res.results[0][0]),
2022-07-20 00:33:06 +00:00
files: parseFiles(res.results[1]),
}
ctx.body = out
}
/** GET: /api/auth/articles */
async auth_getAllArticles(ctx) {
2022-07-27 08:41:18 +00:00
let res = await ctx.db.safeCallProc('article_auth_get_all', [
2022-07-20 00:33:06 +00:00
ctx.state.auth_token,
Math.max(ctx.query.get('page') || 1, 1),
Math.min(ctx.query.get('per_page') || 10, 25)
])
let out = {
2022-07-22 11:18:33 +00:00
articles: parseArticles(res.results[0]),
2022-07-20 00:33:06 +00:00
total_articles: res.results[0][0].total_articles,
}
ctx.body = out
}
2022-07-22 11:18:33 +00:00
async private_getUpdateArticle(ctx, body = null, banner = null, media = null) {
let params = [
2022-07-20 00:33:06 +00:00
ctx.state.auth_token,
2022-07-27 08:41:18 +00:00
ctx.params.id === '0' ? null : ctx.params.id
2022-07-22 11:18:33 +00:00
]
if (body) {
params = params.concat([
body.name,
body.page_id === 'null' ? null : Number(body.page_id),
body.path,
body.content,
new Date(body.publish_at),
Number(body.admin_id),
body.is_featured === 'true' ? 1 : 0,
0,
])
2022-07-27 08:41:18 +00:00
params = params.concat(mediaToDatabase(banner, body.remove_banner === 'true'))
params = params.concat(mediaToDatabase(media, body.remove_media === 'true'))
2022-07-22 11:18:33 +00:00
}
console.log(params)
2022-07-27 08:41:18 +00:00
let res = await ctx.db.safeCallProc('article_auth_get_update_create', params)
2022-07-20 00:33:06 +00:00
let out = {
2022-07-27 08:41:18 +00:00
article: parseArticle(res.results[0][0]) || { publish_at: new Date() },
2022-07-20 00:33:06 +00:00
files: parseFiles(res.results[1]),
staff: res.results[2],
}
ctx.body = out
}
2022-07-22 11:18:33 +00:00
2022-07-27 08:41:18 +00:00
/** GET: /api/auth/articles/:id */
2022-07-22 11:18:33 +00:00
auth_getSingleArticle(ctx) {
return this.private_getUpdateArticle(ctx)
}
2022-07-27 08:41:18 +00:00
/** PUT: /api/auth/articles/:id */
2022-07-20 00:33:06 +00:00
async auth_updateCreateSingleArticle(ctx) {
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)
2022-07-22 11:18:33 +00:00
return this.private_getUpdateArticle(ctx, ctx.req.body, newBanner, newMedia)
}
2022-07-27 08:41:18 +00:00
/** DELETE: /api/auth/articles/:id */
2022-07-22 11:18:33 +00:00
async auth_removeSingleArticle(ctx) {
let params = [
ctx.state.auth_token,
2022-07-27 08:41:18 +00:00
ctx.params.id,
2022-07-22 11:18:33 +00:00
// Article data
null,
null,
null,
null,
null,
null,
null,
1,
]
2022-07-27 08:41:18 +00:00
params = params.concat(mediaToDatabase(null, true))
params = params.concat(mediaToDatabase(null, true))
2022-07-22 11:18:33 +00:00
await ctx.db.safeCallProc('article_auth_get_update_create', params)
ctx.status = 204
2022-07-20 00:33:06 +00:00
}
}