Skip to content

Commit

Permalink
Merge pull request #659 from degica/validation-if-name-exits
Browse files Browse the repository at this point in the history
throw error if heritage name already exits
  • Loading branch information
davidsiaw authored Mar 15, 2021
2 parents f6f5203 + 2882ee6 commit 6d36a83
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/controllers/heritages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions spec/requests/create_heritage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6d36a83

Please sign in to comment.