diff --git a/app/controllers/heritages_controller.rb b/app/controllers/heritages_controller.rb index 01853999..6e25df9b 100644 --- a/app/controllers/heritages_controller.rb +++ b/app/controllers/heritages_controller.rb @@ -12,6 +12,10 @@ def show end def create + if Heritage.find_by(name: params[:name]).present? + raise ExceptionHandler::InternalServerError.new("heritage name is already used ") + end + @heritage = BuildHeritage.new(permitted_params, district: @district).execute @heritage.save_and_deploy!(without_before_deploy: true, description: "Create") render json: @heritage diff --git a/spec/requests/create_heritage_spec.rb b/spec/requests/create_heritage_spec.rb index 1dd68c1d..b9000a2f 100644 --- a/spec/requests/create_heritage_spec.rb +++ b/spec/requests/create_heritage_spec.rb @@ -99,6 +99,15 @@ context "when version is 1" do let(:version) { 1 } it_behaves_like "create" + + it "throw error when heritage name is already used" do + api_request(:post, "/v1/districts/#{district.name}/heritages", params) + + # create same name heritage + api_request(:post, "/v1/districts/#{district.name}/heritages", params) + expect(response.status).to eq 500 + expect(JSON.parse(response.body)["error"]).to eq "heritage name is already used " + end end context "when version is 2" do