Skip to content

Commit 78d92ad

Browse files
committed
fix: gracefully handle two requests coming in at the same time to create the same pacticipant
1 parent ba1f6bc commit 78d92ad

File tree

5 files changed

+8
-7
lines changed

5 files changed

+8
-7
lines changed

lib/pact_broker/api/resources/pacticipant.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def from_json
3434
if pacticipant
3535
@pacticipant = pacticipant_service.update params_with_string_keys.merge('name' => pacticipant_name)
3636
else
37-
@pacticipant = pacticipant_service.create params_with_string_keys.merge('name' => pacticipant_name)
37+
@pacticipant = pacticipant_service.create params.merge(:name => pacticipant_name)
3838
response.headers["Location"] = pacticipant_url(base_url, pacticipant)
3939
end
4040
response.body = to_json

lib/pact_broker/api/resources/pacticipants.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def post_is_create?
3232
end
3333

3434
def from_json
35-
created_model = pacticipant_service.create params_with_string_keys
35+
created_model = pacticipant_service.create(params)
3636
response.body = decorator_for(created_model).to_json(user_options: decorator_context)
3737
end
3838

lib/pact_broker/pacticipants/repository.rb

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def find_by_name_or_create name
4141
end
4242
end
4343

44+
# Need to be able to handle two calls that make the pacticipant at the same time.
45+
# TODO raise error if attributes apart from name are different, because this indicates that
46+
# the second request is not at the same time.
4447
def create args
4548
PactBroker::Domain::Pacticipant.dataset.insert_ignore.insert(
4649
name: args[:name],

lib/pact_broker/pacticipants/service.rb

+2-4
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,15 @@ def self.find_pacticipant_repository_url_by_pacticipant_name name
5858
end
5959

6060
def self.update params
61+
# TODO move this to the repository!
6162
pacticipant = pacticipant_repository.find_by_name(params.fetch('name'))
6263
PactBroker::Api::Decorators::PacticipantDecorator.new(pacticipant).from_hash(params)
6364
pacticipant.save
6465
pacticipant_repository.find_by_name(params.fetch('name'))
6566
end
6667

6768
def self.create params
68-
pacticipant = PactBroker::Domain::Pacticipant.new
69-
PactBroker::Api::Decorators::PacticipantDecorator.new(pacticipant).from_hash(params)
70-
pacticipant.save
71-
pacticipant
69+
pacticipant_repository.create(params)
7270
end
7371

7472
def self.delete name

spec/lib/pact_broker/api/resources/pacticipants_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module Resources
4747

4848
context "with valid JSON" do
4949
it "creates the pacticipant" do
50-
expect(PactBroker::Pacticipants::Service).to receive(:create).with('name' => 'New Consumer')
50+
expect(PactBroker::Pacticipants::Service).to receive(:create).with(:name => 'New Consumer')
5151
subject
5252
end
5353

0 commit comments

Comments
 (0)