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.
 
 
 
 

80 lines
1.7 KiB

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