itizawa 5 лет назад
Родитель
Сommit
58f96e1325
2 измененных файлов с 82 добавлено и 11 удалено
  1. 9 11
      src/client/js/services/PageContainer.js
  2. 73 0
      src/server/routes/apiv3/pages.js

+ 9 - 11
src/client/js/services/PageContainer.js

@@ -377,19 +377,17 @@ export default class PageContainer extends Container {
     });
   }
 
-  rename(pageNameInput, isRenameRecursively, isRenameRedirect, isRenameMetadata) {
+  rename(newPagePath, isRecursively, isRenameRedirect, isRemainMetadata) {
     const socketIoContainer = this.appContainer.getContainer('SocketIoContainer');
-    const isRecursively = isRenameRecursively ? true : null;
-    const isRedirect = isRenameRedirect ? true : null;
-    const isRemain = isRenameMetadata ? true : null;
+    const { pageId, revisionId } = this.state;
 
-    return this.appContainer.apiPost('/pages.rename', {
-      recursively: isRecursively,
-      page_id: this.state.pageId,
-      revision_id: this.state.revisionId,
-      new_path: pageNameInput,
-      create_redirect: isRedirect,
-      remain_metadata: isRemain,
+    return this.appContainer.apiv3Put('/pages/rename', {
+      revisionId,
+      pageId,
+      isRecursively,
+      isRenameRedirect,
+      isRemainMetadata,
+      newPagePath,
       socketClientId: socketIoContainer.getSocketClientId(),
     });
   }

+ 73 - 0
src/server/routes/apiv3/pages.js

@@ -152,6 +152,79 @@ module.exports = (crowi) => {
     }
   });
 
+  // TODO swagger and validation
+  router.put('/rename', accessTokenParser, loginRequiredStrictly, csrf, async(req, res) => {
+    const { pageId, isRecursively,revisionId } = req.body;
+
+    let newPagePath = pathUtils.normalizePath(req.body.newPagePath);
+
+    const options = {
+      createRedirectPage: req.body.isRenameRedirect,
+      updateMetadata: req.body.isRemainMetadata,
+      socketClientId: +req.body.socketClientId || undefined,
+    };
+
+    if (!Page.isCreatableName(newPagePath)) {
+      res.code = 'invalid_path';
+      return res.apiv3Err(`Could not use the path '${newPagePath})'`, 409);
+    }
+
+    console.log(newPagePath);
+
+    // check whether path starts slash
+    newPagePath = pathUtils.addHeadingSlash(newPagePath);
+
+    const isExist = await Page.count({ path: newPagePath }) > 0;
+    if (isExist) {
+      // if page found, cannot cannot rename to that path
+      res.code = 'already_exists';
+      return res.apiv3Err(`'new_path=${newPagePath}' already exists`, 409);
+    }
+
+    let page;
+
+    try {
+      page = await Page.findByIdAndViewer(pageId, req.user);
+
+      if (page == null) {
+        res.code = 'notfound_or_forbidden';
+        return res.apiv3Err(`Page '${pageId}' is not found or forbidden`, 401);
+      }
+
+      if (!page.isUpdatable(revisionId)) {
+        res.code = 'notfound_or_forbidden';
+        return res.apiv3Err('Someone could update this page, so couldn\'t delete.', 409);
+      }
+
+      if (isRecursively) {
+        page = await Page.renameRecursively(page, newPagePath, req.user, options);
+      }
+      else {
+        page = await Page.rename(page, newPagePath, req.user, options);
+      }
+    }
+    catch (err) {
+      logger.error(err);
+      res.code = 'unknown';
+      return res.apiv3Err('Failed to update page.', 500);
+    }
+
+    // result.page = page; // TODO consider to use serializeToObj method -- 2018.08.06 Yuki Takei
+
+    try {
+      // global notification
+      await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_MOVE, page, req.user, {
+        oldPath: req.body.path,
+      });
+    }
+    catch (err) {
+      logger.error('Move notification failed', err);
+    }
+
+    return res.apiv3({ page });
+  });
+
+
   /**
   * @swagger
   *