links_controller.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class LinksController < ApplicationController
  3. prepend_before_action :authentication_check
  4. # GET /api/v1/links
  5. def index
  6. links = Link.list(
  7. link_object: params[:link_object],
  8. link_object_value: params[:link_object_value],
  9. )
  10. linked_objects = links
  11. .map { |elem| elem['link_object']&.safe_constantize&.lookup(id: elem['link_object_value']) }
  12. .compact
  13. # return result
  14. render json: {
  15. links: links,
  16. assets: ApplicationModel::CanAssets.reduce(linked_objects),
  17. }
  18. end
  19. # POST /api/v1/links/add
  20. def add
  21. object = case params[:link_object_source]
  22. when 'Ticket'
  23. Ticket.find_by(number: params[:link_object_source_number])
  24. when 'KnowledgeBase::Answer::Translation'
  25. KnowledgeBase::Answer::Translation.find_by(id: params[:link_object_source_number])
  26. end
  27. if !object
  28. render json: { error: 'No such object!' }, status: :unprocessable_entity
  29. return
  30. end
  31. link = Link.add(
  32. link_type: params[:link_type],
  33. link_object_target: params[:link_object_target],
  34. link_object_target_value: params[:link_object_target_value],
  35. link_object_source: params[:link_object_source],
  36. link_object_source_value: object.id,
  37. )
  38. if link
  39. render json: link, status: :created
  40. else
  41. render json: link.errors, status: :unprocessable_entity
  42. end
  43. end
  44. # DELETE /api/v1/links/remove
  45. def remove
  46. link = Link.remove(params)
  47. if link
  48. render json: link, status: :created
  49. else
  50. render json: link.errors, status: :unprocessable_entity
  51. end
  52. end
  53. end