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 }) ) } if (ctx.req.files.media) { promises.push( this.upload(ctx.req.files.media) .then(res => { newMedia = res }) ) } await Promise.all(promises) console.log(newBanner) console.log(newMedia) ctx.body = {} } }