itizawa 6 лет назад
Родитель
Сommit
9f52ff5769
2 измененных файлов с 129 добавлено и 5 удалено
  1. 1 1
      src/client/js/components/LikeButton.jsx
  2. 128 4
      src/server/routes/apiv3/page.js

+ 1 - 1
src/client/js/components/LikeButton.jsx

@@ -21,8 +21,8 @@ class LikeButton extends React.Component {
     const { appContainer, pageId } = this.props;
     const { appContainer, pageId } = this.props;
     const { isLiked } = this.state;
     const { isLiked } = this.state;
     try {
     try {
-      await appContainer.apiv3.put('/page/likes', { pageId, isLiked });
       this.setState({ isLiked: !isLiked });
       this.setState({ isLiked: !isLiked });
+      await appContainer.apiv3.put('/page/likes', { pageId, isLiked });
     }
     }
     catch (err) {
     catch (err) {
       toastError(err);
       toastError(err);

+ 128 - 4
src/server/routes/apiv3/page.js

@@ -3,7 +3,7 @@ const loggerFactory = require('@alias/logger');
 const logger = loggerFactory('growi:routes:apiv3:page'); // eslint-disable-line no-unused-vars
 const logger = loggerFactory('growi:routes:apiv3:page'); // eslint-disable-line no-unused-vars
 
 
 const express = require('express');
 const express = require('express');
-// const { body } = require('express-validator');
+const { body } = require('express-validator');
 
 
 const router = express.Router();
 const router = express.Router();
 
 
@@ -14,6 +14,101 @@ const router = express.Router();
  *  tags:
  *  tags:
  *    name: Page
  *    name: Page
  */
  */
+
+/**
+ * @swagger
+ *
+ *  components:
+ *    schemas:
+ *      Page:
+ *        description: Page
+ *        type: object
+ *        properties:
+ *          _id:
+ *            type: string
+ *            description: page ID
+ *            example: 5e07345972560e001761fa63
+ *          __v:
+ *            type: number
+ *            description: DB record version
+ *            example: 0
+ *          commentCount:
+ *            type: number
+ *            description: count of comments
+ *            example: 3
+ *          createdAt:
+ *            type: string
+ *            description: date created at
+ *            example: 2010-01-01T00:00:00.000Z
+ *          creator:
+ *            $ref: '#/components/schemas/User'
+ *          extended:
+ *            type: object
+ *            description: extend data
+ *            example: {}
+ *          grant:
+ *            type: number
+ *            description: grant
+ *            example: 1
+ *          grantedUsers:
+ *            type: array
+ *            description: granted users
+ *            items:
+ *              type: string
+ *              description: user ID
+ *            example: ["5ae5fccfc5577b0004dbd8ab"]
+ *          lastUpdateUser:
+ *            $ref: '#/components/schemas/User'
+ *          liker:
+ *            type: array
+ *            description: granted users
+ *            items:
+ *              type: string
+ *              description: user ID
+ *            example: []
+ *          path:
+ *            type: string
+ *            description: page path
+ *            example: /
+ *          redirectTo:
+ *            type: string
+ *            description: redirect path
+ *            example: ""
+ *          revision:
+ *            type: string
+ *            description: page revision
+ *          seenUsers:
+ *            type: array
+ *            description: granted users
+ *            items:
+ *              type: string
+ *              description: user ID
+ *            example: ["5ae5fccfc5577b0004dbd8ab"]
+ *          status:
+ *            type: string
+ *            description: status
+ *            enum:
+ *              - 'wip'
+ *              - 'published'
+ *              - 'deleted'
+ *              - 'deprecated'
+ *            example: published
+ *          updatedAt:
+ *            type: string
+ *            description: date updated at
+ *            example: 2010-01-01T00:00:00.000Z
+ *
+ *      LikeParams:
+ *        description: LikeParams
+ *        type: object
+ *        properties:
+ *          pageId:
+ *            type: string
+ *            description: page ID
+ *            example: 5e07345972560e001761fa63
+ *          isLiked:
+ *            type: boolean
+ */
 module.exports = (crowi) => {
 module.exports = (crowi) => {
   const accessTokenParser = require('../../middleware/access-token-parser')(crowi);
   const accessTokenParser = require('../../middleware/access-token-parser')(crowi);
   const loginRequired = require('../../middleware/login-required')(crowi);
   const loginRequired = require('../../middleware/login-required')(crowi);
@@ -21,10 +116,39 @@ module.exports = (crowi) => {
 
 
   const globalNotificationService = crowi.getGlobalNotificationService();
   const globalNotificationService = crowi.getGlobalNotificationService();
   const { Page, GlobalNotificationSetting } = crowi.models;
   const { Page, GlobalNotificationSetting } = crowi.models;
-  // const { ApiV3FormValidator } = crowi.middlewares;
+  const { ApiV3FormValidator } = crowi.middlewares;
+
+
+  const validator = {
+    likes: [
+      body('pageId').isString(),
+      body('isLiked').isBoolean(),
+    ],
+  };
 
 
-  // TODO swagger
-  router.put('/likes', accessTokenParser, loginRequired, csrf, async(req, res) => {
+  /**
+   * @swagger
+   *
+   *    /page/likes:
+   *      put:
+   *        tags: [Page]
+   *        summary: /page/likes
+   *        description: Update liked status
+   *        operationId: updateLikedStatus
+   *        requestBody:
+   *          content:
+   *            application/json:
+   *              schema:
+   *                $ref: '#/components/schemas/LikeParams'
+   *        responses:
+   *          200:
+   *            description: Succeeded to update liked status.
+   *            content:
+   *              application/json:
+   *                schema:
+   *                  $ref: '#/components/schemas/Page'
+   */
+  router.put('/likes', accessTokenParser, loginRequired, csrf, validator.likes, ApiV3FormValidator, async(req, res) => {
     const { pageId, isLiked } = req.body;
     const { pageId, isLiked } = req.body;
 
 
     let page;
     let page;