From 4f2e504cc2ceb3526a3b00e3a5ed9630f9444727 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 7 May 2020 12:06:42 +0200 Subject: [PATCH] Add carto import api takes a FeatureCollection to import and returns an augmented champ FeatureCollection --- app/controllers/champs/carte_controller.rb | 26 ++++++++++++++++--- config/routes.rb | 1 + .../champs/carte_controller_spec.rb | 22 ++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/app/controllers/champs/carte_controller.rb b/app/controllers/champs/carte_controller.rb index 6004c2676..1ff5980c6 100644 --- a/app/controllers/champs/carte_controller.rb +++ b/app/controllers/champs/carte_controller.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 0595c6014..b77856f98 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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' diff --git a/spec/controllers/champs/carte_controller_spec.rb b/spec/controllers/champs/carte_controller_spec.rb index 14173e150..2e39bc637 100644 --- a/spec/controllers/champs/carte_controller_spec.rb +++ b/spec/controllers/champs/carte_controller_spec.rb @@ -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