links_controller.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. assets = {}
  11. link_list = []
  12. links.each do |item|
  13. link_list.push item
  14. if item['link_object'] == 'Ticket'
  15. ticket = Ticket.lookup(id: item['link_object_value'])
  16. assets = ticket.assets(assets)
  17. end
  18. end
  19. # return result
  20. render json: {
  21. links: link_list,
  22. assets: assets,
  23. }
  24. end
  25. # POST /api/v1/links/add
  26. def add
  27. object = Ticket.find_by(number: params[:link_object_source_number])
  28. if !object
  29. render json: { error: 'No such object!' }, status: :unprocessable_entity
  30. return
  31. end
  32. link = Link.add(
  33. link_type: params[:link_type],
  34. link_object_target: params[:link_object_target],
  35. link_object_target_value: params[:link_object_target_value],
  36. link_object_source: params[:link_object_source],
  37. link_object_source_value: object.id,
  38. )
  39. if link
  40. render json: link, status: :created
  41. else
  42. render json: link.errors, status: :unprocessable_entity
  43. end
  44. end
  45. # DELETE /api/v1/links/remove
  46. def remove
  47. link = Link.remove(params)
  48. if link
  49. render json: link, status: :created
  50. else
  51. render json: link.errors, status: :unprocessable_entity
  52. end
  53. end
  54. end