فهرست منبع

Imprv/121 Add renameRecursively method

ttaka66 8 سال پیش
والد
کامیت
1ee48eacea
2فایلهای تغییر یافته به همراه32 افزوده شده و 42 حذف شده
  1. 30 41
      lib/models/page.js
  2. 2 1
      lib/routes/page.js

+ 30 - 41
lib/models/page.js

@@ -505,21 +505,6 @@ module.exports = function(crowi) {
     });
     });
   };
   };
 
 
-  // find recursive page by path
-  pageSchema.statics.findRecursivePageByPath = function(path) {
-    var Page = this
-      , pathRegExp = new RegExp('^' + path, 'i')
-
-    return new Promise(function(resolve, reject) {
-      Page
-        .find({ path: pathRegExp })
-        .exec()
-        .then(function(pages) {
-          return resolve(pages);
-        });
-    });
-  };
-
   pageSchema.statics.findListByPageIds = function(ids, options) {
   pageSchema.statics.findListByPageIds = function(ids, options) {
     var Page = this;
     var Page = this;
     var User = crowi.model('User');
     var User = crowi.model('User');
@@ -708,7 +693,7 @@ module.exports = function(crowi) {
     return q;
     return q;
   }
   }
 
 
-  pageSchema.statics.updatePageProperty = function(page, updateData, isRename) {
+  pageSchema.statics.updatePageProperty = function(page, updateData) {
     var Page = this;
     var Page = this;
     return new Promise(function(resolve, reject) {
     return new Promise(function(resolve, reject) {
       // TODO foreach して save
       // TODO foreach して save
@@ -717,10 +702,6 @@ module.exports = function(crowi) {
           return reject(err);
           return reject(err);
         }
         }
 
 
-        if (isRename) {
-          return resolve({ oldPagePath: page.path, newPagePath: updateData.path });
-        }
-
         return resolve(data);
         return resolve(data);
       });
       });
     });
     });
@@ -1017,40 +998,48 @@ module.exports = function(crowi) {
       })
       })
   };
   };
 
 
-  pageSchema.statics.rename = function(pageData, newPagePathPrefix, user, options) {
-
+  pageSchema.statics.rename = function(pageData, newPagePath, user, options) {
     var Page = this
     var Page = this
       , Revision = crowi.model('Revision')
       , Revision = crowi.model('Revision')
       , path = pageData.path
       , path = pageData.path
-      , pathRegExp = new RegExp('^' + path, 'i')
       , createRedirectPage = options.createRedirectPage || 0
       , createRedirectPage = options.createRedirectPage || 0
       , moveUnderTrees     = options.moveUnderTrees || 0;
       , moveUnderTrees     = options.moveUnderTrees || 0;
 
 
     return new Promise(function(resolve, reject) {
     return new Promise(function(resolve, reject) {
       // pageData の path を変更
       // pageData の path を変更
+      Page.updatePageProperty(pageData, {updatedAt: Date.now(), path: newPagePath, lastUpdateUser: user})
+      .then(function(data) {
+        // reivisions の path を変更
+        return Revision.updateRevisionListByPath(path, {path: newPagePath}, {})
+      }).then(function(data) {
+        pageData.path = newPagePath;
+
+        if (createRedirectPage) {
+          var body = 'redirect ' + newPagePath;
+          Page.create(path, body, user, {redirectTo: newPagePath}).then(resolve).catch(reject);
+        } else {
+          resolve(data);
+        }
+        pageEvent.emit('update', pageData, user); // update as renamed page
+      });
+    });
+  };
+
+  pageSchema.statics.renameRecursively = function(pageData, newPagePathPrefix, user, options) {
+    var Page = this
+      , path = pageData.path
+      , pathRegExp = new RegExp('^' + path, 'i')
+
+    return new Promise(function(resolve, reject) {
       Page
       Page
-      .findRecursivePageByPath(path)
+      .generateQueryToListByStartWith(path, user, options)
       .then(function(pages) {
       .then(function(pages) {
         Promise.all(pages.map(function(page) {
         Promise.all(pages.map(function(page) {
           newPagePath = page.path.replace(pathRegExp, newPagePathPrefix)
           newPagePath = page.path.replace(pathRegExp, newPagePathPrefix)
-          return Page.updatePageProperty(page, { updatedAt: Date.now(), path: newPagePath, lastUpdateUser: user });
+          return Page.rename(page, newPagePath, user, options);
         }))
         }))
-        .then(function(pagePaths) {
-          // reivisions の path を変更
-          return pagePaths.map(function(pagePaths) {
-            return Revision.updateRevisionListByPath(pagePaths.oldPagePath, { path: pagePaths.newPagePath }, {});
-          });
-        })
-        .then(function(data) {
-          pageData.path = newPagePathPrefix;
-
-          if (createRedirectPage) {
-            var body = 'redirect ' + newPagePath;
-            Page.create(path, body, user, { redirectTo: newPagePath }).then(resolve).catch(reject);
-          } else {
-            resolve(data);
-          }
-          pageEvent.emit('update', pageData, user); // update as renamed page
+        .then(function() {
+          return resolve();
         });
         });
       });
       });
     });
     });

+ 2 - 1
lib/routes/page.js

@@ -1093,9 +1093,10 @@ module.exports = function(crowi, app) {
           throw new Error('Someone could update this page, so couldn\'t delete.');
           throw new Error('Someone could update this page, so couldn\'t delete.');
         }
         }
 
 
-        return Page.rename(pageData, newPagePath, req.user, options);
+        return Page.renameRecursively(pageData, newPagePath, req.user, options);
       }).then(function() {
       }).then(function() {
         var result = {};
         var result = {};
+        page.path = newPagePath
         result.page = page;
         result.page = page;
 
 
         return res.json(ApiResponse.success(result));
         return res.json(ApiResponse.success(result));