feat: delete page
This commit is contained in:
@@ -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`))
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user