All files / lib/controllers comment.ts

24.14% Statements 7/29
0% Branches 0/6
14.29% Functions 1/7
24.14% Lines 7/29

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          16x 16x 16x   16x                   16x                                                                 16x                                         16x    
import Crowi from 'server/crowi'
import ApiResponse from '../util/apiResponse'
 
export default (crowi: Crowi) => {
  // var debug = Debug('crowi:routs:comment')
  const Comment = crowi.model('Comment')
  const actions = {} as any
  const api = {} as any
 
  actions.api = api
 
  /**
   * @api {get} /comments.get Get comments of the page of the revision
   * @apiName GetComments
   * @apiGroup Comment
   *
   * @apiParam {String} page_id Page Id.
   * @apiParam {String} revision_id Revision Id.
   */
  api.get = function(req, res) {
    var pageId = req.query.page_id
    var revisionId = req.query.revision_id
 
    if (revisionId) {
      return Comment.getCommentsByRevisionId(revisionId)
        .then(function(comments) {
          res.json(ApiResponse.success({ comments }))
        })
        .catch(function(err) {
          res.json(ApiResponse.error(err))
        })
    }
 
    return Comment.getCommentsByPageId(pageId)
      .then(function(comments) {
        res.json(ApiResponse.success({ comments }))
      })
      .catch(function(err) {
        res.json(ApiResponse.error(err))
      })
  }
 
  /**
   * @api {post} /comments.add Post comment for the page
   * @apiName PostComment
   * @apiGroup Comment
   *
   * @apiParam {String} page_id Page Id.
   * @apiParam {String} revision_id Revision Id.
   * @apiParam {String} comment Comment body
   * @apiParam {Number} comment_position=-1 Line number of the comment
   */
  api.add = async function(req, res) {
    if (!req.form.isValid) {
      return res.json(ApiResponse.error('Invalid comment.'))
    }
 
    const form = req.form.commentForm
    const page = form.page_id
    const creator = req.user._id
    const revision = form.revision_id
    const comment = form.comment
    const commentPosition = form.comment_position || undefined
 
    try {
      let createdComment = await Comment.create({ page, creator, revision, comment, commentPosition })
      createdComment = await createdComment.populate('creator').execPopulate()
      return res.json(ApiResponse.success({ comment: createdComment }))
    } catch (err) {
      return res.json(ApiResponse.error(err))
    }
  }
 
  return actions
}