text_modules_controller.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class TextModulesController < ApplicationController
  3. prepend_before_action :authentication_check
  4. =begin
  5. Format:
  6. JSON
  7. Example:
  8. {
  9. "id":1,
  10. "name":"some text_module",
  11. "user_id": null,
  12. "keywords":"some keywords",
  13. "content":"some content",
  14. "active":true,
  15. "updated_at":"2012-09-14T17:51:53Z",
  16. "created_at":"2012-09-14T17:51:53Z",
  17. "updated_by_id":2.
  18. "created_by_id":2,
  19. }
  20. =end
  21. =begin
  22. Resource:
  23. GET /api/v1/text_modules.json
  24. Response:
  25. [
  26. {
  27. "id": 1,
  28. "name": "some_name1",
  29. ...
  30. },
  31. {
  32. "id": 2,
  33. "name": "some_name2",
  34. ...
  35. }
  36. ]
  37. Test:
  38. curl http://localhost/api/v1/text_modules.json -v -u #{login}:#{password}
  39. =end
  40. def index
  41. permission_check(['admin.text_module', 'ticket.agent'])
  42. model_index_render(TextModule, params)
  43. end
  44. =begin
  45. Resource:
  46. GET /api/v1/text_modules/#{id}.json
  47. Response:
  48. {
  49. "id": 1,
  50. "name": "name_1",
  51. ...
  52. }
  53. Test:
  54. curl http://localhost/api/v1/text_modules/#{id}.json -v -u #{login}:#{password}
  55. =end
  56. def show
  57. permission_check(['admin.text_module', 'ticket.agent'])
  58. model_show_render(TextModule, params)
  59. end
  60. =begin
  61. Resource:
  62. POST /api/v1/text_modules.json
  63. Payload:
  64. {
  65. "name": "some name",
  66. "keywords":"some keywords",
  67. "content":"some content",
  68. "active":true,
  69. }
  70. Response:
  71. {
  72. "id": 1,
  73. "name": "some_name",
  74. ...
  75. }
  76. Test:
  77. curl http://localhost/api/v1/text_modules.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X POST -d '{"name": "some_name","active": true, "note": "some note"}'
  78. =end
  79. def create
  80. permission_check('admin.text_module')
  81. model_create_render(TextModule, params)
  82. end
  83. =begin
  84. Resource:
  85. PUT /api/v1/text_modules/{id}.json
  86. Payload:
  87. {
  88. "name": "some name",
  89. "keywords":"some keywords",
  90. "content":"some content",
  91. "active":true,
  92. }
  93. Response:
  94. {
  95. "id": 1,
  96. "name": "some_name",
  97. ...
  98. }
  99. Test:
  100. curl http://localhost/api/v1/text_modules.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X PUT -d '{"name": "some_name","active": true, "note": "some note"}'
  101. =end
  102. def update
  103. permission_check('admin.text_module')
  104. model_update_render(TextModule, params)
  105. end
  106. =begin
  107. Resource:
  108. DELETE /api/v1/text_modules/{id}.json
  109. Response:
  110. {}
  111. Test:
  112. curl http://localhost/api/v1/text_modules.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X DELETE
  113. =end
  114. def destroy
  115. permission_check('admin.text_module')
  116. model_destroy_render(TextModule, params)
  117. end
  118. # @path [GET] /text_modules/import_example
  119. #
  120. # @summary Download of example CSV file.
  121. # @notes The requester have 'admin.text_module' permissions to be able to download it.
  122. # @example curl -u 'me@example.com:test' http://localhost:3000/api/v1/text_modules/import_example
  123. #
  124. # @response_message 200 File download.
  125. # @response_message 401 Invalid session.
  126. def import_example
  127. permission_check('admin.text_module')
  128. csv_string = TextModule.csv_example(
  129. col_sep: params[:col_sep] || ',',
  130. )
  131. send_data(
  132. csv_string,
  133. filename: 'text_module-example.csv',
  134. type: 'text/csv',
  135. disposition: 'attachment'
  136. )
  137. end
  138. # @path [POST] /text_modules/import
  139. #
  140. # @summary Starts import.
  141. # @notes The requester have 'admin.text_module' permissions to be create a new import.
  142. # @example curl -u 'me@example.com:test' -F 'file=@/path/to/file/Textbausteine_final2.csv' 'https://your.zammad/api/v1/text_modules/import?try=true'
  143. # @example curl -u 'me@example.com:test' -F 'file=@/path/to/file/Textbausteine_final2.csv' 'https://your.zammad/api/v1/text_modules/import'
  144. #
  145. # @response_message 201 Import started.
  146. # @response_message 401 Invalid session.
  147. def import_start
  148. permission_check('admin.text_module')
  149. string = params[:data]
  150. if string.blank? && params[:file].present?
  151. string = params[:file].read.force_encoding('utf-8')
  152. end
  153. raise Exceptions::UnprocessableEntity, 'No source data submitted!' if string.blank?
  154. result = TextModule.csv_import(
  155. string: string,
  156. parse_params: {
  157. col_sep: params[:col_sep] || ',',
  158. },
  159. try: params[:try],
  160. delete: params[:delete],
  161. )
  162. render json: result, status: :ok
  163. end
  164. end