Răsfoiți Sursa

add mongoose paginate v2

zahmis 5 ani în urmă
părinte
comite
2a919e220c

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

@@ -13,12 +13,15 @@ class PageAttachment extends React.Component {
   constructor(props) {
     super(props);
 
+    const { appContainer } = this.props;
     this.state = {
       attachments: [],
       inUse: {},
       attachmentToDelete: null,
       deleting: false,
       deleteError: '',
+      limit: appContainer.getConfig().recentCreatedLimit,
+      offset: 0,
     };
 
     this.onAttachmentDeleteClicked = this.onAttachmentDeleteClicked.bind(this);
@@ -27,12 +30,13 @@ class PageAttachment extends React.Component {
 
   async componentDidMount() {
     const { pageId } = this.props.pageContainer.state;
+    const { limit, offset } = this.state;
 
     if (!pageId) {
       return;
     }
 
-    await this.props.appContainer.apiv3Get('/attachments/list', { pageId })
+    await this.props.appContainer.apiv3Get('/attachments/list', { pageId, limit, offset })
       .then((res) => {
         const attachments = res.data.attachments;
         const inUse = {};

+ 2 - 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}`;

+ 6 - 0
src/server/routes/apiv3/attachments.js

@@ -39,6 +39,9 @@ module.exports = (crowi) => {
    *              type: string
    */
   router.get('/list', accessTokenParser, loginRequired, async(req, res) => {
+    const limit = +req.query.limit || 30;
+    const offset = +req.query.offset || 0;
+    const queryOptions = { offset, limit };
 
     try {
       const pageId = req.query.pageId;
@@ -51,6 +54,9 @@ module.exports = (crowi) => {
       }
 
       const attachments = await Attachment.find({ page: pageId });
+      const pagination = await Page.paginate({}, { queryOptions });
+
+      console.log(pagination);
 
       return res.apiv3({ attachments });