bookmark.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /**
  2. * @swagger
  3. * tags:
  4. * name: Bookmarks
  5. */
  6. /**
  7. * @swagger
  8. *
  9. * components:
  10. * schemas:
  11. * Bookmark:
  12. * description: Bookmark
  13. * type: object
  14. * properties:
  15. * _id:
  16. * type: string
  17. * description: page ID
  18. * example: 5e07345972560e001761fa63
  19. * __v:
  20. * type: number
  21. * description: DB record version
  22. * example: 0
  23. * createdAt:
  24. * type: string
  25. * description: date created at
  26. * example: 2010-01-01T00:00:00.000Z
  27. * page:
  28. * $ref: '#/components/schemas/Page/properties/_id'
  29. * user:
  30. * $ref: '#/components/schemas/User/properties/_id'
  31. */
  32. module.exports = function(crowi, app) {
  33. const debug = require('debug')('growi:routes:bookmark');
  34. const Bookmark = crowi.model('Bookmark');
  35. const Page = crowi.model('Page');
  36. const ApiResponse = require('../util/apiResponse');
  37. const ApiPaginate = require('../util/apiPaginate');
  38. const actions = {};
  39. actions.api = {};
  40. /**
  41. * @swagger
  42. *
  43. * /bookmarks.get:
  44. * get:
  45. * tags: [Bookmarks, CrowiCompatibles]
  46. * operationId: getBookmark
  47. * summary: /bookmarks.get
  48. * description: Get bookmark of the page with the user
  49. * parameters:
  50. * - in: query
  51. * name: page_id
  52. * required: true
  53. * schema:
  54. * $ref: '#/components/schemas/Page/properties/_id'
  55. * responses:
  56. * 200:
  57. * description: Succeeded to get bookmark of the page with the user.
  58. * content:
  59. * application/json:
  60. * schema:
  61. * properties:
  62. * ok:
  63. * $ref: '#/components/schemas/V1Response/properties/ok'
  64. * bookmark:
  65. * $ref: '#/components/schemas/Bookmark'
  66. * 403:
  67. * $ref: '#/components/responses/403'
  68. * 500:
  69. * $ref: '#/components/responses/500'
  70. */
  71. /**
  72. * @api {get} /bookmarks.get Get bookmark of the page with the user
  73. * @apiName GetBookmarks
  74. * @apiGroup Bookmark
  75. *
  76. * @apiParam {String} page_id Page Id.
  77. */
  78. actions.api.get = function(req, res) {
  79. const pageId = req.query.page_id;
  80. Bookmark.findByPageIdAndUserId(pageId, req.user)
  81. .then((data) => {
  82. debug('bookmark found', pageId, data);
  83. const result = {};
  84. result.bookmark = data;
  85. return res.json(ApiResponse.success(result));
  86. })
  87. .catch((err) => {
  88. return res.json(ApiResponse.error(err));
  89. });
  90. };
  91. actions.api.list = function(req, res) {
  92. const paginateOptions = ApiPaginate.parseOptions(req.query);
  93. const options = Object.assign(paginateOptions, { populatePage: true });
  94. Bookmark.findByUserId(req.user._id, options)
  95. .then((result) => {
  96. return res.json(ApiResponse.success(result));
  97. })
  98. .catch((err) => {
  99. return res.json(ApiResponse.error(err));
  100. });
  101. };
  102. /**
  103. * @swagger
  104. *
  105. * /bookmarks.add:
  106. * post:
  107. * tags: [Bookmarks, CrowiCompatibles]
  108. * operationId: addBookmark
  109. * summary: /bookmarks.add
  110. * description: Add bookmark of the page
  111. * parameters:
  112. * - in: query
  113. * name: page_id
  114. * schema:
  115. * $ref: '#/components/schemas/Page/properties/_id'
  116. * required: true
  117. * responses:
  118. * 200:
  119. * description: Succeeded to add bookmark of the page.
  120. * content:
  121. * application/json:
  122. * schema:
  123. * properties:
  124. * ok:
  125. * $ref: '#/components/schemas/V1Response/properties/ok'
  126. * bookmark:
  127. * $ref: '#/components/schemas/Bookmark'
  128. * 403:
  129. * $ref: '#/components/responses/403'
  130. * 500:
  131. * $ref: '#/components/responses/500'
  132. */
  133. /**
  134. * @api {post} /bookmarks.add Add bookmark of the page
  135. * @apiName AddBookmark
  136. * @apiGroup Bookmark
  137. *
  138. * @apiParam {String} page_id Page Id.
  139. */
  140. actions.api.add = async function(req, res) {
  141. const pageId = req.body.page_id;
  142. const page = await Page.findByIdAndViewer(pageId, req.user);
  143. if (page == null) {
  144. return res.json(ApiResponse.success({ bookmark: null }));
  145. }
  146. const bookmark = await Bookmark.add(page, req.user);
  147. bookmark.depopulate('page');
  148. bookmark.depopulate('user');
  149. const result = { bookmark };
  150. return res.json(ApiResponse.success(result));
  151. };
  152. /**
  153. * @swagger
  154. *
  155. * /bookmarks.remove:
  156. * post:
  157. * tags: [Bookmarks, CrowiCompatibles]
  158. * operationId: removeBookmark
  159. * summary: /bookmarks.remove
  160. * description: Remove bookmark of the page
  161. * requestBody:
  162. * content:
  163. * application/json:
  164. * schema:
  165. * properties:
  166. * page_id:
  167. * $ref: '#/components/schemas/Page/properties/_id'
  168. * required:
  169. * - page_id
  170. * responses:
  171. * 200:
  172. * description: Succeeded to remove bookmark of the page.
  173. * content:
  174. * application/json:
  175. * schema:
  176. * properties:
  177. * ok:
  178. * $ref: '#/components/schemas/V1Response/properties/ok'
  179. * 403:
  180. * $ref: '#/components/responses/403'
  181. * 500:
  182. * $ref: '#/components/responses/500'
  183. */
  184. /**
  185. * @api {post} /bookmarks.remove Remove bookmark of the page
  186. * @apiName RemoveBookmark
  187. * @apiGroup Bookmark
  188. *
  189. * @apiParam {String} page_id Page Id.
  190. */
  191. actions.api.remove = function(req, res) {
  192. const pageId = req.body.page_id;
  193. Bookmark.removeBookmark(pageId, req.user)
  194. .then((data) => {
  195. debug('Bookmark removed.', data); // if the bookmark is not exists, this 'data' is null
  196. return res.json(ApiResponse.success());
  197. })
  198. .catch((err) => {
  199. return res.json(ApiResponse.error(err));
  200. });
  201. };
  202. return actions;
  203. };