itizawa 6 лет назад
Родитель
Сommit
abda65be79
2 измененных файлов с 37 добавлено и 54 удалено
  1. 12 7
      src/client/js/components/BookmarkButton.jsx
  2. 25 47
      src/server/routes/apiv3/bookmarks.js

+ 12 - 7
src/client/js/components/BookmarkButton.jsx

@@ -15,19 +15,24 @@ class BookmarkButton extends React.Component {
     this.handleClick = this.handleClick.bind(this);
   }
 
-  componentDidMount() {
+  async componentDidMount() {
+    const { pageId, crowi } = this.props;
     // if guest user
     if (!this.isUserLoggedIn()) {
       // do nothing
       return;
     }
 
-    this.props.crowi.apiGet('/bookmarks.get', { page_id: this.props.pageId })
-      .then((res) => {
-        if (res.bookmark) {
-          this.setState({ isBookmarked: true });
-        }
-      });
+    try {
+      const response = await crowi.apiv3.get('/bookmarks', { pageId });
+      if (response.data.bookmark != null) {
+        this.setState({ isBookmarked: true });
+      }
+    }
+    catch (err) {
+      toastError(err);
+    }
+
   }
 
   async handleClick() {

+ 25 - 47
src/server/routes/apiv3/bookmarks.js

@@ -66,65 +66,43 @@ module.exports = (crowi) => {
       body('bool').isBoolean(),
     ],
   };
+
   /**
    * @swagger
    *
-   *    /bookmarks.get:
+   *    /bookmarks:
    *      get:
-   *        tags: [Bookmarks, CrowiCompatibles]
-   *        operationId: getBookmark
-   *        summary: /bookmarks.get
-   *        description: Get bookmark of the page with the user
+   *        tags: [Bookmarks]
+   *        summary: /bookmarks
+   *        description: Get bookmarked status
+   *        operationId: getBookmarkedStatus
    *        parameters:
-   *          - in: query
-   *            name: page_id
-   *            required: true
+   *          - name: pageId
+   *            in: query
+   *            description: page id
    *            schema:
-   *              $ref: '#/components/schemas/Page/properties/_id'
+   *              type: string
    *        responses:
    *          200:
-   *            description: Succeeded to get bookmark of the page with the user.
+   *            description: Succeeded to get bookmarked status.
    *            content:
    *              application/json:
    *                schema:
-   *                  properties:
-   *                    ok:
-   *                      $ref: '#/components/schemas/V1Response/properties/ok'
-   *                    bookmark:
-   *                      $ref: '#/components/schemas/Bookmark'
-   *          403:
-   *            $ref: '#/components/responses/403'
-   *          500:
-   *            $ref: '#/components/responses/500'
+   *                  $ref: '#/components/schemas/Bookmark'
    */
-  // actions.api.get = function(req, res) {
-  //   const pageId = req.query.page_id;
-
-  //   Bookmark.findByPageIdAndUserId(pageId, req.user)
-  //     .then((data) => {
-  //       debug('bookmark found', pageId, data);
-  //       const result = {};
-
-  //       result.bookmark = data;
-  //       return res.json(ApiResponse.success(result));
-  //     })
-  //     .catch((err) => {
-  //       return res.json(ApiResponse.error(err));
-  //     });
-  // };
-
-  // actions.api.list = function(req, res) {
-  //   const paginateOptions = ApiPaginate.parseOptions(req.query);
-
-  //   const options = Object.assign(paginateOptions, { populatePage: true });
-  //   Bookmark.findByUserId(req.user._id, options)
-  //     .then((result) => {
-  //       return res.json(ApiResponse.success(result));
-  //     })
-  //     .catch((err) => {
-  //       return res.json(ApiResponse.error(err));
-  //     });
-  // };
+  router.get('/', accessTokenParser, loginRequired, async(req, res) => {
+    const { pageId } = req.query;
+
+    try {
+      const bookmark = await Bookmark.findByPageIdAndUserId(pageId, req.user);
+      return res.apiv3({ bookmark });
+    }
+    catch (err) {
+      logger.error('get-bookmark-failed', err);
+      return res.apiv3Err(err, 500);
+    }
+  });
+
 
   /**
    * @swagger