You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.2 KiB

6 years ago
6 years ago
  1. const Model = require('objection').Model
  2. /* global WIKI */
  3. /**
  4. * Pages model
  5. */
  6. module.exports = class Page extends Model {
  7. static get tableName() { return 'pages' }
  8. static get jsonSchema () {
  9. return {
  10. type: 'object',
  11. required: ['path', 'title'],
  12. properties: {
  13. id: {type: 'integer'},
  14. path: {type: 'string'},
  15. title: {type: 'string'},
  16. description: {type: 'string'},
  17. isPublished: {type: 'boolean'},
  18. publishStartDate: {type: 'string'},
  19. publishEndDate: {type: 'string'},
  20. content: {type: 'string'},
  21. contentType: {type: 'string'},
  22. createdAt: {type: 'string'},
  23. updatedAt: {type: 'string'}
  24. }
  25. }
  26. }
  27. static get relationMappings() {
  28. return {
  29. tags: {
  30. relation: Model.ManyToManyRelation,
  31. modelClass: require('./tags'),
  32. join: {
  33. from: 'pages.id',
  34. through: {
  35. from: 'pageTags.pageId',
  36. to: 'pageTags.tagId'
  37. },
  38. to: 'tags.id'
  39. }
  40. },
  41. author: {
  42. relation: Model.BelongsToOneRelation,
  43. modelClass: require('./users'),
  44. join: {
  45. from: 'pages.authorId',
  46. to: 'users.id'
  47. }
  48. },
  49. creator: {
  50. relation: Model.BelongsToOneRelation,
  51. modelClass: require('./users'),
  52. join: {
  53. from: 'pages.creatorId',
  54. to: 'users.id'
  55. }
  56. },
  57. editor: {
  58. relation: Model.BelongsToOneRelation,
  59. modelClass: require('./editors'),
  60. join: {
  61. from: 'pages.editorKey',
  62. to: 'editors.key'
  63. }
  64. },
  65. locale: {
  66. relation: Model.BelongsToOneRelation,
  67. modelClass: require('./locales'),
  68. join: {
  69. from: 'pages.localeCode',
  70. to: 'locales.code'
  71. }
  72. }
  73. }
  74. }
  75. $beforeUpdate() {
  76. this.updatedAt = new Date().toISOString()
  77. }
  78. $beforeInsert() {
  79. this.createdAt = new Date().toISOString()
  80. this.updatedAt = new Date().toISOString()
  81. }
  82. static async createPage(opts) {
  83. const page = await WIKI.models.pages.query().insertAndFetch({
  84. authorId: opts.authorId,
  85. content: opts.content,
  86. creatorId: opts.authorId,
  87. description: opts.description,
  88. editorKey: opts.editor,
  89. isPrivate: opts.isPrivate,
  90. isPublished: opts.isPublished,
  91. localeCode: opts.locale,
  92. path: opts.path,
  93. publishEndDate: opts.publishEndDate,
  94. publishStartDate: opts.publishStartDate,
  95. title: opts.title
  96. })
  97. await WIKI.models.storage.pageEvent({
  98. event: 'created',
  99. page
  100. })
  101. return page
  102. }
  103. static async updatePage(opts) {
  104. const ogPage = await WIKI.models.pages.query().findById(opts.id)
  105. if (!ogPage) {
  106. throw new Error('Invalid Page Id')
  107. }
  108. await WIKI.models.pageHistory.addVersion(ogPage)
  109. const page = await WIKI.models.pages.query().patchAndFetchById(ogPage.id, {
  110. authorId: opts.authorId,
  111. content: opts.content,
  112. description: opts.description,
  113. isPublished: opts.isPublished,
  114. publishEndDate: opts.publishEndDate,
  115. publishStartDate: opts.publishStartDate,
  116. title: opts.title
  117. })
  118. await WIKI.models.storage.pageEvent({
  119. event: 'updated',
  120. page
  121. })
  122. return page
  123. }
  124. }