nfp_sites/api/article/model.mjs

145 lines
3.5 KiB
JavaScript
Raw Normal View History

2019-09-13 13:33:10 +00:00
import bookshelf from '../bookshelf.mjs'
import Media from '../media/model.mjs'
2019-09-14 19:03:38 +00:00
import File from '../file/model.mjs'
2019-09-13 13:33:10 +00:00
import Staff from '../staff/model.mjs'
import Page from '../page/model.mjs'
/*
Article model:
{
name,
path,
description,
media_id,
staff_id,
parent_id,
is_deleted,
created_at,
updated_at,
}
*/
const Article = bookshelf.createModel({
tableName: 'articles',
parent() {
return this.belongsTo(Page, 'parent_id')
},
banner() {
return this.belongsTo(Media, 'banner_id')
},
media() {
return this.belongsTo(Media, 'media_id')
},
staff() {
return this.belongsTo(Staff, 'staff_id')
},
2019-09-14 19:03:38 +00:00
files() {
return this.hasManyFiltered(File, 'file', 'article_id')
.query(qb => {
qb.orderBy('id', 'asc')
})
},
2019-09-13 13:33:10 +00:00
}, {
2019-10-02 00:16:11 +00:00
getAll(ctx, where = {}, withRelated = [], orderBy = 'id', limitToday = false) {
2019-09-13 13:33:10 +00:00
return this.query(qb => {
2019-10-02 00:16:11 +00:00
this.baseQueryAll(ctx, qb, where, orderBy)
if (limitToday) {
qb.where('published_at', '<=', (new Date()).toISOString())
}
})
.fetchPage({
pageSize: ctx.state.pagination.perPage,
page: ctx.state.pagination.page,
withRelated,
ctx: ctx,
})
.then(result => {
ctx.state.pagination.total = result.pagination.rowCount
return result
})
},
getSingle(id, withRelated = [], require = true, ctx = null, limitToday = false) {
return this.query(qb => {
qb.where(subq => {
subq.where({ id: Number(id) || 0 })
.orWhere({ path: id })
})
if (limitToday && (!ctx || !ctx.state.user || ctx.state.user.level < 10)) {
qb.where('published_at', '<=', (new Date()).toISOString())
}
2019-09-13 13:33:10 +00:00
})
.fetch({ require, withRelated, ctx })
},
2019-09-14 19:03:38 +00:00
2019-10-02 00:16:11 +00:00
async getFeatured(withRelated = [], ctx = null) {
let data = await this.query(qb => {
qb.where({ is_featured: true })
.where('published_at', '<=', (new Date()).toISOString())
})
.fetch({ require: false, withRelated, ctx })
if (!data) {
data = await this.query(qb => {
qb.where('published_at', '<=', (new Date()).toISOString())
.whereNotNull('banner_id')
})
.fetch({ require: false, withRelated, ctx })
}
return data
},
getAllFromPage(ctx, pageId, withRelated = [], orderBy = 'id', limitToday = false) {
2019-09-14 19:03:38 +00:00
return this.query(qb => {
this.baseQueryAll(ctx, qb, {}, orderBy)
qb.leftOuterJoin('pages', 'articles.parent_id', 'pages.id')
qb.where(subq => {
subq.where('pages.id', pageId)
.orWhere('pages.parent_id', pageId)
})
2019-10-02 00:16:11 +00:00
if (limitToday) {
qb.where('published_at', '<=', (new Date()).toISOString())
}
2019-09-14 19:03:38 +00:00
qb.select('articles.*')
})
.fetchPage({
pageSize: ctx.state.pagination.perPage,
page: ctx.state.pagination.page,
withRelated,
ctx: ctx,
})
.then(result => {
ctx.state.pagination.total = result.pagination.rowCount
return result
})
},
2019-10-01 03:45:44 +00:00
2019-10-02 00:16:11 +00:00
setAllUnfeatured() {
return bookshelf.knex('articles')
.where({ is_featured: true })
.update({
is_featured: false,
})
},
getFrontpageArticles(page = 1) {
2019-10-01 03:45:44 +00:00
return this.query(qb => {
2019-10-02 00:16:11 +00:00
qb.orderBy('published_at', 'DESC')
.where('published_at', '<=', (new Date()).toISOString())
2019-10-01 03:45:44 +00:00
})
.fetchPage({
pageSize: 10,
page: page,
2019-10-02 18:47:20 +00:00
withRelated: ['files', 'media', 'banner', 'parent', 'staff'],
2019-10-01 03:45:44 +00:00
})
},
2019-09-13 13:33:10 +00:00
})
export default Article