Parcourir la source

Add /_api/pages.list

b4b4r07 il y a 9 ans
Parent
commit
ef4fe2d03d
2 fichiers modifiés avec 62 ajouts et 0 suppressions
  1. 1 0
      lib/routes/index.js
  2. 61 0
      lib/routes/page.js

+ 1 - 0
lib/routes/index.js

@@ -95,6 +95,7 @@ module.exports = function(crowi, app) {
 
   // HTTP RPC Styled API (に徐々に移行していいこうと思う)
   app.get('/_api/users.list'          , accessTokenParser , loginRequired(crowi, app) , user.api.list);
+  app.get('/_api/pages.list'          , accessTokenParser , loginRequired(crowi, app) , page.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);

+ 61 - 0
lib/routes/page.js

@@ -526,6 +526,67 @@ module.exports = function(crowi, app) {
     });
   };
 
+  /**
+   * @api {get} /pages.list List pages by user
+   * @apiName ListPage
+   * @apiGroup Page
+   *
+   * @apiParam {String} path
+   * @apiParam {String} user
+   */
+  api.list = function(req, res) {
+    var username = req.query.user || null;
+    var path = req.query.path || null;
+    var limit = 50;
+    var offset = parseInt(req.query.offset) || 0;
+
+    var user;
+    var result = {};
+
+    var pagerOptions = { offset: offset, limit : limit };
+    var queryOptions = { offset: offset, limit : limit + 1};
+
+    if (username === null && path === null) {
+      return res.json(ApiResponse.error('Parameters user or path are required.'));
+    }
+
+    if (path === null) {
+      User.findUserByUsername(username)
+      .then(function(user) {
+        if (user === null) {
+          throw new Error('The user not found.');
+        }
+        result.pageUser = user;
+        return Page.findListByCreator(user, queryOptions, req.user);
+      }).then(function(pages) {
+        if (pages.length > limit) {
+          pages.pop();
+        }
+        pagerOptions.length = pages.length;
+
+        result.pages = pages;
+        result.searchPath = '/';
+        return res.json(ApiResponse.success(result));
+      }).catch(function(err) {
+        return res.json(ApiResponse.error(err));
+      });
+    } else {
+      Page.findListByStartWith(path, req.user, queryOptions)
+      .then(function(pageList) {
+        if (pageList.length > limit) {
+          pageList.pop();
+        }
+        pagerOptions.length = pageList.length;
+
+        result.pages = pageList;
+        result.searchPath = /\/$/.test(path) ? path : path + '/';
+        return res.json(ApiResponse.success(result));
+      }).catch(function(err) {
+        return res.json(ApiResponse.error(err));
+      });
+    }
+  };
+
   /**
    * @api {post} /pages.create Create new page
    * @apiName CreatePage