Selaa lähdekoodia

create api in Model

itizawa 6 vuotta sitten
vanhempi
sitoutus
2fc8610f8b
2 muutettua tiedostoa jossa 49 lisäystä ja 3 poistoa
  1. 7 0
      src/server/models/comment.js
  2. 42 3
      src/server/routes/comment.js

+ 7 - 0
src/server/models/comment.js

@@ -64,6 +64,13 @@ module.exports = function(crowi) {
     }));
   };
 
+  commentSchema.statics.updateCommentsByPageId = function(comment, isMarkdown, pageId) {
+    // TODO GW-61 update new comment
+    console.log('ここはmodel');
+
+    return pageId;
+  };
+
   commentSchema.statics.removeCommentsByPageId = function(pageId) {
     const Comment = this;
 

+ 42 - 3
src/server/routes/comment.js

@@ -153,13 +153,52 @@ module.exports = function(crowi, app) {
    * @apiGroup Comment
    *
    */
-  api.update = function(req, res) {
+  api.update = async function(req, res) {
+    const { commentForm } = req.body;
+    const { validationResult } = require('express-validator/check');
+
+    const errors = validationResult(req.body);
+    if (!errors.isEmpty()) {
+      // return res.json(ApiResponse.error('Invalid comment.'));
+      // return res.status(422).json({ errors: errors.array() });
+      return res.json(ApiResponse.error('コメントを入力してください。'));
+    }
 
-    if (req.body.commentForm.comment_id == null) {
+    const pageId = commentForm.page_id;
+    const revisionId = commentForm.revision_id;
+    const comment = commentForm.comment;
+    const isMarkdown = commentForm.is_markdown;
+    const commentId = commentForm.comment_id;
+
+    if (commentId == null) {
       return Promise.resolve(res.json(ApiResponse.error('\'comment_id\' is undefined')));
     }
 
-    return console.log('Idが渡ってきています');
+    // check whether accessible
+    const isAccessible = await Page.isAccessiblePageByViewer(pageId, req.user._id, revisionId, comment, isMarkdown, req.user);
+    if (!isAccessible) {
+      return res.json(ApiResponse.error('Current user is not accessible to this page.'));
+    }
+
+    const updatedComment = await Comment.updateCommentsByPageId(comment, isMarkdown, commentId);
+    // TODO GW-61 catch err
+    // .catch((err) => {
+    //   return res.json(ApiResponse.error(err));
+    // });
+
+    // update page
+    const page = await Page.findOneAndUpdate({ _id: pageId }, {
+      lastUpdateUser: req.user,
+      updatedAt: new Date(),
+    });
+
+    res.json(ApiResponse.success({ comment: updatedComment }));
+
+    const path = page.path;
+
+    // global notification
+    globalNotificationService.notifyComment(updatedComment, path);
+
   };