All files / controllers comment.js

30% Statements 9/30
0% Branches 0/4
14.29% Functions 1/7
30% Lines 9/30

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 7515x       15x 15x 15x 15x   15x                   15x                                                                 15x                                       15x    
module.exports = function(crowi, app) {
  'use strict'
 
  // var debug = require('debug')('crowi:routs:comment')
  var Comment = crowi.model('Comment')
  var ApiResponse = require('../util/apiResponse')
  var actions = {}
  var api = {}
 
  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 pageId = form.page_id
    const revisionId = form.revision_id
    const comment = form.comment
    const position = form.comment_position
 
    try {
      let createdComment = await Comment.create(pageId, req.user._id, revisionId, comment, position)
      createdComment = await createdComment.populate('creator').execPopulate()
      return res.json(ApiResponse.success({ comment: createdComment }))
    } catch (err) {
      return res.json(ApiResponse.error(err))
    }
  }
 
  return actions
}