aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/api
diff options
context:
space:
mode:
authorcedricnjanga2017-11-30 14:15:43 +0100
committercedricnjanga2017-12-05 15:39:08 +0100
commit03df723a37dc53ab73fc6539a85c9a0c892e7c99 (patch)
treecae942e3f51bc5c15db8b8bd00d2fc31b266e78c /app/controllers/api
parentbb5fc4ea0755c2b7fd16d31e9d31d2a0967fe5cb (diff)
downloadchouette-core-03df723a37dc53ab73fc6539a85c9a0c892e7c99.tar.bz2
Refs #5127 Add two new endpoints to the API
ComplianceCheckSet#notify_parent NetexImport#notify_parent
Diffstat (limited to 'app/controllers/api')
-rw-r--r--app/controllers/api/v1/internals/application_controller.rb19
-rw-r--r--app/controllers/api/v1/internals/compliance_check_sets_controller.rb46
-rw-r--r--app/controllers/api/v1/internals/netex_imports_controller.rb104
3 files changed, 169 insertions, 0 deletions
diff --git a/app/controllers/api/v1/internals/application_controller.rb b/app/controllers/api/v1/internals/application_controller.rb
new file mode 100644
index 000000000..2a9f2558a
--- /dev/null
+++ b/app/controllers/api/v1/internals/application_controller.rb
@@ -0,0 +1,19 @@
+module Api
+ module V1
+ module Internals
+ class ApplicationController < ActionController::Base
+ inherit_resources
+ respond_to :json
+ layout false
+ before_action :authenticate
+
+ private
+ def authenticate
+ authenticate_with_http_token do |token, options|
+ @current_organisation = Api::V1::ApiKey.find_by_token(token).try(:organisation)
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/app/controllers/api/v1/internals/compliance_check_sets_controller.rb b/app/controllers/api/v1/internals/compliance_check_sets_controller.rb
new file mode 100644
index 000000000..5e9137cb5
--- /dev/null
+++ b/app/controllers/api/v1/internals/compliance_check_sets_controller.rb
@@ -0,0 +1,46 @@
+module Api
+ module V1
+ module Internals
+ class ComplianceCheckSetsController < Api::V1::Internals::ApplicationController
+ include ControlFlow
+
+ def validated
+ find_compliance_check_set
+
+ if @compliance_check_set.update_status
+ render :validated
+ else
+ render json: {
+ status: "error",
+ messages: @compliance_check_set.errors.full_messages
+ }
+ end
+ end
+
+ def notify_parent
+ find_compliance_check_set
+ if @compliance_check_set.notify_parent
+ render json: {
+ status: "ok",
+ message:"#{@compliance_check_set.parent_type} (id: #{@compliance_check_set.parent_id}) successfully notified at #{l(@compliance_check_set.notified_parent_at)}"
+ }
+ else
+ render json: {status: "error", message: @compliance_check_set.errors.full_messages }
+ end
+ end
+
+ private
+
+ def find_compliance_check_set
+ @compliance_check_set = ComplianceCheckSet.find(params[:id])
+ rescue ActiveRecord::RecordNotFound
+ render json: {
+ status: "error",
+ message: "Record not found"
+ }
+ finish_action!
+ end
+ end
+ end
+ end
+end
diff --git a/app/controllers/api/v1/internals/netex_imports_controller.rb b/app/controllers/api/v1/internals/netex_imports_controller.rb
new file mode 100644
index 000000000..c8e33f7b8
--- /dev/null
+++ b/app/controllers/api/v1/internals/netex_imports_controller.rb
@@ -0,0 +1,104 @@
+module Api
+ module V1
+ module Internals
+ class NetexImportsController < Api::V1::Internals::ApplicationController
+ include ControlFlow
+
+ def create
+ respond_to do | format |
+ format.json(&method(:create_models))
+ end
+ end
+
+ def notify_parent
+ find_netex_import
+ if @netex_import.notify_parent
+ render json: {
+ status: "ok",
+ message:"#{@netex_import.parent_type} (id: #{@netex_import.parent_id}) successfully notified at #{l(@netex_import.notified_parent_at)}"
+ }
+ else
+ render json: {status: "error", message: @netex_import.errors.full_messages }
+ end
+ end
+
+ private
+
+ def find_netex_import
+ @netex_import = NetexImport.find(params[:id])
+ rescue ActiveRecord::RecordNotFound
+ render json: {
+ status: "error",
+ message: "Record not found"
+ }
+ finish_action!
+ end
+
+ def find_workbench
+ @workbench = Workbench.find(netex_import_params['workbench_id'])
+ rescue ActiveRecord::RecordNotFound
+ render json: {errors: {'workbench_id' => 'missing'}}, status: 406
+ finish_action!
+ end
+
+ def create_models
+ find_workbench
+ create_referential
+ create_netex_import
+ end
+
+ def create_netex_import
+ attributes = netex_import_params.merge creator: "Webservice"
+
+ attributes = attributes.merge referential_id: @new_referential.id
+
+ @netex_import = NetexImport.new attributes
+ @netex_import.save!
+
+ unless @netex_import.referential
+ Rails.logger.info "Can't create referential for import #{@netex_import.id}: #{@new_referential.inspect} #{@new_referential.metadatas.inspect} #{@new_referential.errors.full_messages}"
+ @netex_import.messages.create criticity: :error, message_key: "referential_creation"
+ end
+ rescue ActiveRecord::RecordInvalid
+ render json: {errors: @netex_import.errors}, status: 406
+ finish_action!
+ end
+
+ def create_referential
+ @new_referential =
+ Referential.new(
+ name: netex_import_params['name'],
+ organisation_id: @workbench.organisation_id,
+ workbench_id: @workbench.id,
+ metadatas: [metadata]
+ )
+ @new_referential.save
+ end
+
+ def metadata
+ metadata = ReferentialMetadata.new
+
+ if netex_import_params['file']
+ netex_file = STIF::NetexFile.new(netex_import_params['file'].to_io)
+ frame = netex_file.frames.first
+
+ if frame
+ metadata.periodes = frame.periods
+
+ line_objectids = frame.line_refs.map { |ref| "STIF:CODIFLIGNE:Line:#{ref}" }
+ metadata.line_ids = @workbench.lines.where(objectid: line_objectids).pluck(:id)
+ end
+ end
+
+ metadata
+ end
+
+ def netex_import_params
+ params
+ .require('netex_import')
+ .permit(:file, :name, :workbench_id, :parent_id, :parent_type)
+ end
+ end
+ end
+ end
+end