domain_allows_controller.rb 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # frozen_string_literal: true
  2. class Api::V1::Admin::DomainAllowsController < Api::BaseController
  3. include Authorization
  4. include AccountableConcern
  5. LIMIT = 100
  6. before_action -> { authorize_if_got_token! :'admin:read', :'admin:read:domain_allows' }, only: [:index, :show]
  7. before_action -> { authorize_if_got_token! :'admin:write', :'admin:write:domain_allows' }, except: [:index, :show]
  8. before_action :set_domain_allows, only: :index
  9. before_action :set_domain_allow, only: [:show, :destroy]
  10. after_action :verify_authorized
  11. after_action :insert_pagination_headers, only: :index
  12. PAGINATION_PARAMS = %i(limit).freeze
  13. def create
  14. authorize :domain_allow, :create?
  15. @domain_allow = DomainAllow.find_by(resource_params)
  16. if @domain_allow.nil?
  17. @domain_allow = DomainAllow.create!(resource_params)
  18. log_action :create, @domain_allow
  19. end
  20. render json: @domain_allow, serializer: REST::Admin::DomainAllowSerializer
  21. end
  22. def index
  23. authorize :domain_allow, :index?
  24. render json: @domain_allows, each_serializer: REST::Admin::DomainAllowSerializer
  25. end
  26. def show
  27. authorize @domain_allow, :show?
  28. render json: @domain_allow, serializer: REST::Admin::DomainAllowSerializer
  29. end
  30. def destroy
  31. authorize @domain_allow, :destroy?
  32. UnallowDomainService.new.call(@domain_allow)
  33. log_action :destroy, @domain_allow
  34. render json: @domain_allow, serializer: REST::Admin::DomainAllowSerializer
  35. end
  36. private
  37. def set_domain_allows
  38. @domain_allows = filtered_domain_allows.order(id: :desc).to_a_paginated_by_id(limit_param(LIMIT), params_slice(:max_id, :since_id, :min_id))
  39. end
  40. def set_domain_allow
  41. @domain_allow = DomainAllow.find(params[:id])
  42. end
  43. def filtered_domain_allows
  44. # TODO: no filtering yet
  45. DomainAllow.all
  46. end
  47. def insert_pagination_headers
  48. set_pagination_headers(next_path, prev_path)
  49. end
  50. def next_path
  51. api_v1_admin_domain_allows_url(pagination_params(max_id: pagination_max_id)) if records_continue?
  52. end
  53. def prev_path
  54. api_v1_admin_domain_allows_url(pagination_params(min_id: pagination_since_id)) unless @domain_allows.empty?
  55. end
  56. def pagination_max_id
  57. @domain_allows.last.id
  58. end
  59. def pagination_since_id
  60. @domain_allows.first.id
  61. end
  62. def records_continue?
  63. @domain_allows.size == limit_param(LIMIT)
  64. end
  65. def pagination_params(core_params)
  66. params.slice(*PAGINATION_PARAMS).permit(*PAGINATION_PARAMS).merge(core_params)
  67. end
  68. def resource_params
  69. params.permit(:domain)
  70. end
  71. end