2021-08-24 09:10:45 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Pagination
|
|
|
|
module Keyset
|
|
|
|
class CursorBasedRequestContext
|
2021-09-07 09:11:43 +00:00
|
|
|
DEFAULT_SORT_DIRECTION = :desc
|
2022-07-19 15:09:10 +00:00
|
|
|
DEFAULT_SORT_COLUMN = :id
|
|
|
|
|
2021-09-07 09:11:43 +00:00
|
|
|
attr_reader :request_context
|
2022-02-28 18:14:03 +00:00
|
|
|
|
2021-09-07 09:11:43 +00:00
|
|
|
delegate :params, to: :request_context
|
2021-08-24 09:10:45 +00:00
|
|
|
|
2021-09-07 09:11:43 +00:00
|
|
|
def initialize(request_context)
|
|
|
|
@request_context = request_context
|
2021-08-24 09:10:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def per_page
|
|
|
|
params[:per_page]
|
|
|
|
end
|
|
|
|
|
|
|
|
def cursor
|
|
|
|
params[:cursor]
|
|
|
|
end
|
|
|
|
|
|
|
|
def apply_headers(cursor_for_next_page)
|
|
|
|
Gitlab::Pagination::Keyset::HeaderBuilder
|
2021-09-07 09:11:43 +00:00
|
|
|
.new(request_context)
|
2021-08-24 09:10:45 +00:00
|
|
|
.add_next_page_header({ cursor: cursor_for_next_page })
|
|
|
|
end
|
2021-09-07 09:11:43 +00:00
|
|
|
|
|
|
|
def order_by
|
2022-07-19 15:09:10 +00:00
|
|
|
{ (params[:order_by]&.to_sym || DEFAULT_SORT_COLUMN) => (params[:sort]&.to_sym || DEFAULT_SORT_DIRECTION) }
|
2021-09-07 09:11:43 +00:00
|
|
|
end
|
2021-08-24 09:10:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|