feat: delete page
This commit is contained in:
@@ -5,6 +5,8 @@ const Promise = require('bluebird')
|
||||
const Knex = require('knex')
|
||||
const Objection = require('objection')
|
||||
|
||||
const migrationSource = require('../db/migrator-source')
|
||||
|
||||
/* global WIKI */
|
||||
|
||||
/**
|
||||
@@ -89,12 +91,14 @@ module.exports = {
|
||||
|
||||
// Set init tasks
|
||||
|
||||
console.info(migrationSource)
|
||||
|
||||
let initTasks = {
|
||||
// -> Migrate DB Schemas
|
||||
async syncSchemas() {
|
||||
return self.knex.migrate.latest({
|
||||
directory: path.join(WIKI.SERVERPATH, 'db/migrations'),
|
||||
tableName: 'migrations'
|
||||
tableName: 'migrations',
|
||||
migrationSource
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,8 @@
|
||||
const _ = require('lodash')
|
||||
const cfgHelper = require('../helpers/config')
|
||||
const Promise = require('bluebird')
|
||||
const fs = require('fs-extra')
|
||||
const path = require('path')
|
||||
|
||||
/* global WIKI */
|
||||
|
||||
@@ -22,6 +24,9 @@ module.exports = {
|
||||
}
|
||||
})
|
||||
|
||||
// Clear content cache
|
||||
fs.emptyDir(path.join(WIKI.ROOTPATH, 'data/cache'))
|
||||
|
||||
return this
|
||||
},
|
||||
/**
|
||||
|
15
server/db/migrations/2.0.0-beta.11.js
Normal file
15
server/db/migrations/2.0.0-beta.11.js
Normal file
@@ -0,0 +1,15 @@
|
||||
exports.up = knex => {
|
||||
return knex.schema
|
||||
.table('pageHistory', table => {
|
||||
table.string('action').defaultTo('updated')
|
||||
table.dropForeign('pageId')
|
||||
})
|
||||
}
|
||||
|
||||
exports.down = knex => {
|
||||
return knex.schema
|
||||
.table('pageHistory', table => {
|
||||
table.dropColumn('action')
|
||||
table.integer('pageId').unsigned().references('id').inTable('pages')
|
||||
})
|
||||
}
|
28
server/db/migrator-source.js
Normal file
28
server/db/migrator-source.js
Normal file
@@ -0,0 +1,28 @@
|
||||
const path = require('path')
|
||||
const fs = require('fs-extra')
|
||||
const semver = require('semver')
|
||||
|
||||
/* global WIKI */
|
||||
|
||||
module.exports = {
|
||||
/**
|
||||
* Gets the migration names
|
||||
* @returns Promise<string[]>
|
||||
*/
|
||||
async getMigrations() {
|
||||
const absoluteDir = path.join(WIKI.SERVERPATH, 'db/migrations')
|
||||
const migrationFiles = await fs.readdirAsync(absoluteDir)
|
||||
return migrationFiles.sort(semver.compare).map(m => ({
|
||||
file: m,
|
||||
directory: absoluteDir
|
||||
}))
|
||||
},
|
||||
|
||||
getMigrationName(migration) {
|
||||
return migration.file;
|
||||
},
|
||||
|
||||
getMigration(migration) {
|
||||
return require(path.join(WIKI.SERVERPATH, 'db/migrations', migration.file));
|
||||
}
|
||||
}
|
@@ -29,8 +29,11 @@ module.exports = {
|
||||
page
|
||||
}
|
||||
},
|
||||
async delete(obj, args) {
|
||||
await WIKI.models.groups.query().deleteById(args.id)
|
||||
async delete(obj, args, context) {
|
||||
await WIKI.models.pages.deletePage({
|
||||
...args,
|
||||
authorId: context.req.user.id
|
||||
})
|
||||
return {
|
||||
responseResult: graphHelper.generateSuccess('Page has been deleted.')
|
||||
}
|
||||
|
@@ -99,7 +99,8 @@ module.exports = class PageHistory extends Model {
|
||||
path: opts.path,
|
||||
publishEndDate: opts.publishEndDate || '',
|
||||
publishStartDate: opts.publishStartDate || '',
|
||||
title: opts.title
|
||||
title: opts.title,
|
||||
action: opts.action || 'updated'
|
||||
})
|
||||
}
|
||||
|
||||
@@ -109,6 +110,7 @@ module.exports = class PageHistory extends Model {
|
||||
'pageHistory.id',
|
||||
'pageHistory.path',
|
||||
'pageHistory.authorId',
|
||||
'pageHistory.action',
|
||||
'pageHistory.createdAt',
|
||||
{
|
||||
authorName: 'author.name'
|
||||
@@ -130,6 +132,7 @@ module.exports = class PageHistory extends Model {
|
||||
'pageHistory.id',
|
||||
'pageHistory.path',
|
||||
'pageHistory.authorId',
|
||||
'pageHistory.action',
|
||||
'pageHistory.createdAt',
|
||||
{
|
||||
authorName: 'author.name'
|
||||
|
@@ -96,6 +96,7 @@ module.exports = class Page extends Model {
|
||||
|
||||
static get cacheSchema() {
|
||||
return new JSBinType({
|
||||
id: 'uint',
|
||||
authorId: 'uint',
|
||||
authorName: 'string',
|
||||
createdAt: 'string',
|
||||
@@ -150,7 +151,10 @@ module.exports = class Page extends Model {
|
||||
if (!ogPage) {
|
||||
throw new Error('Invalid Page Id')
|
||||
}
|
||||
await WIKI.models.pageHistory.addVersion(ogPage)
|
||||
await WIKI.models.pageHistory.addVersion({
|
||||
...ogPage,
|
||||
action: 'updated'
|
||||
})
|
||||
await WIKI.models.pages.query().patch({
|
||||
authorId: opts.authorId,
|
||||
content: opts.content,
|
||||
@@ -174,6 +178,23 @@ module.exports = class Page extends Model {
|
||||
return page
|
||||
}
|
||||
|
||||
static async deletePage(opts) {
|
||||
const page = await WIKI.models.pages.query().findById(opts.id)
|
||||
if (!page) {
|
||||
throw new Error('Invalid Page Id')
|
||||
}
|
||||
await WIKI.models.pageHistory.addVersion({
|
||||
...page,
|
||||
action: 'deleted'
|
||||
})
|
||||
await WIKI.models.pages.query().delete().where('id', page.id)
|
||||
await WIKI.models.pages.deletePageFromCache(page)
|
||||
await WIKI.models.storage.pageEvent({
|
||||
event: 'deleted',
|
||||
page
|
||||
})
|
||||
}
|
||||
|
||||
static async renderPage(page) {
|
||||
const pipeline = await WIKI.models.renderers.getRenderingPipeline(page.contentType)
|
||||
WIKI.queue.job.renderPage.add({
|
||||
@@ -232,6 +253,7 @@ module.exports = class Page extends Model {
|
||||
static async savePageToCache(page) {
|
||||
const cachePath = path.join(process.cwd(), `data/cache/${page.hash}.bin`)
|
||||
await fs.outputFile(cachePath, WIKI.models.pages.cacheSchema.encode({
|
||||
id: page.id,
|
||||
authorId: page.authorId,
|
||||
authorName: page.authorName,
|
||||
createdAt: page.createdAt,
|
||||
@@ -270,4 +292,8 @@ module.exports = class Page extends Model {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
static async deletePageFromCache(page) {
|
||||
return fs.remove(path.join(process.cwd(), `data/cache/${page.hash}.bin`))
|
||||
}
|
||||
}
|
||||
|
@@ -20,6 +20,7 @@ block body
|
||||
:author-id=page.authorId
|
||||
:is-published=page.isPublished.toString()
|
||||
:toc=page.toc
|
||||
:page-id=page.id
|
||||
)
|
||||
template(slot='sidebar')
|
||||
each navItem in sidebar
|
||||
|
Reference in New Issue
Block a user