All files / lib/controllers revision.ts

25% Statements 9/36
0% Branches 0/14
7.14% Functions 1/14
25% Lines 9/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101          16x 16x 16x 16x 16x                 16x                                             16x                                                       16x                                                         16x    
import Crowi from 'server/crowi'
import Debug from 'debug'
import ApiResponse from '../util/apiResponse'
 
export default (crowi: Crowi) => {
  const debug = Debug('crowi:routes:revision')
  const Page = crowi.model('Page')
  const Revision = crowi.model('Revision')
  const actions = {} as any
  actions.api = {} as any
 
  /**
   * @api {get} /revisions.get Get revision
   * @apiName GetRevision
   * @apiGroup Revision
   *
   * @apiParam {String} revision_id Revision Id.
   */
  actions.api.get = function(req, res) {
    var revisionId = req.query.revision_id
 
    Revision.findRevision(revisionId)
      .then(function(revisionData) {
        var result = {
          revision: revisionData,
        }
        return res.json(ApiResponse.success(result))
      })
      .catch(function(err) {
        debug('Error revisios.get', err)
        return res.json(ApiResponse.error(err))
      })
  }
 
  /**
   * @api {get} /revisions.ids Get revision id list of the page
   * @apiName ids
   * @apiGroup Revision
   *
   * @apiParam {String} page_id      Page Id.
   */
  actions.api.ids = function(req, res) {
    var pageId = req.query.page_id || null
 
    if (pageId && crowi.isPageId(pageId)) {
      Page.findPageByIdAndGrantedUser(pageId, req.user)
        .then(function(pageData) {
          debug('Page found', pageData._id, pageData.path)
          return Revision.findRevisionIdList(pageData.path)
        })
        .then(function(revisions) {
          return res.json(ApiResponse.success({ revisions }))
        })
        .catch(function(err) {
          return res.json(ApiResponse.error(err))
        })
    } else {
      return res.json(ApiResponse.error('Parameter error.'))
    }
  }
 
  /**
   * @api {get} /revisions.list Get revisions
   * @apiName ListRevision
   * @apiGroup Revision
   *
   * @apiParam {String} revision_ids Revision Ids.
   * @apiParam {String} page_id      Page Id.
   */
  actions.api.list = function(req, res) {
    var revisionIds = (req.query.revision_ids || '').split(',')
    var pageId = req.query.page_id || null
 
    if (pageId) {
      Page.findPageByIdAndGrantedUser(pageId, req.user)
        .then(function(pageData) {
          debug('Page found', pageData._id, pageData.path)
          return Revision.findRevisionList(pageData.path, {})
        })
        .then(function(revisions) {
          return res.json(ApiResponse.success(revisions))
        })
        .catch(function(err) {
          return res.json(ApiResponse.error(err))
        })
    } else if (revisionIds.length > 0) {
      Revision.findRevisions(revisionIds)
        .then(function(revisions) {
          return res.json(ApiResponse.success(revisions))
        })
        .catch(function(err) {
          return res.json(ApiResponse.error(err))
        })
    } else {
      return res.json(ApiResponse.error('Parameter error.'))
    }
  }
 
  return actions
}