diff options
Diffstat (limited to 'lib/gitlab/pagination/keyset/cursor_based_request_context.rb')
-rw-r--r-- | lib/gitlab/pagination/keyset/cursor_based_request_context.rb | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/gitlab/pagination/keyset/cursor_based_request_context.rb b/lib/gitlab/pagination/keyset/cursor_based_request_context.rb new file mode 100644 index 00000000000..18390f5b59d --- /dev/null +++ b/lib/gitlab/pagination/keyset/cursor_based_request_context.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +module Gitlab + module Pagination + module Keyset + class CursorBasedRequestContext + DEFAULT_SORT_DIRECTION = :desc + attr_reader :request_context + delegate :params, to: :request_context + + def initialize(request_context) + @request_context = request_context + end + + def per_page + params[:per_page] + end + + def cursor + params[:cursor] + end + + def apply_headers(cursor_for_next_page) + Gitlab::Pagination::Keyset::HeaderBuilder + .new(request_context) + .add_next_page_header({ cursor: cursor_for_next_page }) + end + + def order_by + { params[:order_by].to_sym => params[:sort]&.to_sym || DEFAULT_SORT_DIRECTION } + end + end + end + end +end |