tags_controller.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class TagsController < ApplicationController
  3. prepend_before_action :authentication_check
  4. # GET /api/v1/tag_search?term=abc
  5. def search
  6. list = Tag::Item.where('name_downcase LIKE ?', "#{params[:term].strip.downcase}%").order('name ASC').limit(params[:limit] || 10)
  7. results = []
  8. list.each do |item|
  9. result = {
  10. id: item.id,
  11. value: item.name,
  12. }
  13. results.push result
  14. end
  15. render json: results
  16. end
  17. # GET /api/v1/tags
  18. def list
  19. list = Tag.tag_list(
  20. object: params[:object],
  21. o_id: params[:o_id],
  22. )
  23. # return result
  24. render json: {
  25. tags: list,
  26. }
  27. end
  28. # POST /api/v1/tag/add
  29. def add
  30. success = Tag.tag_add(
  31. object: params[:object],
  32. o_id: params[:o_id],
  33. item: params[:item],
  34. )
  35. if success
  36. render json: success, status: :created
  37. else
  38. render json: success.errors, status: :unprocessable_entity
  39. end
  40. end
  41. # DELETE /api/v1/tag/remove
  42. def remove
  43. success = Tag.tag_remove(
  44. object: params[:object],
  45. o_id: params[:o_id],
  46. item: params[:item],
  47. )
  48. if success
  49. render json: success, status: :created
  50. else
  51. render json: success.errors, status: :unprocessable_entity
  52. end
  53. end
  54. # GET /api/v1/tag_list
  55. def admin_list
  56. permission_check('admin.tag')
  57. list = Tag::Item.order('name ASC').limit(params[:limit] || 1000)
  58. results = []
  59. list.each do |item|
  60. result = {
  61. id: item.id,
  62. name: item.name,
  63. count: Tag.where(tag_item_id: item.id).count
  64. }
  65. results.push result
  66. end
  67. render json: results
  68. end
  69. # POST /api/v1/tag_list
  70. def admin_create
  71. permission_check('admin.tag')
  72. Tag::Item.lookup_by_name_and_create(params[:name])
  73. render json: {}
  74. end
  75. # PUT /api/v1/tag_list/:id
  76. def admin_rename
  77. permission_check('admin.tag')
  78. Tag::Item.rename(
  79. id: params[:id],
  80. name: params[:name],
  81. )
  82. render json: {}
  83. end
  84. # DELETE /api/v1/tag_list/:id
  85. def admin_delete
  86. permission_check('admin.tag')
  87. Tag::Item.remove(params[:id])
  88. render json: {}
  89. end
  90. end