itizawa 5 years ago
parent
commit
308a69cb94
1 changed files with 45 additions and 18 deletions
  1. 45 18
      src/server/routes/apiv3/page.js

+ 45 - 18
src/server/routes/apiv3/page.js

@@ -110,6 +110,17 @@ const ErrorV3 = require('../../models/vo/error-apiv3');
  *          bool:
  *            type: boolean
  *            description: boolean for like status
+ *
+ *      LikeInfo:
+ *        description: LikeInfo
+ *        type: object
+ *        properties:
+ *          sumOfLikers:
+ *            type: number
+ *            description: how many people liked the page
+ *          isLiked:
+ *            type: boolean
+ *            description: Whether the request user liked (will be returned if the user is included in the request)
  */
 module.exports = (crowi) => {
   const accessTokenParser = require('../../middlewares/access-token-parser')(crowi);
@@ -119,7 +130,7 @@ module.exports = (crowi) => {
   const apiV3FormValidator = require('../../middlewares/apiv3-form-validator')(crowi);
 
   const globalNotificationService = crowi.getGlobalNotificationService();
-  const { Page, GlobalNotificationSetting, User } = crowi.models;
+  const { Page, GlobalNotificationSetting } = crowi.models;
   const { exportService } = crowi;
 
   const validator = {
@@ -201,32 +212,48 @@ module.exports = (crowi) => {
     return res.apiv3({ result });
   });
 
+  /**
+   * @swagger
+   *
+   *    /page/like-info:
+   *      get:
+   *        tags: [Page]
+   *        summary: /page/like-info
+   *        description: Get like info
+   *        operationId: getLikeInfo
+   *        parameters:
+   *          - name: _id
+   *            in: query
+   *            description: page id
+   *            schema:
+   *              type: string
+   *        responses:
+   *          200:
+   *            description: Succeeded to get bookmark info.
+   *            content:
+   *              application/json:
+   *                schema:
+   *                  $ref: '#/components/schemas/LikeInfo'
+   */
   router.get('/like-info', loginRequired, validator.likeInfo, apiV3FormValidator, async(req, res) => {
     const pageId = req.query._id;
-    let users;
-    let sumOfLikers;
-    let page;
-    try {
-      page = await Page.findById(pageId);
-      users = await Page.findById(pageId).populate('liker', User.USER_PUBLIC_FIELDS);
-      sumOfLikers = page.liker.length;
-    }
-    catch (err) {
-      logger.error('error like info ', err);
-      return res.apiv3Err(err, 500);
-    }
+
+    const responsesParams = {};
+
     try {
+      const page = await Page.findById(pageId);
+      responsesParams.sumOfLikers = page.liker.length;
+
       // guest user return nothing
       if (!req.user) {
-        return res.apiv3({ users, sumOfLikers });
+        return res.apiv3(responsesParams);
       }
-      const userId = req.user._id;
-      const isLiked = page.liker.includes(userId);
 
-      return res.apiv3({ users, sumOfLikers, isLiked });
+      responsesParams.isLiked = page.liker.includes(req.user._id);
+      return res.apiv3(responsesParams);
     }
     catch (err) {
-      logger.error('error like info', err);
+      logger.error('get-like-count-failed', err);
       return res.apiv3Err(err, 500);
     }
   });