nfp_sites/base/article/routes.mjs

150 lines
4.1 KiB
JavaScript
Raw Normal View History

2022-07-22 11:18:33 +00:00
import { parseArticles, parseArticle } from './util.mjs'
import { uploadMedia, uploadFile, deleteFile } 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, {
2022-08-12 23:33:50 +00:00
uploadMedia: uploadMedia,
uploadFile: uploadFile,
deleteFile: deleteFile,
requireAuth: opts.requireAuth,
2022-07-20 00:33:06 +00:00
})
}
register(server) {
if (!this.requireAuth) {
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, onlyReturn = false) {
2022-07-27 08:41:18 +00:00
let res = await ctx.db.safeCallProc('article_get_single', [ctx.params.path])
if (onlyReturn) {
return this.getArticle_resOutput(res)
}
2022-07-20 00:33:06 +00:00
2022-08-12 23:33:50 +00:00
ctx.body = this.getArticle_resOutput(res)
}
getArticle_resOutput(res) {
return {
2022-07-22 11:18:33 +00:00
article: parseArticle(res.results[0][0]),
2022-07-20 00:33:06 +00:00
}
}
/** 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') || 20, 100)
2022-07-20 00:33:06 +00:00
])
let out = {
2022-07-22 11:18:33 +00:00
articles: parseArticles(res.results[0]),
total_articles: res.results[1][0].total_articles,
2022-07-20 00:33:06 +00:00
}
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
}
2023-11-20 07:11:57 +00:00
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
2022-08-12 23:33:50 +00:00
ctx.body = this.private_getUpdateArticle_resOutput(res)
}
private_getUpdateArticle_resOutput(res) {
return {
article: parseArticle(res.results[0][0] || {}),
staff: res.results[1],
2022-07-20 00:33:06 +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) {
let newBanner = null
let newMedia = null
let promises = []
if (ctx.req.files.banner) {
promises.push(
2022-08-12 23:33:50 +00:00
this.uploadMedia(ctx.req.files.banner)
2022-07-20 00:33:06 +00:00
.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(
2022-08-12 23:33:50 +00:00
this.uploadMedia(ctx.req.files.media)
2022-07-20 00:33:06 +00:00
.then(res => { newMedia = res })
)
}
if (ctx.req.body.media && ctx.req.body.media.filename && ctx.req.body.media.type && ctx.req.body.media.path && ctx.req.body.media.size) {
newMedia = ctx.req.body.media
}
2022-07-20 00:33:06 +00:00
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
}
}