feat: save page - updated + page history

This commit is contained in:
NGPixel
2018-07-22 21:13:01 -04:00
parent 076e923d48
commit 803d86ff63
11 changed files with 289 additions and 53 deletions

View File

@@ -68,6 +68,19 @@ exports.up = knex => {
table.string('createdAt').notNullable()
table.string('updatedAt').notNullable()
})
// PAGE HISTORY ------------------------
.createTable('pageHistory', table => {
table.increments('id').primary()
table.string('path').notNullable()
table.string('title').notNullable()
table.string('description')
table.boolean('isPrivate').notNullable().defaultTo(false)
table.boolean('isPublished').notNullable().defaultTo(false)
table.string('publishStartDate')
table.string('publishEndDate')
table.text('content')
table.string('createdAt').notNullable()
})
// PAGES -------------------------------
.createTable('pages', table => {
table.increments('id').primary()
@@ -126,6 +139,12 @@ exports.up = knex => {
// =====================================
// RELATION TABLES
// =====================================
// PAGE HISTORY TAGS ---------------------------
.createTable('pageHistoryTags', table => {
table.increments('id').primary()
table.integer('pageId').unsigned().references('id').inTable('pageHistory').onDelete('CASCADE')
table.integer('tagId').unsigned().references('id').inTable('tags').onDelete('CASCADE')
})
// PAGE TAGS ---------------------------
.createTable('pageTags', table => {
table.increments('id').primary()
@@ -149,10 +168,17 @@ exports.up = knex => {
table.integer('pageId').unsigned().references('id').inTable('pages')
table.integer('authorId').unsigned().references('id').inTable('users')
})
.table('pageHistory', table => {
table.integer('pageId').unsigned().references('id').inTable('pages')
table.string('editorKey').references('key').inTable('editors')
table.string('localeCode', 2).references('code').inTable('locales')
table.integer('authorId').unsigned().references('id').inTable('users')
})
.table('pages', table => {
table.string('editorKey').references('key').inTable('editors')
table.string('localeCode', 2).references('code').inTable('locales')
table.integer('authorId').unsigned().references('id').inTable('users')
table.integer('creatorId').unsigned().references('id').inTable('users')
})
.table('users', table => {
table.string('providerKey').references('key').inTable('authentication').notNullable().defaultTo('local')

View File

@@ -0,0 +1,100 @@
const Model = require('objection').Model
/* global WIKI */
/**
* Page History model
*/
module.exports = class PageHistory extends Model {
static get tableName() { return 'pageHistory' }
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'},
createdAt: {type: 'string'}
}
}
}
static get relationMappings() {
return {
tags: {
relation: Model.ManyToManyRelation,
modelClass: require('./tags'),
join: {
from: 'pageHistory.id',
through: {
from: 'pageHistoryTags.pageId',
to: 'pageHistoryTags.tagId'
},
to: 'tags.id'
}
},
page: {
relation: Model.BelongsToOneRelation,
modelClass: require('./pages'),
join: {
from: 'pageHistory.pageId',
to: 'pages.id'
}
},
author: {
relation: Model.BelongsToOneRelation,
modelClass: require('./users'),
join: {
from: 'pageHistory.authorId',
to: 'users.id'
}
},
editor: {
relation: Model.BelongsToOneRelation,
modelClass: require('./editors'),
join: {
from: 'pageHistory.editorKey',
to: 'editors.key'
}
},
locale: {
relation: Model.BelongsToOneRelation,
modelClass: require('./locales'),
join: {
from: 'pageHistory.localeCode',
to: 'locales.code'
}
}
}
}
$beforeInsert() {
this.createdAt = new Date().toISOString()
}
static async addVersion(opts) {
await WIKI.db.pageHistory.query().insert({
pageId: opts.id,
authorId: opts.authorId,
content: opts.content,
description: opts.description,
editorKey: opts.editorKey,
isPrivate: opts.isPrivate,
isPublished: opts.isPublished,
localeCode: opts.localeCode,
path: opts.path,
publishEndDate: opts.publishEndDate,
publishStartDate: opts.publishStartDate,
title: opts.title
})
}
}

View File

@@ -51,6 +51,14 @@ module.exports = class Page extends Model {
to: 'users.id'
}
},
creator: {
relation: Model.BelongsToOneRelation,
modelClass: require('./users'),
join: {
from: 'pages.creatorId',
to: 'users.id'
}
},
editor: {
relation: Model.BelongsToOneRelation,
modelClass: require('./editors'),
@@ -82,6 +90,7 @@ module.exports = class Page extends Model {
const page = await WIKI.db.pages.query().insertAndFetch({
authorId: opts.authorId,
content: opts.content,
creatorId: opts.authorId,
description: opts.description,
editorKey: opts.editor,
isPrivate: opts.isPrivate,
@@ -92,7 +101,26 @@ module.exports = class Page extends Model {
publishStartDate: opts.publishStartDate,
title: opts.title
})
await WIKI.db.storage.createPage(page)
await WIKI.db.storage.pageEvent({
event: 'created',
page
})
return page
}
static async updatePage(opts) {
const ogPage = await WIKI.db.pages.query().findById(opts.id)
if (!ogPage) {
throw new Error('Invalid Page Id')
}
await WIKI.db.pageHistory.addVersion(ogPage)
const page = await WIKI.db.pages.query().patchAndFetch({
title: opts.title
}).where('id', opts.id)
await WIKI.db.storage.pageEvent({
event: 'updated',
page
})
return page
}
}

View File

@@ -87,12 +87,12 @@ module.exports = class Storage extends Model {
}
}
static async createPage(page) {
static async pageEvent(event, page) {
const targets = await WIKI.db.storage.query().where('isEnabled', true)
if (targets && targets.length > 0) {
_.forEach(targets, target => {
WIKI.queue.job.syncStorage.add({
event: 'created',
event,
target,
page
}, {

View File

@@ -24,7 +24,6 @@ module.exports = {
async create(obj, args, context) {
const page = await WIKI.db.pages.createPage({
...args,
isPrivate: false,
authorId: context.req.user.id
})
return {
@@ -38,10 +37,14 @@ module.exports = {
responseResult: graphHelper.generateSuccess('Page has been deleted.')
}
},
async update(obj, args) {
await WIKI.db.groups.query().patch({ name: args.name }).where('id', args.id)
async update(obj, args, context) {
const page = await WIKI.db.pages.updatePage({
...args,
authorId: context.req.user.id
})
return {
responseResult: graphHelper.generateSuccess('Page has been updated.')
responseResult: graphHelper.generateSuccess('Page has been updated.'),
page
}
}
},

View File

@@ -52,6 +52,7 @@ type PageMutation {
content: String
description: String
editor: String
isPrivate: Boolean
isPublished: Boolean
locale: String
path: String
@@ -59,7 +60,7 @@ type PageMutation {
publishStartDate: Date
tags: [String]
title: String
): DefaultResponse
): PageResponse
delete(
id: Int!