diff --git a/lib/pact_broker/api/resources/pacticipant.rb b/lib/pact_broker/api/resources/pacticipant.rb index e0c9c02f0..b1d06a794 100644 --- a/lib/pact_broker/api/resources/pacticipant.rb +++ b/lib/pact_broker/api/resources/pacticipant.rb @@ -34,7 +34,7 @@ def from_json if pacticipant @pacticipant = pacticipant_service.update params_with_string_keys.merge('name' => pacticipant_name) else - @pacticipant = pacticipant_service.create params_with_string_keys.merge('name' => pacticipant_name) + @pacticipant = pacticipant_service.create params.merge(:name => pacticipant_name) response.headers["Location"] = pacticipant_url(base_url, pacticipant) end response.body = to_json diff --git a/lib/pact_broker/api/resources/pacticipants.rb b/lib/pact_broker/api/resources/pacticipants.rb index 9d8ef1439..a201fb233 100644 --- a/lib/pact_broker/api/resources/pacticipants.rb +++ b/lib/pact_broker/api/resources/pacticipants.rb @@ -32,7 +32,7 @@ def post_is_create? end def from_json - created_model = pacticipant_service.create params_with_string_keys + created_model = pacticipant_service.create(params) response.body = decorator_for(created_model).to_json(user_options: decorator_context) end diff --git a/lib/pact_broker/pacticipants/repository.rb b/lib/pact_broker/pacticipants/repository.rb index 231b483cd..50401afcc 100644 --- a/lib/pact_broker/pacticipants/repository.rb +++ b/lib/pact_broker/pacticipants/repository.rb @@ -41,6 +41,9 @@ def find_by_name_or_create name end end + # Need to be able to handle two calls that make the pacticipant at the same time. + # TODO raise error if attributes apart from name are different, because this indicates that + # the second request is not at the same time. def create args PactBroker::Domain::Pacticipant.dataset.insert_ignore.insert( name: args[:name], diff --git a/lib/pact_broker/pacticipants/service.rb b/lib/pact_broker/pacticipants/service.rb index fd34b5f7d..6752125ce 100644 --- a/lib/pact_broker/pacticipants/service.rb +++ b/lib/pact_broker/pacticipants/service.rb @@ -58,6 +58,7 @@ def self.find_pacticipant_repository_url_by_pacticipant_name name end def self.update params + # TODO move this to the repository! pacticipant = pacticipant_repository.find_by_name(params.fetch('name')) PactBroker::Api::Decorators::PacticipantDecorator.new(pacticipant).from_hash(params) pacticipant.save @@ -65,10 +66,7 @@ def self.update params end def self.create params - pacticipant = PactBroker::Domain::Pacticipant.new - PactBroker::Api::Decorators::PacticipantDecorator.new(pacticipant).from_hash(params) - pacticipant.save - pacticipant + pacticipant_repository.create(params) end def self.delete name diff --git a/spec/lib/pact_broker/api/resources/pacticipants_spec.rb b/spec/lib/pact_broker/api/resources/pacticipants_spec.rb index 98ea5013c..cf289bc97 100644 --- a/spec/lib/pact_broker/api/resources/pacticipants_spec.rb +++ b/spec/lib/pact_broker/api/resources/pacticipants_spec.rb @@ -47,7 +47,7 @@ module Resources context "with valid JSON" do it "creates the pacticipant" do - expect(PactBroker::Pacticipants::Service).to receive(:create).with('name' => 'New Consumer') + expect(PactBroker::Pacticipants::Service).to receive(:create).with(:name => 'New Consumer') subject end