text_modules_controller.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class TextModulesController < ApplicationController
  3. prepend_before_action :authenticate_and_authorize!
  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. model_index_render(TextModule, params)
  42. end
  43. =begin
  44. Resource:
  45. GET /api/v1/text_modules/#{id}.json
  46. Response:
  47. {
  48. "id": 1,
  49. "name": "name_1",
  50. ...
  51. }
  52. Test:
  53. curl http://localhost/api/v1/text_modules/#{id}.json -v -u #{login}:#{password}
  54. =end
  55. def show
  56. model_show_render(TextModule, params)
  57. end
  58. =begin
  59. Resource:
  60. POST /api/v1/text_modules.json
  61. Payload:
  62. {
  63. "name": "some name",
  64. "keywords":"some keywords",
  65. "content":"some content",
  66. "active":true,
  67. }
  68. Response:
  69. {
  70. "id": 1,
  71. "name": "some_name",
  72. ...
  73. }
  74. Test:
  75. 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"}'
  76. =end
  77. def create
  78. model_create_render(TextModule, params)
  79. end
  80. =begin
  81. Resource:
  82. PUT /api/v1/text_modules/{id}.json
  83. Payload:
  84. {
  85. "name": "some name",
  86. "keywords":"some keywords",
  87. "content":"some content",
  88. "active":true,
  89. }
  90. Response:
  91. {
  92. "id": 1,
  93. "name": "some_name",
  94. ...
  95. }
  96. Test:
  97. 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"}'
  98. =end
  99. def update
  100. model_update_render(TextModule, params)
  101. end
  102. def search
  103. model_search_render(TextModule, params)
  104. end
  105. =begin
  106. Resource:
  107. DELETE /api/v1/text_modules/{id}.json
  108. Response:
  109. {}
  110. Test:
  111. curl http://localhost/api/v1/text_modules.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X DELETE
  112. =end
  113. def destroy
  114. model_destroy_render(TextModule, params)
  115. end
  116. # @path [GET] /text_modules/import_example
  117. #
  118. # @summary Download of example CSV file.
  119. # @notes The requester have 'admin.text_module' permissions to be able to download it.
  120. # @example curl -u #{login}:#{password} http://localhost:3000/api/v1/text_modules/import_example
  121. #
  122. # @response_message 200 File download.
  123. # @response_message 403 Forbidden / Invalid session.
  124. def import_example
  125. csv_string = TextModule.csv_example(
  126. col_sep: params[:col_sep] || ',',
  127. )
  128. send_data(
  129. csv_string,
  130. filename: 'text_module-example.csv',
  131. type: 'text/csv',
  132. disposition: 'attachment'
  133. )
  134. end
  135. # @path [POST] /text_modules/import
  136. #
  137. # @summary Starts import.
  138. # @notes The requester have 'admin.text_module' permissions to be create a new import.
  139. # @example curl -u #{login}:#{password} -F 'file=@/path/to/file/Textbausteine_final2.csv' 'https://your.zammad/api/v1/text_modules/import?try=true'
  140. # @example curl -u #{login}:#{password} -F 'file=@/path/to/file/Textbausteine_final2.csv' 'https://your.zammad/api/v1/text_modules/import'
  141. #
  142. # @response_message 201 Import started.
  143. # @response_message 403 Forbidden / Invalid session.
  144. def import_start
  145. string = params[:data]
  146. if string.blank? && params[:file].present?
  147. string = params[:file].read.force_encoding('utf-8')
  148. end
  149. raise Exceptions::UnprocessableEntity, __('No source data submitted!') if string.blank?
  150. result = TextModule.csv_import(
  151. string: string,
  152. parse_params: {
  153. col_sep: params[:col_sep] || ',',
  154. },
  155. try: params[:try],
  156. delete: params[:delete],
  157. )
  158. render json: result, status: :ok
  159. end
  160. end