pages.js 3.25 KB
Newer Older
1 2
const Model = require('objection').Model

3 4
/* global WIKI */

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/**
 * 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'},
25
        contentType: {type: 'string'},
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

        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'
        }
      },
55 56 57 58 59 60 61 62
      creator: {
        relation: Model.BelongsToOneRelation,
        modelClass: require('./users'),
        join: {
          from: 'pages.creatorId',
          to: 'users.id'
        }
      },
NGPixel's avatar
NGPixel committed
63 64 65 66 67 68 69 70
      editor: {
        relation: Model.BelongsToOneRelation,
        modelClass: require('./editors'),
        join: {
          from: 'pages.editorKey',
          to: 'editors.key'
        }
      },
71 72 73 74
      locale: {
        relation: Model.BelongsToOneRelation,
        modelClass: require('./locales'),
        join: {
NGPixel's avatar
NGPixel committed
75
          from: 'pages.localeCode',
76 77 78 79 80 81 82 83 84 85 86 87 88
          to: 'locales.code'
        }
      }
    }
  }

  $beforeUpdate() {
    this.updatedAt = new Date().toISOString()
  }
  $beforeInsert() {
    this.createdAt = new Date().toISOString()
    this.updatedAt = new Date().toISOString()
  }
89 90

  static async createPage(opts) {
91
    const page = await WIKI.models.pages.query().insertAndFetch({
92 93
      authorId: opts.authorId,
      content: opts.content,
94
      creatorId: opts.authorId,
95 96 97 98 99 100 101 102 103 104
      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
    })
105
    await WIKI.models.storage.pageEvent({
106 107 108 109 110 111 112
      event: 'created',
      page
    })
    return page
  }

  static async updatePage(opts) {
113
    const ogPage = await WIKI.models.pages.query().findById(opts.id)
114 115 116
    if (!ogPage) {
      throw new Error('Invalid Page Id')
    }
117 118 119 120 121 122 123 124
    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,
125
      title: opts.title
126 127
    })
    await WIKI.models.storage.pageEvent({
128 129 130
      event: 'updated',
      page
    })
131 132
    return page
  }
133
}