Просмотр исходного кода

Merge pull request #2748 from weseek/add-pagination-in-endpoint-of-attachment

Add pagination in endpoint of attachment
Yuki Takei 5 лет назад
Родитель
Сommit
5d4cdc3c74

+ 5 - 2
src/client/js/components/PageAttachment.jsx

@@ -32,8 +32,11 @@ class PageAttachment extends React.Component {
       return;
     }
 
-    const res = await this.props.appContainer.apiv3Get('/attachment/list', { pageId });
-    const attachments = res.data.attachments;
+    const limit = 10;
+    // offset値は、dummy data この後のタスクで実装
+    const offset = 0;
+    const res = await this.props.appContainer.apiv3Get('/attachment/list', { pageId, limit, offset });
+    const attachments = res.data.paginateResult.docs;
     const inUse = {};
 
     for (const attachment of attachments) {

+ 3 - 0
src/server/models/attachment.js

@@ -7,6 +7,7 @@ const path = require('path');
 
 const mongoose = require('mongoose');
 const uniqueValidator = require('mongoose-unique-validator');
+const mongoosePaginate = require('mongoose-paginate-v2');
 
 const ObjectId = mongoose.Schema.Types.ObjectId;
 
@@ -29,6 +30,7 @@ module.exports = function(crowi) {
     createdAt: { type: Date, default: Date.now },
   });
   attachmentSchema.plugin(uniqueValidator);
+  attachmentSchema.plugin(mongoosePaginate);
 
   attachmentSchema.virtual('filePathProxied').get(function() {
     return `/attachment/${this._id}`;
@@ -63,5 +65,6 @@ module.exports = function(crowi) {
     return attachment;
   };
 
+
   return mongoose.model('Attachment', attachmentSchema);
 };

+ 8 - 3
src/server/routes/apiv3/attachment.js

@@ -39,10 +39,12 @@ module.exports = (crowi) => {
    *              type: string
    */
   router.get('/list', accessTokenParser, loginRequired, async(req, res) => {
+    const offset = +req.query.offset || 0;
+    const limit = +req.query.limit || 30;
+    const queryOptions = { offset, limit };
 
     try {
       const pageId = req.query.pageId;
-
       // check whether accessible
       const isAccessible = await Page.isAccessiblePageByViewer(pageId, req.user);
       if (!isAccessible) {
@@ -50,9 +52,12 @@ module.exports = (crowi) => {
         return res.apiv3Err(new ErrorV3(msg, 'attachment-list-failed'), 403);
       }
 
-      const attachments = await Attachment.find({ page: pageId });
+      const paginateResult = await Attachment.paginate(
+        { page: pageId },
+        queryOptions,
+      );
 
-      return res.apiv3({ attachments });
+      return res.apiv3({ paginateResult });
 
     }
     catch (err) {