feat: delete page

This commit is contained in:
Nicolas Giard
2019-01-26 18:35:56 -05:00
committed by Nick
parent faa1f389d9
commit 658c105ab5
24 changed files with 2126 additions and 2955 deletions

View File

@@ -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
})
}
}

View File

@@ -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
},
/**

View 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')
})
}

View 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));
}
}

View 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.')
}

View File

@@ -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'

View File

@@ -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`))
}
}

View File

@@ -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