Skip to content

Commit

Permalink
chore: allow dashboard API to be paginated
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jul 16, 2021
1 parent 655e9dd commit da8bde4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/pact_broker/api/resources/dashboard.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
require "pact_broker/api/resources/base_resource"
require "pact_broker/api/decorators/dashboard_decorator"
require "pact_broker/api/decorators/dashboard_text_decorator"
require "pact_broker/api/resources/pagination_methods"

module PactBroker
module Api
module Resources
class Dashboard < BaseResource
include PaginationMethods

def content_types_provided
[
["application/hal+json", :to_json],
Expand All @@ -32,7 +35,7 @@ def policy_name
private

def index_items
index_service.find_index_items_for_api(identifier_from_path)
index_service.find_index_items_for_api(identifier_from_path.merge(pagination_options))
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions lib/pact_broker/api/resources/pagination_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module PactBroker
module Api
module Resources
module PaginationMethods
def pagination_options
{
page_number: request.query["pageNumber"]&.to_i,
page_size: request.query["pageSize"]&.to_i
}.compact
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/pact_broker/index/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ def self.consumer_version_tags(pact_publication, tags_option)
end
end

def self.find_index_items_for_api(consumer_name: nil, provider_name: nil, **_ignored)
def self.find_index_items_for_api(consumer_name: nil, provider_name: nil, page_number: nil, page_size: nil, **_ignored)
latest_pp_ids = latest_pact_publication_ids
pact_publications = head_pact_publications(consumer_name: consumer_name, provider_name: provider_name, tags: true)
pact_publications = head_pact_publications(consumer_name: consumer_name, provider_name: provider_name, tags: true, page_number: page_number, page_size: page_size)
.eager(:consumer)
.eager(:provider)
.eager(:pact_version)
Expand Down
25 changes: 23 additions & 2 deletions spec/lib/pact_broker/api/resources/dashboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,33 @@
module PactBroker
module Api
module Resources

describe Dashboard do
before do
td.create_pact_with_verification("Foo1", "1", "Bar", "2")
.create_pact_with_verification("Foo2", "1", "Bar", "2")
.create_pact_with_verification("Foo3", "1", "Bar", "2")
.create_pact_with_verification("Foo4", "1", "Bar", "2")
end

let(:response_body_hash) { JSON.parse(subject.body) }

let(:path) { "/dashboard" }
subject { get path; last_response }

subject { get(path) }

it { is_expected.to be_a_hal_json_success_response }

it "returns a list of items" do
expect(response_body_hash["items"]).to be_a(Array)
end

context "with pagination" do
subject { get(path, { pageNumber: 1, pageSize: 1 }) }

it "only returns the items for the page" do
expect(response_body_hash["items"].size).to eq 1
end
end
end
end
end
Expand Down

0 comments on commit da8bde4

Please sign in to comment.