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