Skip to content

Commit

Permalink
feat: store latest pact publication ids for each consumer version
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jun 3, 2018
1 parent 1db5b7b commit c8fa554
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/pact_broker/pacts/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,42 @@ class Repository
include PactBroker::Logging

def create params
PactPublication.new(
pact_publication = PactPublication.new(
consumer_version_id: params[:version_id],
provider_id: params[:provider_id],
pact_version: find_or_create_pact_version(params.fetch(:consumer_id), params.fetch(:provider_id), params[:json_content]),
).save.to_domain
).save
update_latest_pact_publication_ids(pact_publication)
pact_publication.to_domain
end

def update id, params
existing_model = PactPublication.find(id: id)
pact_version = find_or_create_pact_version(existing_model.consumer_version.pacticipant_id, existing_model.provider_id, params[:json_content])
if existing_model.pact_version_id != pact_version.id
PactPublication.new(
pact_publication = PactPublication.new(
consumer_version_id: existing_model.consumer_version_id,
provider_id: existing_model.provider_id,
revision_number: (existing_model.revision_number + 1),
pact_version: pact_version,
).save.to_domain
).save
update_latest_pact_publication_ids(pact_publication)
pact_publication.to_domain
else
existing_model.to_domain
end
end

def update_latest_pact_publication_ids(pact_publication)
table = AllPactPublications.db[:latest_pact_publication_ids_by_consumer_versions]
count = table.where(consumer_version_id: pact_publication.consumer_version_id, provider_id: pact_publication.provider_id).count
if count == 0
table.insert(consumer_version_id: pact_publication.consumer_version_id, provider_id: pact_publication.provider_id, pact_publication_id: pact_publication.id)
else
table.where(consumer_version_id: pact_publication.consumer_version_id, provider_id: pact_publication.provider_id).update(pact_publication_id: pact_publication.id)
end
end

def delete params
id = AllPactPublications
.consumer(params.consumer_name)
Expand Down

0 comments on commit c8fa554

Please sign in to comment.