bookmark.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. module.exports = function(crowi, app) {
  2. 'use strict';
  3. var debug = require('debug')('growi:routes:bookmark')
  4. , Bookmark = crowi.model('Bookmark')
  5. , Page = crowi.model('Page')
  6. , User = crowi.model('User')
  7. , Revision = crowi.model('Revision')
  8. , Bookmark = crowi.model('Bookmark')
  9. , ApiResponse = require('../util/apiResponse')
  10. , actions = {}
  11. ;
  12. actions.api = {};
  13. /**
  14. * @api {get} /bookmarks.get Get bookmark of the page with the user
  15. * @apiName GetBookmarks
  16. * @apiGroup Bookmark
  17. *
  18. * @apiParam {String} page_id Page Id.
  19. */
  20. actions.api.get = function(req, res) {
  21. var pageId = req.query.page_id;
  22. Bookmark.findByPageIdAndUserId(pageId, req.user)
  23. .then(function(data) {
  24. debug('bookmark found', pageId, data);
  25. var result = {};
  26. if (data) {
  27. }
  28. result.bookmark = data;
  29. return res.json(ApiResponse.success(result));
  30. }).catch(function(err) {
  31. return res.json(ApiResponse.error(err));
  32. });
  33. };
  34. /**
  35. * @api {post} /bookmarks.add Add bookmark of the page
  36. * @apiName AddBookmark
  37. * @apiGroup Bookmark
  38. *
  39. * @apiParam {String} page_id Page Id.
  40. */
  41. actions.api.add = function(req, res) {
  42. var pageId = req.body.page_id;
  43. Page.findPageByIdAndGrantedUser(pageId, req.user)
  44. .then(function(pageData) {
  45. if (pageData) {
  46. return Bookmark.add(pageData, req.user);
  47. }
  48. else {
  49. return res.json(ApiResponse.success({bookmark: null}));
  50. }
  51. }).then(function(data) {
  52. var result = {};
  53. data.depopulate('page');
  54. data.depopulate('user');
  55. result.bookmark = data;
  56. return res.json(ApiResponse.success(result));
  57. }).catch(function(err) {
  58. return res.json(ApiResponse.error(err));
  59. });
  60. };
  61. /**
  62. * @api {post} /bookmarks.remove Remove bookmark of the page
  63. * @apiName RemoveBookmark
  64. * @apiGroup Bookmark
  65. *
  66. * @apiParam {String} page_id Page Id.
  67. */
  68. actions.api.remove = function(req, res) {
  69. var pageId = req.body.page_id;
  70. Bookmark.removeBookmark(pageId, req.user)
  71. .then(function(data) {
  72. debug('Bookmark removed.', data); // if the bookmark is not exists, this 'data' is null
  73. return res.json(ApiResponse.success());
  74. }).catch(function(err) {
  75. return res.json(ApiResponse.error(err));
  76. });
  77. };
  78. return actions;
  79. };