Răsfoiți Sursa

Implement: pages.update

Sotaro KARASAWA 9 ani în urmă
părinte
comite
513bbe6060
2 a modificat fișierele cu 32 adăugiri și 3 ștergeri
  1. 1 0
      lib/routes/index.js
  2. 31 3
      lib/routes/page.js

+ 1 - 0
lib/routes/index.js

@@ -94,6 +94,7 @@ module.exports = function(crowi, app) {
   // HTTP RPC Styled API (に徐々に移行していいこうと思う)
   app.get('/_api/users.list'          , accessTokenParser , loginRequired(crowi, app) , user.api.list);
   app.post('/_api/pages.create'       , accessTokenParser , loginRequired(crowi, app) , csrf, page.api.create);
+  app.post('/_api/pages.update'       , accessTokenParser , loginRequired(crowi, app) , csrf, page.api.update);
   app.get('/_api/pages.get'           , accessTokenParser , loginRequired(crowi, app) , page.api.get);
   app.get('/_api/pages.updatePost'    , accessTokenParser , loginRequired(crowi, app) , page.api.getUpdatePost);
   app.post('/_api/pages.seen'         , accessTokenParser , loginRequired(crowi, app) , page.api.seen);

+ 31 - 3
lib/routes/page.js

@@ -568,15 +568,43 @@ module.exports = function(crowi, app) {
    * @apiParam {String} body
    * @apiParam {String} page_id
    * @apiParam {String} revision_id
+   * @apiParam {String} grant
    *
    * In the case of the page exists:
    * - If revision_id is specified => update the page,
    * - If revision_id is not specified => error.
    */
   api.update = function(req, res){
-    var pagePath = req.query.path || null;
-    var pageId = req.query.page_id || null;
-    var revisionId = req.query.revision_id || null;
+    var pageBody = req.body.body || null;
+    var pageId = req.body.page_id || null;
+    var revisionId = req.body.revision_id || null;
+    var grant = req.body.grant || null;
+
+    if (pageId === null || pageBody === null) {
+      return res.json(ApiResponse.error('page_id, revision_id, body is required.'));
+    }
+
+    Page.findPageByIdAndGrantedUser(pageId, req.user)
+    .then(function(pageData) {
+      if (pageData && !pageData.isUpdatable(revisionId)) {
+        throw new Error('Revision error.');
+      };
+
+      var grantOption = {grant: pageData.grant};
+      if (grant !== null) {
+        grantOption.grant = grant;
+      }
+      return Page.updatePage(pageData, pageBody, req.user, grantOption);
+    }).then(function(pageData) {
+      var result = {
+        page: pageData.toObject(),
+      };
+      result.page.lastUpdateUser = User.filterToPublicFields(result.page.lastUpdateUser);
+      return res.json(ApiResponse.success(result));
+    }).catch(function(err) {
+      debug('error on _api/pages.update', err);
+      return res.json(ApiResponse.error(err));
+    });
 
   };