TsuyoshiSuzukief 7 лет назад
Родитель
Сommit
29c116ba1a

+ 3 - 1
src/client/js/components/RecentCreated/RecentCreated.js

@@ -16,13 +16,14 @@ export default class RecentCreated extends React.Component {
 
   componentWillMount() {
     this.getRecentCreatedList( );
+    console.log(this.state);
   }
   getRecentCreatedList() {
 
     const pageId = this.props.pageId;
     const userId = this.props.crowi.me;
 
-    this.props.crowi.apiGet('/pages.list', {page_id: pageId , user: userId , limit: 3 , offset: 3 , })
+    this.props.crowi.apiGet('/pages.recentCreated', {page_id: pageId , user: userId , limit: 3 , offset: 0 , })
       .then(res => {
         const pages = res.pages;
         let inUse = {};
@@ -38,6 +39,7 @@ export default class RecentCreated extends React.Component {
   render() {
     let active = 1;
     let items = [];
+    console.log(this.state);
     for (let number = 1; number <= 5; number++) {
       items.push(
         <Pagination.Item key={number} active={number === active}>{number}</Pagination.Item>

+ 8 - 1
src/server/models/page.js

@@ -696,7 +696,14 @@ module.exports = function(crowi) {
       .populate('revision')
       .exec()
       .then(function(pages) {
-        return Page.populate(pages, {path: 'lastUpdateUser', model: 'User', select: User.USER_PUBLIC_FIELDS}).then(resolve);
+        return Page.populate(pages, {path: 'lastUpdateUser', model: 'User', select: User.USER_PUBLIC_FIELDS}).then(function(data){
+          let totalCount = "";
+          totalCount = new Promise(function(rev,rej){
+            Page.find(conditions).count().populate('revision').exec().then(data);
+          });
+          data.totalCount = totalCount;
+          resolve(data);
+        });
       });
     });
   };

+ 1 - 0
src/server/routes/index.js

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

+ 47 - 1
src/server/routes/page.js

@@ -734,7 +734,7 @@ module.exports = function(crowi, app) {
   api.list = function(req, res) {
     const username = req.query.user || null;
     const path = req.query.path || null;
-    const limit = parseInt(req.query.limit) ;
+    const limit = + req.query.limit || 50;
     const offset = parseInt(req.query.offset) || 0;
 
     const pagerOptions = { offset: offset, limit: limit };
@@ -1262,5 +1262,51 @@ module.exports = function(crowi, app) {
     });
   };
 
+  api.recentCreated = function(req, res) {
+    const username = req.query.user || null;
+    const path = req.query.path || null;
+    const limit = + req.query.limit || 50;
+    const offset = + req.query.offset || 0;
+
+    const pagerOptions = { offset: offset, limit: limit };
+    const queryOptions = { offset: offset, limit: limit };
+
+    // Accepts only one of these
+    if (username === null && path === null) {
+      return res.json(ApiResponse.error('Parameter user or path is required.'));
+    }
+    if (username !== null && path !== null) {
+      return res.json(ApiResponse.error('Parameter user or path is required.'));
+    }
+
+    let pageFetcher;
+    if (path === null) {
+      pageFetcher = User.findUserByUsername(username)
+      .then(function(user) {
+        if (user === null) {
+          throw new Error('The user not found.');
+        }
+        return Page.findListByCreator(user, queryOptions, req.user);
+      });
+    }
+    else {
+      pageFetcher = Page.findListByStartWith(path, req.user, queryOptions);
+    }
+
+    pageFetcher
+    .then(function(pages) {
+      if (pages.length > limit) {
+        pages.pop();
+      }
+      pagerOptions.length = pages.length;
+
+      const result = {};
+      result.pages = pagePathUtils.encodePagesPath(pages);
+      return res.json(ApiResponse.success(result));
+    }).catch(function(err) {
+      return res.json(ApiResponse.error(err));
+    });
+  };
+
   return actions;
 };