mirror of https://github.com/requarks/wiki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
3.2 KiB
134 lines
3.2 KiB
7 years ago
|
const Model = require('objection').Model
|
||
|
|
||
6 years ago
|
/* global WIKI */
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Pages model
|
||
|
*/
|
||
|
module.exports = class Page extends Model {
|
||
|
static get tableName() { return 'pages' }
|
||
|
|
||
|
static get jsonSchema () {
|
||
|
return {
|
||
|
type: 'object',
|
||
|
required: ['path', 'title'],
|
||
|
|
||
|
properties: {
|
||
|
id: {type: 'integer'},
|
||
|
path: {type: 'string'},
|
||
|
title: {type: 'string'},
|
||
|
description: {type: 'string'},
|
||
|
isPublished: {type: 'boolean'},
|
||
|
publishStartDate: {type: 'string'},
|
||
|
publishEndDate: {type: 'string'},
|
||
|
content: {type: 'string'},
|
||
6 years ago
|
contentType: {type: 'string'},
|
||
7 years ago
|
|
||
|
createdAt: {type: 'string'},
|
||
|
updatedAt: {type: 'string'}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static get relationMappings() {
|
||
|
return {
|
||
|
tags: {
|
||
|
relation: Model.ManyToManyRelation,
|
||
|
modelClass: require('./tags'),
|
||
|
join: {
|
||
|
from: 'pages.id',
|
||
|
through: {
|
||
|
from: 'pageTags.pageId',
|
||
|
to: 'pageTags.tagId'
|
||
|
},
|
||
|
to: 'tags.id'
|
||
|
}
|
||
|
},
|
||
|
author: {
|
||
|
relation: Model.BelongsToOneRelation,
|
||
|
modelClass: require('./users'),
|
||
|
join: {
|
||
|
from: 'pages.authorId',
|
||
|
to: 'users.id'
|
||
|
}
|
||
|
},
|
||
6 years ago
|
creator: {
|
||
|
relation: Model.BelongsToOneRelation,
|
||
|
modelClass: require('./users'),
|
||
|
join: {
|
||
|
from: 'pages.creatorId',
|
||
|
to: 'users.id'
|
||
|
}
|
||
|
},
|
||
6 years ago
|
editor: {
|
||
|
relation: Model.BelongsToOneRelation,
|
||
|
modelClass: require('./editors'),
|
||
|
join: {
|
||
|
from: 'pages.editorKey',
|
||
|
to: 'editors.key'
|
||
|
}
|
||
|
},
|
||
7 years ago
|
locale: {
|
||
|
relation: Model.BelongsToOneRelation,
|
||
|
modelClass: require('./locales'),
|
||
|
join: {
|
||
6 years ago
|
from: 'pages.localeCode',
|
||
7 years ago
|
to: 'locales.code'
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$beforeUpdate() {
|
||
|
this.updatedAt = new Date().toISOString()
|
||
|
}
|
||
|
$beforeInsert() {
|
||
|
this.createdAt = new Date().toISOString()
|
||
|
this.updatedAt = new Date().toISOString()
|
||
|
}
|
||
6 years ago
|
|
||
|
static async createPage(opts) {
|
||
6 years ago
|
const page = await WIKI.models.pages.query().insertAndFetch({
|
||
6 years ago
|
authorId: opts.authorId,
|
||
|
content: opts.content,
|
||
6 years ago
|
creatorId: opts.authorId,
|
||
6 years ago
|
description: opts.description,
|
||
|
editorKey: opts.editor,
|
||
|
isPrivate: opts.isPrivate,
|
||
|
isPublished: opts.isPublished,
|
||
|
localeCode: opts.locale,
|
||
|
path: opts.path,
|
||
|
publishEndDate: opts.publishEndDate,
|
||
|
publishStartDate: opts.publishStartDate,
|
||
|
title: opts.title
|
||
|
})
|
||
6 years ago
|
await WIKI.models.storage.pageEvent({
|
||
6 years ago
|
event: 'created',
|
||
|
page
|
||
|
})
|
||
|
return page
|
||
|
}
|
||
|
|
||
|
static async updatePage(opts) {
|
||
6 years ago
|
const ogPage = await WIKI.models.pages.query().findById(opts.id)
|
||
6 years ago
|
if (!ogPage) {
|
||
|
throw new Error('Invalid Page Id')
|
||
|
}
|
||
6 years ago
|
await WIKI.models.pageHistory.addVersion(ogPage)
|
||
|
const page = await WIKI.models.pages.query().patchAndFetchById(ogPage.id, {
|
||
|
authorId: opts.authorId,
|
||
|
content: opts.content,
|
||
|
description: opts.description,
|
||
|
isPublished: opts.isPublished,
|
||
|
publishEndDate: opts.publishEndDate,
|
||
|
publishStartDate: opts.publishStartDate,
|
||
6 years ago
|
title: opts.title
|
||
6 years ago
|
})
|
||
|
await WIKI.models.storage.pageEvent({
|
||
6 years ago
|
event: 'updated',
|
||
|
page
|
||
|
})
|
||
6 years ago
|
return page
|
||
|
}
|
||
7 years ago
|
}
|