bookmark.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. module.exports = function(crowi, app) {
  2. 'use strict';
  3. var debug = require('debug')('crowi: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. } else {
  48. return res.json(ApiResponse.success({bookmark: null}));
  49. }
  50. }).then(function(data) {
  51. var result = {};
  52. data.depopulate('page');
  53. data.depopulate('user');
  54. result.bookmark = data;
  55. return res.json(ApiResponse.success(result));
  56. }).catch(function(err) {
  57. return res.json(ApiResponse.error(err));
  58. });
  59. };
  60. /**
  61. * @api {post} /bookmarks.remove Remove bookmark of the page
  62. * @apiName RemoveBookmark
  63. * @apiGroup Bookmark
  64. *
  65. * @apiParam {String} page_id Page Id.
  66. */
  67. actions.api.remove = function(req, res){
  68. var pageId = req.body.page_id;
  69. Bookmark.removeBookmark(pageId, req.user)
  70. .then(function(data) {
  71. debug('Bookmark removed.', data); // if the bookmark is not exists, this 'data' is null
  72. return res.json(ApiResponse.success());
  73. }).catch(function(err) {
  74. return res.json(ApiResponse.error(err));
  75. });
  76. };
  77. return actions;
  78. };