From ab38c60a5d8a41c594a682789ef89dc01d5e7ed6 Mon Sep 17 00:00:00 2001 From: Bilel Kihal <61744974+Bilelkihal@users.noreply.github.com> Date: Tue, 23 Jul 2024 15:22:46 +0200 Subject: [PATCH 1/2] fix: remove duplicated agents endpoint ('/Agents') (#85) --- controllers/agents_controller.rb | 222 +++++++++++++++---------------- 1 file changed, 110 insertions(+), 112 deletions(-) diff --git a/controllers/agents_controller.rb b/controllers/agents_controller.rb index 1bf86321..06fcd27c 100644 --- a/controllers/agents_controller.rb +++ b/controllers/agents_controller.rb @@ -1,150 +1,148 @@ class AgentsController < ApplicationController - %w[/agents /Agents].each do |namespace| - namespace namespace do - # Display all agents - get do - check_last_modified_collection(LinkedData::Models::Agent) - query = LinkedData::Models::Agent.where - query = apply_filters(LinkedData::Models::Agent, query) - query = query.include(LinkedData::Models::Agent.goo_attrs_to_load(includes_param)) - if page? - page, size = page_params - agents = query.page(page, size).all - else - agents = query.to_a - end - - if includes_param.include?(:all) || includes_param.include?(:usages) - LinkedData::Models::Agent.load_agents_usages(agents) - end - - reply agents - end - - # Display a single agent - get '/:id' do - check_last_modified_collection(LinkedData::Models::Agent) - id = params["id"] - agent = LinkedData::Models::Agent.find(id).include(LinkedData::Models::Agent.goo_attrs_to_load(includes_param)).first - error 404, "Agent #{id} not found" if agent.nil? - reply 200, agent - end - - # Create a agent with the given acronym - post do - reply 201, create_new_agent + namespace "/agents" do + get do + check_last_modified_collection(LinkedData::Models::Agent) + query = LinkedData::Models::Agent.where + query = apply_filters(LinkedData::Models::Agent, query) + query = query.include(LinkedData::Models::Agent.goo_attrs_to_load(includes_param)) + if page? + page, size = page_params + agents = query.page(page, size).all + else + agents = query.to_a end - # Create a agent with the given acronym - put '/:acronym' do - reply 201, create_new_agent + if includes_param.include?(:all) || includes_param.include?(:usages) + LinkedData::Models::Agent.load_agents_usages(agents) end - # Update an existing submission of a agent - patch '/:id' do - acronym = params["id"] - agent = LinkedData::Models::Agent.find(acronym).include(LinkedData::Models::Agent.attributes).first + reply agents + end - if agent.nil? - error 400, "Agent does not exist, please create using HTTP PUT before modifying" - else - agent = update_agent(agent, params) + # Display a single agent + get '/:id' do + check_last_modified_collection(LinkedData::Models::Agent) + id = params["id"] + agent = LinkedData::Models::Agent.find(id).include(LinkedData::Models::Agent.goo_attrs_to_load(includes_param)).first + error 404, "Agent #{id} not found" if agent.nil? + reply 200, agent + end - error 400, agent.errors unless agent.errors.empty? - end - halt 204 - end + # Create a agent with the given acronym + post do + reply 201, create_new_agent + end - # Delete a agent - delete '/:id' do - agent = LinkedData::Models::Agent.find(params["id"]).first - agent.delete - halt 204 - end + # Create a agent with the given acronym + put '/:acronym' do + reply 201, create_new_agent + end - private + # Update an existing submission of a agent + patch '/:id' do + acronym = params["id"] + agent = LinkedData::Models::Agent.find(acronym).include(LinkedData::Models::Agent.attributes).first - def update_identifiers(identifiers) - Array(identifiers).map do |i| - next nil if i.empty? + if agent.nil? + error 400, "Agent does not exist, please create using HTTP PUT before modifying" + else + agent = update_agent(agent, params) - id = i["id"] || LinkedData::Models::AgentIdentifier.generate_identifier(i['notation'], i['schemaAgency']) - identifier = LinkedData::Models::AgentIdentifier.find(RDF::URI.new(id)).first + error 400, agent.errors unless agent.errors.empty? + end + halt 204 + end - if identifier - identifier.bring_remaining - else - identifier = LinkedData::Models::AgentIdentifier.new - end + # Delete a agent + delete '/:id' do + agent = LinkedData::Models::Agent.find(params["id"]).first + agent.delete + halt 204 + end - i.delete "id" + private - next identifier if i.keys.size.zero? + def update_identifiers(identifiers) + Array(identifiers).map do |i| + next nil if i.empty? - populate_from_params(identifier, i) + id = i["id"] || LinkedData::Models::AgentIdentifier.generate_identifier(i['notation'], i['schemaAgency']) + identifier = LinkedData::Models::AgentIdentifier.find(RDF::URI.new(id)).first - if identifier.valid? - identifier.save - else - error 400, identifier.errors - end - identifier - end.compact - end + if identifier + identifier.bring_remaining + else + identifier = LinkedData::Models::AgentIdentifier.new + end - def update_affiliations(affiliations) - Array(affiliations).map do |aff| - affiliation = aff["id"] ? LinkedData::Models::Agent.find(RDF::URI.new(aff["id"])).first : nil + i.delete "id" - if affiliation - affiliation.bring_remaining - affiliation.identifiers.each{|i| i.bring_remaining} - end + next identifier if i.keys.size.zero? - next affiliation if aff.keys.size.eql?(1) && aff["id"] + populate_from_params(identifier, i) - if affiliation - affiliation = update_agent(affiliation, aff) - else - affiliation = create_new_agent(aff["id"], aff) - end + if identifier.valid? + identifier.save + else + error 400, identifier.errors + end + identifier + end.compact + end - error 400, affiliation.errors unless affiliation.errors.empty? + def update_affiliations(affiliations) + Array(affiliations).map do |aff| + affiliation = aff["id"] ? LinkedData::Models::Agent.find(RDF::URI.new(aff["id"])).first : nil - affiliation + if affiliation + affiliation.bring_remaining + affiliation.identifiers.each{|i| i.bring_remaining} end - end - def create_new_agent (id = @params['id'], params = @params) - agent = nil - agent = LinkedData::Models::Agent.find(id).include(LinkedData::Models::Agent.goo_attrs_to_load(includes_param)).first if id + next affiliation if aff.keys.size.eql?(1) && aff["id"] - if agent.nil? - agent = update_agent(LinkedData::Models::Agent.new, params) - error 400, agent.errors unless agent.errors.empty? - - return agent + if affiliation + affiliation = update_agent(affiliation, aff) else - error 400, "Agent exists, please use HTTP PATCH to update" + affiliation = create_new_agent(aff["id"], aff) end + + error 400, affiliation.errors unless affiliation.errors.empty? + + affiliation end + end - def update_agent(agent, params) - return agent unless agent + def create_new_agent (id = @params['id'], params = @params) + agent = nil + agent = LinkedData::Models::Agent.find(id).include(LinkedData::Models::Agent.goo_attrs_to_load(includes_param)).first if id - identifiers = params.delete "identifiers" - affiliations = params.delete "affiliations" - params.delete "id" - populate_from_params(agent, params) - agent.identifiers = update_identifiers(identifiers) - agent.affiliations = update_affiliations(affiliations) + if agent.nil? + agent = update_agent(LinkedData::Models::Agent.new, params) + error 400, agent.errors unless agent.errors.empty? - agent.save if agent.valid? return agent + else + error 400, "Agent exists, please use HTTP PATCH to update" end + end + def update_agent(agent, params) + return agent unless agent + + identifiers = params.delete "identifiers" + affiliations = params.delete "affiliations" + params.delete "id" + populate_from_params(agent, params) + agent.identifiers = update_identifiers(identifiers) + agent.affiliations = update_affiliations(affiliations) + + agent.save if agent.valid? + return agent end + end -end \ No newline at end of file + +end From 67bc9fb2413f411cbb9d1842293859044123c971 Mon Sep 17 00:00:00 2001 From: Syphax bouazzouni Date: Tue, 23 Jul 2024 16:14:49 +0200 Subject: [PATCH 2/2] Feature: implement ontology agents endpoint (#84) * implement ontology agents endpoint * Move ontology agents method out of agents namespace in agents_controller * return a list of uniq values, for the endpoint '/ontologies/:acronym/agents' that contains all the agents of the ontology using agents_attrs list * test for ontology agents endpoing * add another ontologyin test ontology agents test, and assert only the number of results and the names --------- Co-authored-by: Bilel KIHAL --- Gemfile.lock | 2 +- controllers/agents_controller.rb | 24 ++++++++- .../controllers/test_ontologies_controller.rb | 50 +++++++++++++++++++ 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 023fd1b4..ccb0b5ee 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -57,7 +57,7 @@ GIT GIT remote: https://github.com/ontoportal-lirmm/ontologies_linked_data.git - revision: a5b56a68e6dc8ecfc9db708d44350342dac38ce6 + revision: fd78d689dac4a7393e20a36ac930c6c9d191a619 branch: development specs: ontologies_linked_data (0.0.1) diff --git a/controllers/agents_controller.rb b/controllers/agents_controller.rb index 06fcd27c..6b69fbc5 100644 --- a/controllers/agents_controller.rb +++ b/controllers/agents_controller.rb @@ -1,5 +1,27 @@ class AgentsController < ApplicationController + get '/ontologies/:acronym/agents' do + ont = Ontology.find(params["acronym"]).first + latest = ont.latest_submission(status: :any) + latest.bring(*OntologySubmission.agents_attrs) + properties_agents= {} + OntologySubmission.agents_attrs.each do |attr| + properties_agents[attr] = Array(latest.send(attr)) + end + + agents = [] + properties_agents.each do |key, value| + agents.concat(value.map{ |agent| agent.bring_remaining}) + end + agents.uniq! + + if includes_param.include?(:all) || includes_param.include?(:usages) + LinkedData::Models::Agent.load_agents_usages(agents) + end + + reply agents + end + namespace "/agents" do get do check_last_modified_collection(LinkedData::Models::Agent) @@ -143,6 +165,4 @@ def update_agent(agent, params) end end - - end diff --git a/test/controllers/test_ontologies_controller.rb b/test/controllers/test_ontologies_controller.rb index ad062742..681ab93b 100644 --- a/test/controllers/test_ontologies_controller.rb +++ b/test/controllers/test_ontologies_controller.rb @@ -282,6 +282,46 @@ def test_detach_a_view assert_equal onto["viewOf"], ont.id.to_s end + def test_ontology_agents + ontologies_and_submissions = create_ontologies_and_submissions(ont_count: 2, submission_count: 1, process_submission: true) + submission1 = ontologies_and_submissions[2].first.submissions.last + submission2 = ontologies_and_submissions[2].last.submissions.last + + ontology_acronym1 = ontologies_and_submissions[1].first + ontology_acronym2 = ontologies_and_submissions[1].last + + submission1.bring(*OntologySubmission.agents_attrs) + submission2.bring(*OntologySubmission.agents_attrs) + + # To insure that we don't have duplicated agents in the response + agent_syphax = _create_agent(name: 'Syphax', type: 'person') + + submission1.publisher = [_create_agent(name: 'Bilel', type: 'person'), agent_syphax] + submission1.hasContributor = [_create_agent(name: 'Clement', type: 'person'), agent_syphax] + + submission2.publisher = [_create_agent(name: 'Imad', type: 'person'), _create_agent(name: 'Serine', type: 'person')] + + submission1.save + submission2.save + + + get "/ontologies/#{ontology_acronym1}/agents" + + response = MultiJson.load(last_response.body) + assert_equal response.length, 3 + response.each do |r| + assert_includes ['Bilel', 'Syphax', 'Clement'], r["name"] + end + + get "/ontologies/#{ontology_acronym2}/agents" + + response = MultiJson.load(last_response.body) + assert_equal response.length, 2 + response.each do |r| + assert_includes ['Imad', 'Serine'], r["name"] + end + end + private def check400(response) @@ -289,4 +329,14 @@ def check400(response) assert MultiJson.load(response.body)["errors"] end + def _create_agent(name: 'name', type: 'person') + agent = LinkedData::Models::Agent.new({ + agentType: type, + name: name, + creator: User.find('tim').first + }) + agent.save + agent + end + end