2019-09-13 13:33:10 +00:00
|
|
|
import Article from './model.mjs'
|
|
|
|
import * as security from './security.mjs'
|
|
|
|
|
|
|
|
export default class ArticleRoutes {
|
|
|
|
constructor(opts = {}) {
|
|
|
|
Object.assign(this, {
|
|
|
|
Article: opts.Article || Article,
|
|
|
|
security: opts.security || security,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/** GET: /api/articles */
|
|
|
|
async getAllArticles(ctx) {
|
|
|
|
await this.security.ensureIncludes(ctx)
|
|
|
|
|
2019-10-02 00:16:11 +00:00
|
|
|
ctx.body = await this.Article.getAll(ctx, { }, ctx.state.filter.includes, ctx.query.sort || '-published_at')
|
2019-09-14 19:03:38 +00:00
|
|
|
}
|
|
|
|
|
2019-09-13 13:33:10 +00:00
|
|
|
/** GET: /api/articles/:id */
|
|
|
|
async getSingleArticle(ctx) {
|
|
|
|
await this.security.ensureIncludes(ctx)
|
|
|
|
|
|
|
|
ctx.body = await this.Article.getSingle(ctx.params.id, ctx.state.filter.includes, true, ctx)
|
|
|
|
}
|
|
|
|
|
2019-10-02 00:16:11 +00:00
|
|
|
/** GET: /api/articles/public */
|
|
|
|
async getPublicAllArticles(ctx) {
|
|
|
|
await this.security.ensureIncludes(ctx)
|
|
|
|
|
|
|
|
ctx.body = await this.Article.getAll(ctx, { }, ctx.state.filter.includes, ctx.query.sort || '-published_at', true)
|
|
|
|
}
|
|
|
|
|
|
|
|
/** GET: /api/pages/:pageId/articles/public */
|
|
|
|
async getPublicAllPageArticles(ctx) {
|
|
|
|
await this.security.ensureIncludes(ctx)
|
|
|
|
|
|
|
|
ctx.body = await this.Article.getAllFromPage(ctx, ctx.params.pageId, ctx.state.filter.includes, ctx.query.sort || '-published_at', true)
|
|
|
|
}
|
|
|
|
|
|
|
|
/** GET: /api/articles/public/:id */
|
|
|
|
async getPublicSingleArticle(ctx) {
|
|
|
|
await this.security.ensureIncludes(ctx)
|
|
|
|
|
|
|
|
ctx.body = await this.Article.getSingle(ctx.params.id, ctx.state.filter.includes, true, ctx, true)
|
|
|
|
}
|
|
|
|
|
2019-09-13 13:33:10 +00:00
|
|
|
/** POST: /api/articles */
|
|
|
|
async createArticle(ctx) {
|
|
|
|
await this.security.validUpdate(ctx)
|
|
|
|
|
2019-10-02 18:47:20 +00:00
|
|
|
if (!ctx.request.body.staff_id) {
|
|
|
|
ctx.request.body.staff_id = ctx.state.user.id
|
|
|
|
}
|
|
|
|
|
2019-09-13 13:33:10 +00:00
|
|
|
ctx.body = await this.Article.create(ctx.request.body)
|
|
|
|
}
|
|
|
|
|
|
|
|
/** PUT: /api/articles/:id */
|
|
|
|
async updateArticle(ctx) {
|
|
|
|
await this.security.validUpdate(ctx)
|
|
|
|
|
2019-10-02 00:16:11 +00:00
|
|
|
if (ctx.request.body.is_featured) {
|
|
|
|
await Article.setAllUnfeatured()
|
|
|
|
}
|
|
|
|
|
2021-02-05 11:50:01 +00:00
|
|
|
let article = await this.Article.updateSingle(ctx, ctx.params.id, ctx.request.body)
|
2019-09-13 13:33:10 +00:00
|
|
|
|
2021-02-05 11:50:01 +00:00
|
|
|
ctx.body = article
|
2019-09-13 13:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** DELETE: /api/articles/:id */
|
|
|
|
async removeArticle(ctx) {
|
2021-02-05 11:50:01 +00:00
|
|
|
await this.Article.updateSingle(ctx, ctx.params.id, { is_deleted: true })
|
2019-09-13 13:33:10 +00:00
|
|
|
|
|
|
|
ctx.status = 204
|
|
|
|
}
|
|
|
|
}
|