online_notifications_controller.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class OnlineNotificationsController < ApplicationController
  3. prepend_before_action -> { authorize! }, only: %i[show update destroy]
  4. prepend_before_action :authentication_check
  5. =begin
  6. Format:
  7. JSON
  8. Example:
  9. {
  10. "id":1,
  11. "name":"some template",
  12. "user_id": null,
  13. "options":{"a":1,"b":2},
  14. "updated_at":"2012-09-14T17:51:53Z",
  15. "created_at":"2012-09-14T17:51:53Z",
  16. "updated_by_id":2.
  17. "created_by_id":2,
  18. }
  19. =end
  20. =begin
  21. Resource:
  22. GET /api/v1/templates.json
  23. Response:
  24. [
  25. {
  26. "id": 1,
  27. "name": "some_name1",
  28. ...
  29. },
  30. {
  31. "id": 2,
  32. "name": "some_name2",
  33. ...
  34. }
  35. ]
  36. Test:
  37. curl http://localhost/api/v1/online_notifications.json -v -u #{login}:#{password}
  38. =end
  39. def index
  40. online_notifications = OnlineNotification.list(current_user, 200)
  41. if response_expand?
  42. list = []
  43. online_notifications.each do |item|
  44. list.push item.attributes_with_association_names
  45. end
  46. render json: list, status: :ok
  47. return
  48. end
  49. if response_full?
  50. assets = {}
  51. item_ids = []
  52. online_notifications.each do |item|
  53. item_ids.push item.id
  54. assets = item.assets(assets)
  55. end
  56. render json: {
  57. record_ids: item_ids,
  58. assets: assets,
  59. }, status: :ok
  60. return
  61. end
  62. all = []
  63. online_notifications.each do |item|
  64. all.push item.attributes_with_association_ids
  65. end
  66. render json: all, status: :ok
  67. end
  68. =begin
  69. Resource:
  70. GET /api/v1/online_notifications/{id}
  71. Payload:
  72. {
  73. "id": "123",
  74. }
  75. Response:
  76. {
  77. "id": 1,
  78. "name": "some_name",
  79. ...
  80. }
  81. Test:
  82. curl http://localhost/api/v1/online_notifications/#{id} -v -u #{login}:#{password}
  83. =end
  84. def show
  85. model_show_render(OnlineNotification, params)
  86. end
  87. =begin
  88. Resource:
  89. PUT /api/v1/online_notifications/{id}
  90. Payload:
  91. {
  92. "name": "some name",
  93. "options":{"a":1,"b":2},
  94. }
  95. Response:
  96. {
  97. "id": 1,
  98. "name": "some_name",
  99. ...
  100. }
  101. Test:
  102. curl http://localhost/api/v1/online_notifications -v -u #{login}:#{password} -H "Content-Type: application/json" -X PUT -d '{"name": "some_name","active": true, "note": "some note"}'
  103. =end
  104. def update
  105. model_update_render(OnlineNotification, params)
  106. end
  107. =begin
  108. Resource:
  109. DELETE /api/v1/online_notifications/{id}.json
  110. Response:
  111. {}
  112. Test:
  113. curl http://localhost/api/v1/online_notifications/{id}.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X DELETE
  114. =end
  115. def destroy
  116. model_destroy_render(OnlineNotification, params)
  117. end
  118. =begin
  119. Resource:
  120. PUT /api/v1/online_notifications/mark_all_as_read
  121. Payload:
  122. {}
  123. Response:
  124. {}
  125. Test:
  126. curl http://localhost/api/v1/online_notifications/mark_all_as_read -v -u #{login}:#{password} -X POST -d '{}'
  127. =end
  128. def mark_all_as_read
  129. notifications = OnlineNotification.list(current_user, 200)
  130. notifications.each do |notification|
  131. next if notification['seen']
  132. OnlineNotification.find(notification['id']).update!(seen: true)
  133. end
  134. render json: {}, status: :ok
  135. end
  136. end