Skip to content

Commit

Permalink
feat: Pacticipant pagination (#585)
Browse files Browse the repository at this point in the history
* chore: add pagination to pacticipants

* chore: Remove unused pagination options from versions resource

* chore: Fix arguments in pacticipant service find method

* chore: add missing dependencies to versions resource

* chore: fix up argument parameters for pacticipant find method

* chore: fix up argument parameters for pacticipant find method

* chore: PR feedback refactors for pagination of pacticipants

* chore: PR feedback remove request arg as it's already available
  • Loading branch information
Inksprout authored Dec 13, 2022
1 parent bcda9a3 commit f1a9be2
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "roar/json/hal"
require "pact_broker/api/pact_broker_urls"
require_relative "embedded_version_decorator"
require_relative "pagination_links"
require "pact_broker/domain/pacticipant"
require "pact_broker/api/decorators/pacticipant_decorator"

Expand All @@ -11,6 +12,8 @@ class PacticipantCollectionDecorator < BaseDecorator

collection :entries, :as => :pacticipants, :class => PactBroker::Domain::Pacticipant, :extend => PactBroker::Api::Decorators::PacticipantDecorator, embedded: true

include PaginationLinks

link :self do | options |
pacticipants_url options[:base_url]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def policy_name
private

def index_items
index_service.find_index_items_for_api(identifier_from_path.merge(pagination_options))
index_service.find_index_items_for_api(identifier_from_path.merge(pagination_options || {}))
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion lib/pact_broker/api/resources/pacticipants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
require "pact_broker/domain/pacticipant"
require "pact_broker/hash_refinements"
require "pact_broker/api/contracts/pacticipant_create_schema"
require "pact_broker/api/resources/pagination_methods"

module PactBroker
module Api
module Resources
class Pacticipants < BaseResource
using PactBroker::HashRefinements
include PaginationMethods


def content_types_provided
[["application/hal+json", :to_json]]
Expand Down Expand Up @@ -44,7 +47,7 @@ def create_path
end

def to_json
generate_json(pacticipant_service.find_all_pacticipants)
generate_json(pacticipant_service.find_all_pacticipants(pagination_options))
end

def generate_json pacticipants
Expand Down
12 changes: 8 additions & 4 deletions lib/pact_broker/api/resources/pagination_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ module Api
module Resources
module PaginationMethods
def pagination_options
{
page_number: request.query["pageNumber"]&.to_i,
page_size: request.query["pageSize"]&.to_i
}.compact
if request.query["pageNumber"] || request.query["pageSize"]
{
page_number: request.query["pageNumber"]&.to_i || 1,
page_size: request.query["pageSize"]&.to_i || 100
}
else
nil
end
end
end
end
Expand Down
17 changes: 5 additions & 12 deletions lib/pact_broker/api/resources/versions.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
require "pact_broker/api/resources/base_resource"
require "pact_broker/configuration"
require "pact_broker/api/decorators/versions_decorator"
require "pact_broker/api/resources/pagination_methods"


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

def content_types_provided
[["application/hal+json", :to_json]]
end
Expand All @@ -23,23 +27,12 @@ def to_json
end

def versions
@versions ||= pacticipant_service.find_all_pacticipant_versions_in_reverse_order(pacticipant_name, pagination_options)
@versions ||= version_service.find_all_pacticipant_versions_in_reverse_order(pacticipant_name, pagination_options)
end

def policy_name
:'versions::versions'
end

def pagination_options
if request.query["pageNumber"] || request.query["pageSize"]
{
page_number: request.query["pageNumber"]&.to_i || 1,
page_size: request.query["pageSize"]&.to_i || 100
}
else
nil
end
end
end
end
end
Expand Down
20 changes: 4 additions & 16 deletions lib/pact_broker/pacticipants/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,14 @@ def find_by_id id
PactBroker::Domain::Pacticipant.where(id: id).single_record
end

def find_all
find
def find_all(pagination_options = nil)
find({}, pagination_options)
end

def find options = {}
def find(options = {}, pagination_options = nil)
query = PactBroker::Domain::Pacticipant.select_all_qualified
query = query.label(options[:label_name]) if options[:label_name]
query.order_ignore_case(Sequel[:pacticipants][:name]).eager(:labels).eager(:latest_version).all
end

def find_all_pacticipant_versions_in_reverse_order name, pagination_options = nil
pacticipant = pacticipant_repository.find_by_name!(name)
query = PactBroker::Domain::Version
.where(pacticipant: pacticipant)
.eager(:pacticipant)
.eager(branch_versions: [:version, :branch_head, { branch: :pacticipant }])
.eager(tags: :head_tag)
.eager(:pact_publications)
.reverse_order(:order)
query.all_with_pagination_options(pagination_options)
query.order_ignore_case(Sequel[:pacticipants][:name]).eager(:labels).eager(:latest_version).all_with_pagination_options(pagination_options)
end

def find_by_name_or_create name
Expand Down
12 changes: 4 additions & 8 deletions lib/pact_broker/pacticipants/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def self.find_potential_duplicate_pacticipants pacticipant_name
} .collect{ | name | pacticipant_repository.find_by_name(name) }
end

def self.find_all_pacticipants
pacticipant_repository.find_all
def self.find_all_pacticipants(pagination_options)
pacticipant_repository.find_all(pagination_options)
end

def self.find_pacticipant_by_name(name)
Expand All @@ -48,12 +48,8 @@ def self.find_by_id(id)
pacticipant_repository.find_by_id(id)
end

def self.find(options)
pacticipant_repository.find options
end

def self.find_all_pacticipant_versions_in_reverse_order(name, pagination_options = nil)
pacticipant_repository.find_all_pacticipant_versions_in_reverse_order(name, pagination_options)
def self.find(options, pagination_options = nil)
pacticipant_repository.find(options, pagination_options)
end

def self.find_pacticipant_repository_url_by_pacticipant_name(name)
Expand Down
12 changes: 12 additions & 0 deletions lib/pact_broker/versions/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ def find_by_pacticipant_name_and_number pacticipant_name, number
.single_record
end

def find_all_pacticipant_versions_in_reverse_order name, pagination_options = nil
pacticipant = pacticipant_repository.find_by_name!(name)
query = PactBroker::Domain::Version
.where(pacticipant: pacticipant)
.eager(:pacticipant)
.eager(branch_versions: [:version, :branch_head, { branch: :pacticipant }])
.eager(tags: :head_tag)
.eager(:pact_publications)
.reverse_order(:order)
query.all_with_pagination_options(pagination_options)
end

# There may be a race condition if two simultaneous requests come in to create the same version
def create(args)
version_params = {
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/versions/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def self.find_latest_by_pacticipant_name_and_branch_name(pacticipant_name, branc
version_repository.find_latest_by_pacticipant_name_and_branch_name(pacticipant_name, branch_name)
end

def self.find_all_pacticipant_versions_in_reverse_order(name, pagination_options = nil)
version_repository.find_all_pacticipant_versions_in_reverse_order(name, pagination_options)
end

def self.create_or_overwrite(pacticipant_name, version_number, version)
pacticipant = pacticipant_repository.find_by_name_or_create(pacticipant_name)
version = version_repository.create_or_overwrite(pacticipant, version_number, version)
Expand Down
34 changes: 34 additions & 0 deletions spec/features/get_pacticipants_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
describe "Get pacticipants" do

let(:path) { "/pacticipants" }
let(:response_body_hash) { JSON.parse(subject.body, symbolize_names: true) }
let(:expected_response_body) { {name: "Foo"} }

subject { get(path) }

context "when pacts exist" do

before do
td.create_pacticipant("Foo")
.create_label("ios")
.create_pacticipant("Bar")
.create_label("android")
end

it "returns a 200 OK" do
expect(subject).to be_a_hal_json_success_response
end

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

it "paginates the response" do
expect(response_body_hash[:_links][:"pacticipants"].size).to eq 1
end

it "includes the pagination relations" do
expect(response_body_hash[:_links]).to have_key(:next)
end
end
end
end
25 changes: 0 additions & 25 deletions spec/lib/pact_broker/pacticipants/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,31 +185,6 @@ module Pacticipants

end

describe "#find_all_pacticipant_versions_in_reverse_order" do
before do
td
.create_consumer("Foo")
.create_consumer_version("1.2.3")
.create_consumer_version("4.5.6")
.create_consumer("Bar")
.create_consumer_version("8.9.0")
end

subject { Repository.new.find_all_pacticipant_versions_in_reverse_order "Foo" }

it "returns all the application versions for the given consumer" do
expect(subject.collect(&:number)).to eq ["4.5.6", "1.2.3"]
end

context "with pagination options" do
subject { Repository.new.find_all_pacticipant_versions_in_reverse_order "Foo", page_number: 1, page_size: 1 }

it "paginates the query" do
expect(subject.collect(&:number)).to eq ["4.5.6"]
end
end
end

describe "#search_by_name" do
let(:consumer_name) { "This is_a test-consumer" }
let(:provider_name) { "and a test/provider" }
Expand Down
25 changes: 25 additions & 0 deletions spec/lib/pact_broker/versions/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@ module Versions
end
end

describe "#find_all_pacticipant_versions_in_reverse_order" do
before do
td
.create_consumer("Foo")
.create_consumer_version("1.2.3")
.create_consumer_version("4.5.6")
.create_consumer("Bar")
.create_consumer_version("8.9.0")
end

subject { Repository.new.find_all_pacticipant_versions_in_reverse_order "Foo" }

it "returns all the application versions for the given consumer" do
expect(subject.collect(&:number)).to eq ["4.5.6", "1.2.3"]
end

context "with pagination options" do
subject { Repository.new.find_all_pacticipant_versions_in_reverse_order "Foo", page_number: 1, page_size: 1 }

it "paginates the query" do
expect(subject.collect(&:number)).to eq ["4.5.6"]
end
end
end

describe "#find_by_pacticipant_name_and_latest_tag" do
before do
td.create_consumer("Bar")
Expand Down

0 comments on commit f1a9be2

Please sign in to comment.