2024-04-15 15:06:05 +02:00
|
|
|
class Champs::CarteController < Champs::ChampController
|
2020-05-05 15:09:29 +02:00
|
|
|
def index
|
2021-05-06 18:50:06 +02:00
|
|
|
@focus = params[:focus].present?
|
2020-05-05 15:09:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2021-05-06 18:50:06 +02:00
|
|
|
geo_area = if params_source == GeoArea.sources.fetch(:cadastre)
|
2024-04-15 15:06:05 +02:00
|
|
|
@champ.geo_areas.find_by("properties->>'id' = :id", id: create_params_feature[:properties][:id])
|
2021-05-06 18:50:06 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if geo_area.nil?
|
2024-04-15 15:06:05 +02:00
|
|
|
geo_area = @champ.geo_areas.build(source: params_source, properties: {})
|
2020-05-05 15:09:29 +02:00
|
|
|
|
2021-06-02 12:43:24 +02:00
|
|
|
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
|
2020-05-05 15:09:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2024-04-15 15:06:05 +02:00
|
|
|
geo_area = @champ.geo_areas.find(params[:id])
|
2020-05-05 15:09:29 +02:00
|
|
|
|
2021-06-02 12:43:24 +02:00
|
|
|
if save_feature(geo_area, update_params_feature)
|
|
|
|
head :no_content
|
|
|
|
else
|
|
|
|
render json: { errors: geo_area.errors.full_messages }, status: :unprocessable_entity
|
|
|
|
end
|
2020-05-05 15:09:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2024-04-15 15:06:05 +02:00
|
|
|
@champ.geo_areas.find(params[:id]).destroy!
|
|
|
|
@champ.touch
|
2020-05-05 15:09:29 +02:00
|
|
|
|
|
|
|
head :no_content
|
|
|
|
end
|
|
|
|
|
2020-04-09 17:32:20 +02:00
|
|
|
private
|
|
|
|
|
2021-05-06 18:50:06 +02:00
|
|
|
def params_source
|
|
|
|
params[:source]
|
2020-05-07 12:06:42 +02:00
|
|
|
end
|
|
|
|
|
2021-05-25 16:10:01 +02:00
|
|
|
def create_params_feature
|
2021-05-06 18:50:06 +02:00
|
|
|
params.require(:feature).permit(properties: [
|
|
|
|
:filename,
|
|
|
|
:description,
|
|
|
|
:arpente,
|
|
|
|
:commune,
|
|
|
|
:contenance,
|
|
|
|
:created,
|
|
|
|
:id,
|
|
|
|
:numero,
|
|
|
|
:prefixe,
|
|
|
|
:section,
|
|
|
|
:updated
|
|
|
|
]).tap do |feature|
|
|
|
|
feature[:geometry] = params[:feature][:geometry]
|
|
|
|
end
|
2020-05-07 12:06:42 +02:00
|
|
|
end
|
|
|
|
|
2021-05-25 16:10:01 +02:00
|
|
|
def update_params_feature
|
|
|
|
params.require(:feature).permit(properties: [:description]).tap do |feature|
|
|
|
|
feature[:geometry] = params[:feature][:geometry]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-02 12:43:24 +02:00
|
|
|
def save_feature(geo_area, feature)
|
2020-06-04 11:32:59 +02:00
|
|
|
if feature[:geometry]
|
|
|
|
geo_area.geometry = feature[:geometry]
|
|
|
|
end
|
2021-05-06 18:50:06 +02:00
|
|
|
if feature[:properties]
|
|
|
|
geo_area.properties.merge!(feature[:properties])
|
2020-06-04 11:32:59 +02:00
|
|
|
end
|
2023-06-06 11:28:10 +02:00
|
|
|
if geo_area.save
|
2024-04-15 15:06:05 +02:00
|
|
|
@champ.touch
|
2023-06-06 11:28:10 +02:00
|
|
|
true
|
|
|
|
end
|
2020-05-05 15:09:29 +02:00
|
|
|
end
|
2018-10-17 12:02:34 +02:00
|
|
|
end
|