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

Merge branch 'feat/transplant-tabs-to-modal-for-master-merge' into imprv/gw3400-add-animation-for-activeTab

kaoritokashiki 5 лет назад
Родитель
Сommit
856fd9032c

+ 14 - 13
src/client/js/components/PageAttachment.jsx

@@ -25,27 +25,28 @@ class PageAttachment extends React.Component {
     this.onAttachmentDeleteClickedConfirm = this.onAttachmentDeleteClickedConfirm.bind(this);
     this.onAttachmentDeleteClickedConfirm = this.onAttachmentDeleteClickedConfirm.bind(this);
   }
   }
 
 
-  componentDidMount() {
+  async componentDidMount() {
     const { pageId } = this.props.pageContainer.state;
     const { pageId } = this.props.pageContainer.state;
 
 
     if (!pageId) {
     if (!pageId) {
       return;
       return;
     }
     }
 
 
-    this.props.appContainer.apiGet('/attachments.list', { page_id: pageId })
-      .then((res) => {
-        const attachments = res.attachments;
-        const inUse = {};
+    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) {
-          inUse[attachment._id] = this.checkIfFileInUse(attachment);
-        }
+    for (const attachment of attachments) {
+      inUse[attachment._id] = this.checkIfFileInUse(attachment);
+    }
 
 
-        this.setState({
-          attachments,
-          inUse,
-        });
-      });
+    this.setState({
+      attachments,
+      inUse,
+    });
   }
   }
 
 
   checkIfFileInUse(attachment) {
   checkIfFileInUse(attachment) {

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

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

+ 11 - 4
src/server/routes/apiv3/attachment.js

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

+ 0 - 58
src/server/routes/attachment.js

@@ -127,7 +127,6 @@ const ApiResponse = require('../util/apiResponse');
 
 
 module.exports = function(crowi, app) {
 module.exports = function(crowi, app) {
   const Attachment = crowi.model('Attachment');
   const Attachment = crowi.model('Attachment');
-  const User = crowi.model('User');
   const Page = crowi.model('Page');
   const Page = crowi.model('Page');
   const { fileUploadService, attachmentService } = crowi;
   const { fileUploadService, attachmentService } = crowi;
 
 
@@ -295,63 +294,6 @@ module.exports = function(crowi, app) {
     return responseForAttachment(req, res, attachment);
     return responseForAttachment(req, res, attachment);
   };
   };
 
 
-  /**
-   * @swagger
-   *
-   *    /attachments.list:
-   *      get:
-   *        tags: [Attachments, CrowiCompatibles]
-   *        operationId: listAttachments
-   *        summary: /attachments.list
-   *        description: Get list of attachments in page
-   *        parameters:
-   *          - in: query
-   *            name: page_id
-   *            schema:
-   *              $ref: '#/components/schemas/Page/properties/_id'
-   *            required: true
-   *        responses:
-   *          200:
-   *            description: Succeeded to get list of attachments.
-   *            content:
-   *              application/json:
-   *                schema:
-   *                  properties:
-   *                    ok:
-   *                      $ref: '#/components/schemas/V1Response/properties/ok'
-   *                    attachments:
-   *                      type: array
-   *                      items:
-   *                        $ref: '#/components/schemas/Attachment'
-   *                      description: attachment list
-   *          403:
-   *            $ref: '#/components/responses/403'
-   *          500:
-   *            $ref: '#/components/responses/500'
-   */
-  /**
-   * @api {get} /attachments.list Get attachments of the page
-   * @apiName ListAttachments
-   * @apiGroup Attachment
-   *
-   * @apiParam {String} page_id
-   */
-  api.list = async function(req, res) {
-    const id = req.query.page_id || null;
-    if (!id) {
-      return res.json(ApiResponse.error('Parameters page_id is required.'));
-    }
-
-    let attachments = await Attachment.find({ page: id })
-      .sort({ updatedAt: 1 })
-      .populate({ path: 'creator', select: User.USER_PUBLIC_FIELDS });
-
-    attachments = attachments.map((attachment) => {
-      return attachment.toObject({ virtuals: true });
-    });
-
-    return res.json(ApiResponse.success({ attachments }));
-  };
 
 
   /**
   /**
    * @swagger
    * @swagger

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

@@ -156,7 +156,6 @@ module.exports = function(crowi, app) {
   app.post('/_api/comments.add'       , comment.api.validators.add(), accessTokenParser , loginRequiredStrictly , csrf, comment.api.add);
   app.post('/_api/comments.add'       , comment.api.validators.add(), accessTokenParser , loginRequiredStrictly , csrf, comment.api.add);
   app.post('/_api/comments.update'    , comment.api.validators.add(), accessTokenParser , loginRequiredStrictly , csrf, comment.api.update);
   app.post('/_api/comments.update'    , comment.api.validators.add(), accessTokenParser , loginRequiredStrictly , csrf, comment.api.update);
   app.post('/_api/comments.remove'    , accessTokenParser , loginRequiredStrictly , csrf, comment.api.remove);
   app.post('/_api/comments.remove'    , accessTokenParser , loginRequiredStrictly , csrf, comment.api.remove);
-  app.get('/_api/attachments.list'    , accessTokenParser , loginRequired , attachment.api.list);
   app.post('/_api/attachments.add'                  , uploads.single('file'), autoReap, accessTokenParser, loginRequiredStrictly ,csrf, attachment.api.add);
   app.post('/_api/attachments.add'                  , uploads.single('file'), autoReap, accessTokenParser, loginRequiredStrictly ,csrf, attachment.api.add);
   app.post('/_api/attachments.uploadProfileImage'   , uploads.single('file'), autoReap, accessTokenParser, loginRequiredStrictly ,csrf, attachment.api.uploadProfileImage);
   app.post('/_api/attachments.uploadProfileImage'   , uploads.single('file'), autoReap, accessTokenParser, loginRequiredStrictly ,csrf, attachment.api.uploadProfileImage);
   app.post('/_api/attachments.remove'               , accessTokenParser , loginRequiredStrictly , csrf, attachment.api.remove);
   app.post('/_api/attachments.remove'               , accessTokenParser , loginRequiredStrictly , csrf, attachment.api.remove);