Add carto import api

takes a FeatureCollection to import and returns an augmented champ FeatureCollection
This commit is contained in:
Paul Chavard 2020-05-07 12:06:42 +02:00
parent f4677a0907
commit 4f2e504cc2
3 changed files with 45 additions and 4 deletions

View file

@ -58,7 +58,7 @@ class Champs::CarteController < ApplicationController
def create
champ = policy_scope(Champ).find(params[:champ_id])
geo_area = champ.geo_areas.selections_utilisateur.new
save_geometry!(geo_area)
save_geometry!(geo_area, params_feature)
render json: { feature: geo_area.to_feature }, status: :created
end
@ -66,7 +66,7 @@ class Champs::CarteController < ApplicationController
def update
champ = policy_scope(Champ).find(params[:champ_id])
geo_area = champ.geo_areas.selections_utilisateur.find(params[:id])
save_geometry!(geo_area)
save_geometry!(geo_area, params_feature)
head :no_content
end
@ -78,6 +78,16 @@ class Champs::CarteController < ApplicationController
head :no_content
end
def import
champ = policy_scope(Champ).find(params[:champ_id])
params_features.each do |feature|
geo_area = champ.geo_areas.selections_utilisateur.new
save_geometry!(geo_area, feature)
end
render json: champ.to_feature_collection, status: :created
end
private
def populate_cadastres(feature_collection)
@ -100,8 +110,16 @@ class Champs::CarteController < ApplicationController
end
end
def save_geometry!(geo_area)
geo_area.geometry = params[:feature][:geometry]
def params_feature
params[:feature]
end
def params_features
params[:feature_collection][:features]
end
def save_geometry!(geo_area, feature)
geo_area.geometry = feature[:geometry]
geo_area.save!
end

View file

@ -123,6 +123,7 @@ Rails.application.routes.draw do
get ':champ_id/carte/features', to: 'carte#index', as: :carte_features
post ':champ_id/carte/features', to: 'carte#create'
post ':champ_id/carte/features/import', to: 'carte#import'
patch ':champ_id/carte/features/:id', to: 'carte#update'
delete ':champ_id/carte/features/:id', to: 'carte#destroy'

View file

@ -73,6 +73,28 @@ describe Champs::CarteController, type: :controller do
it { expect(response.status).to eq 204 }
end
describe 'POST #import' do
render_views
let(:params) do
{
champ_id: champ.id,
feature_collection: {
features: [feature]
}
}
end
before do
post :import, params: params
end
it {
expect(response.status).to eq 201
expect(response.body).to include("bbox")
}
end
describe 'GET #index' do
render_views