diff --git a/app/controllers/api/v0/dmpopidor/madmp_fragments_controller.rb b/app/controllers/api/v0/dmpopidor/madmp_fragments_controller.rb index 887f254..aaaeadb 100644 --- a/app/controllers/api/v0/dmpopidor/madmp_fragments_controller.rb +++ b/app/controllers/api/v0/dmpopidor/madmp_fragments_controller.rb @@ -1,39 +1,35 @@ # frozen_string_literal: true -require 'jsonpath' +require "jsonpath" class Api::V0::Dmpopidor::MadmpFragmentsController < Api::V0::BaseController - before_action :authenticate - def show - @fragment = MadmpFragment.find(params[:id]) - # check if the user has permissions to use the templates API - unless Api::V0::Dmpopidor::MadmpFragmentPolicy.new(@user, @fragment).show? - raise Pundit::NotAuthorizedError - end + before_action :authenticate - fragment_data = nil - if query_params[:mode] == "slim" - fragment_data = @fragment.data - else - fragment_data = @fragment.get_full_fragment - end - - fragment_data = select_property(fragment_data, query_params[:property]) - - respond_with fragment_data + def show + @fragment = MadmpFragment.find(params[:id]) + # check if the user has permissions to use the templates API + unless Api::V0::Dmpopidor::MadmpFragmentPolicy.new(@user, @fragment).show? + raise Pundit::NotAuthorizedError end + fragment_data = query_params[:mode] == "slim" ? @fragment.data : @fragment.get_full_fragment - private - - def select_property(fragment_data, property_name) - if property_name.present? - fragment_data = JsonPath.on(fragment_data, "$..#{property_name}") - end - fragment_data - end + fragment_data = select_property(fragment_data, query_params[:property]) - def query_params - params.permit(:mode, :property) + respond_with fragment_data + end + + private + + def select_property(fragment_data, property_name) + if property_name.present? + fragment_data = JsonPath.on(fragment_data, "$..#{property_name}") end -end \ No newline at end of file + fragment_data + end + + def query_params + params.permit(:mode, :property) + end + +end diff --git a/app/controllers/api/v0/dmpopidor/plans_controller.rb b/app/controllers/api/v0/dmpopidor/plans_controller.rb index 2427fc5..4ff87ab 100644 --- a/app/controllers/api/v0/dmpopidor/plans_controller.rb +++ b/app/controllers/api/v0/dmpopidor/plans_controller.rb @@ -1,34 +1,37 @@ # frozen_string_literal: true class Api::V0::Dmpopidor::PlansController < Api::V0::BaseController - before_action :authenticate - def show - @plan = Plan.find(params[:id]) - @plan_fragment = @plan.json_fragment.dup - research_output_id = query_params[:research_output_id] ? query_params[:research_output_id].to_i : nil - # check if the user has permissions to use the API - unless Api::V0::Dmpopidor::PlanPolicy.new(@user, @plan).show? - raise Pundit::NotAuthorizedError - end - @plan_fragment = select_research_output(@plan_fragment, research_output_id) - if query_params[:mode] == "slim" - respond_with @plan_fragment.data - else - respond_with @plan_fragment.get_full_fragment - end - ends + before_action :authenticate - private - - def select_research_output(plan_fragment, research_output_id) - if research_output_id.present? - plan_fragment.data["research_outputs"] = plan_fragment.data["research_outputs"].select { |r| r == { "dbid" => research_output_id } } - end - plan_fragment + def show + @plan = Plan.find(params[:id]) + @plan_fragment = @plan.json_fragment.dup + research_output_id = query_params[:research_output_id] ? query_params[:research_output_id].to_i : nil + # check if the user has permissions to use the API + unless Api::V0::Dmpopidor::PlanPolicy.new(@user, @plan).show? + raise Pundit::NotAuthorizedError end - - def query_params - params.permit(:mode, :research_output_id) + + @plan_fragment = select_research_output(@plan_fragment, research_output_id) + if query_params[:mode] == "slim" + respond_with @plan_fragment.data + else + respond_with @plan_fragment.get_full_fragment end -end \ No newline at end of file + end + + private + + def select_research_output(plan_fragment, research_output_id) + if research_output_id.present? + plan_fragment.data["research_outputs"] = plan_fragment.data["research_outputs"].select { |r| r == { "dbid" => research_output_id } } + end + plan_fragment + end + + def query_params + params.permit(:mode, :research_output_id) + end + +end