Merge pull request #6250 from tchak/fix-validation-json

Handle GeoJSON validation errors
This commit is contained in:
Paul Chavard 2021-06-02 20:07:50 +02:00 committed by GitHub
commit 8c889797ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 23 deletions

View file

@ -15,18 +15,26 @@ class Champs::CarteController < ApplicationController
if geo_area.nil? if geo_area.nil?
geo_area = champ.geo_areas.build(source: params_source, properties: {}) geo_area = champ.geo_areas.build(source: params_source, properties: {})
save_feature!(geo_area, create_params_feature)
end
render json: { feature: geo_area.to_feature }, status: :created if save_feature(geo_area, create_params_feature)
render json: { feature: geo_area.to_feature }, status: :created
else
render json: { errors: geo_area.errors.full_messages }, status: :unprocessable_entity
end
else
render json: { feature: geo_area.to_feature }, status: :ok
end
end end
def update def update
champ = policy_scope(Champ).find(params[:champ_id]) champ = policy_scope(Champ).find(params[:champ_id])
geo_area = champ.geo_areas.find(params[:id]) geo_area = champ.geo_areas.find(params[:id])
save_feature!(geo_area, update_params_feature)
head :no_content if save_feature(geo_area, update_params_feature)
head :no_content
else
render json: { errors: geo_area.errors.full_messages }, status: :unprocessable_entity
end
end end
def destroy def destroy
@ -66,13 +74,13 @@ class Champs::CarteController < ApplicationController
end end
end end
def save_feature!(geo_area, feature) def save_feature(geo_area, feature)
if feature[:geometry] if feature[:geometry]
geo_area.geometry = feature[:geometry] geo_area.geometry = feature[:geometry]
end end
if feature[:properties] if feature[:properties]
geo_area.properties.merge!(feature[:properties]) geo_area.properties.merge!(feature[:properties])
end end
geo_area.save! geo_area.save
end end
end end

View file

@ -41,23 +41,38 @@ describe Champs::CarteController, type: :controller do
post :create, params: params post :create, params: params
end end
it { expect(response.status).to eq 201 } context 'success' do
it { expect(response.status).to eq 201 }
end
context 'error' do
let(:feature) { attributes_for(:geo_area, :invalid_right_hand_rule_polygon) }
let(:params) do
{
champ_id: champ.id,
feature: feature,
source: GeoArea.sources.fetch(:selection_utilisateur)
}
end
it { expect(response.status).to eq 422 }
end
end end
describe 'PATCH #update' do describe 'PATCH #update' do
let(:params) do
{
champ_id: champ.id,
id: geo_area.id,
feature: feature
}
end
before do before do
patch :update, params: params patch :update, params: params
end end
context 'update geometry' do context 'update geometry' do
let(:params) do
{
champ_id: champ.id,
id: geo_area.id,
feature: feature
}
end
it { expect(response.status).to eq 204 } it { expect(response.status).to eq 204 }
end end
@ -69,19 +84,18 @@ describe Champs::CarteController, type: :controller do
} }
} }
end end
let(:params) do
{
champ_id: champ.id,
id: geo_area.id,
feature: feature
}
end
it { it {
expect(response.status).to eq 204 expect(response.status).to eq 204
expect(geo_area.reload.description).to eq('un point') expect(geo_area.reload.description).to eq('un point')
} }
end end
context 'error' do
let(:feature) { attributes_for(:geo_area, :invalid_right_hand_rule_polygon) }
it { expect(response.status).to eq 422 }
end
end end
describe 'DELETE #destroy' do describe 'DELETE #destroy' do