The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

82 lines
1.7 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::Lists::AccountsController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :read }, only: [:show]
  4. before_action -> { doorkeeper_authorize! :write }, except: [:show]
  5. before_action :require_user!
  6. before_action :set_list
  7. after_action :insert_pagination_headers, only: :show
  8. def show
  9. @accounts = @list.accounts.paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id])
  10. render json: @accounts, each_serializer: REST::AccountSerializer
  11. end
  12. def create
  13. ApplicationRecord.transaction do
  14. list_accounts.each do |account|
  15. @list.accounts << account
  16. end
  17. end
  18. render_empty
  19. end
  20. def destroy
  21. ListAccount.where(list: @list, account_id: account_ids).destroy_all
  22. render_empty
  23. end
  24. private
  25. def set_list
  26. @list = List.where(account: current_account).find(params[:list_id])
  27. end
  28. def list_accounts
  29. Account.find(account_ids)
  30. end
  31. def account_ids
  32. Array(resource_params[:account_ids])
  33. end
  34. def resource_params
  35. params.permit(account_ids: [])
  36. end
  37. def insert_pagination_headers
  38. set_pagination_headers(next_path, prev_path)
  39. end
  40. def next_path
  41. if records_continue?
  42. api_v1_list_accounts_url pagination_params(max_id: pagination_max_id)
  43. end
  44. end
  45. def prev_path
  46. unless @accounts.empty?
  47. api_v1_list_accounts_url pagination_params(since_id: pagination_since_id)
  48. end
  49. end
  50. def pagination_max_id
  51. @accounts.last.id
  52. end
  53. def pagination_since_id
  54. @accounts.first.id
  55. end
  56. def records_continue?
  57. @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  58. end
  59. def pagination_params(core_params)
  60. params.permit(:limit).merge(core_params)
  61. end
  62. end