mizozobu 6 лет назад
Родитель
Сommit
85ad6ecd92
2 измененных файлов с 15 добавлено и 9 удалено
  1. 2 0
      src/server/models/page.js
  2. 13 9
      src/server/routes/apiv3/user-group.js

+ 2 - 0
src/server/models/page.js

@@ -6,6 +6,7 @@
 const debug = require('debug')('growi:models:page');
 const nodePath = require('path');
 const mongoose = require('mongoose');
+const mongoosePaginate = require('mongoose-paginate');
 const uniqueValidator = require('mongoose-unique-validator');
 
 const ObjectId = mongoose.Schema.Types.ObjectId;
@@ -64,6 +65,7 @@ const pageSchema = new mongoose.Schema({
   toObject: { getters: true },
 });
 // apply plugins
+pageSchema.plugin(mongoosePaginate);
 pageSchema.plugin(uniqueValidator);
 
 

+ 13 - 9
src/server/routes/apiv3/user-group.js

@@ -564,18 +564,22 @@ module.exports = (crowi) => {
     const { limit, offset } = req.query;
 
     try {
-      const [pages, total] = await Promise.all([
-        Page
-          .find({ grant: Page.GRANT_USER_GROUP, grantedGroup: { $in: [id] } }, null, { skip: offset, limit })
-          .populate('lastUpdateUser', User.USER_PUBLIC_FIELDS)
-          .exec(),
-        Page.countDocuments({ grant: Page.GRANT_USER_GROUP, grantedGroup: { $in: [id] } }),
-      ]);
+      const { docs, total } = await Page.paginate({
+        grant: Page.GRANT_USER_GROUP,
+        grantedGroup: { $in: [id] },
+      }, {
+        offset,
+        limit,
+        populate: {
+          path: 'lastUpdateUser',
+          select: User.USER_PUBLIC_FIELDS,
+        },
+      });
 
       const current = offset / limit + 1;
 
-      // TODO: create a moudle for paginated response
-      return res.apiv3({ total, current, pages });
+      // TODO: create a common moudule for paginated response
+      return res.apiv3({ total, current, pages: docs });
     }
     catch (err) {
       const msg = `Error occurred in fetching pages for group: ${id}`;