From 03df723a37dc53ab73fc6539a85c9a0c892e7c99 Mon Sep 17 00:00:00 2001 From: cedricnjanga Date: Thu, 30 Nov 2017 14:15:43 +0100 Subject: Refs #5127 Add two new endpoints to the API ComplianceCheckSet#notify_parent NetexImport#notify_parent --- .../api/v1/internals/application_controller.rb | 19 ++++ .../internals/compliance_check_sets_controller.rb | 46 +++++++++ .../api/v1/internals/netex_imports_controller.rb | 104 +++++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 app/controllers/api/v1/internals/application_controller.rb create mode 100644 app/controllers/api/v1/internals/compliance_check_sets_controller.rb create mode 100644 app/controllers/api/v1/internals/netex_imports_controller.rb (limited to 'app/controllers/api') 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 -- cgit v1.2.3 From c3a56df2125a43178e701d7d149e18201f276cf0 Mon Sep 17 00:00:00 2001 From: cedricnjanga Date: Mon, 4 Dec 2017 17:06:18 +0100 Subject: Small refacto to handle no_parent error for ComplianceCheckSet --- app/controllers/api/v1/internals/application_controller.rb | 5 +---- app/controllers/api/v1/internals/compliance_check_sets_controller.rb | 4 ++-- app/controllers/api/v1/internals/netex_imports_controller.rb | 2 +- 3 files changed, 4 insertions(+), 7 deletions(-) (limited to 'app/controllers/api') diff --git a/app/controllers/api/v1/internals/application_controller.rb b/app/controllers/api/v1/internals/application_controller.rb index 2a9f2558a..77b74f5f6 100644 --- a/app/controllers/api/v1/internals/application_controller.rb +++ b/app/controllers/api/v1/internals/application_controller.rb @@ -2,16 +2,13 @@ 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 + authenticate_with_http_token { |token| Rails.application.secrets.api_token == token } 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 index 5e9137cb5..db92c3fad 100644 --- a/app/controllers/api/v1/internals/compliance_check_sets_controller.rb +++ b/app/controllers/api/v1/internals/compliance_check_sets_controller.rb @@ -1,7 +1,7 @@ module Api module V1 module Internals - class ComplianceCheckSetsController < Api::V1::Internals::ApplicationController + class ComplianceCheckSetsController < ApplicationController include ControlFlow def validated @@ -19,7 +19,7 @@ module Api def notify_parent find_compliance_check_set - if @compliance_check_set.notify_parent + if @compliance_check_set.notify_parent && @compliance_check_set.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)}" diff --git a/app/controllers/api/v1/internals/netex_imports_controller.rb b/app/controllers/api/v1/internals/netex_imports_controller.rb index c8e33f7b8..89bc1b81d 100644 --- a/app/controllers/api/v1/internals/netex_imports_controller.rb +++ b/app/controllers/api/v1/internals/netex_imports_controller.rb @@ -1,7 +1,7 @@ module Api module V1 module Internals - class NetexImportsController < Api::V1::Internals::ApplicationController + class NetexImportsController < ApplicationController include ControlFlow def create -- cgit v1.2.3