forked from ontoportal/ontologies_api
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from ontoportal-lirmm/feature/add-agent-crud-e…
…ndpoint Feature: Add agent crud endpoints
- Loading branch information
1 parent
472677a
commit 20482a6
Showing
9 changed files
with
490 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
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 | ||
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 | ||
end | ||
|
||
# Create a agent with the given acronym | ||
put '/:acronym' do | ||
reply 201, create_new_agent | ||
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 | ||
|
||
if agent.nil? | ||
error 400, "Agent does not exist, please create using HTTP PUT before modifying" | ||
else | ||
agent = update_agent(agent, params) | ||
|
||
error 400, agent.errors unless agent.errors.empty? | ||
end | ||
halt 204 | ||
end | ||
|
||
# Delete a agent | ||
delete '/:id' do | ||
agent = LinkedData::Models::Agent.find(params["id"]).first | ||
agent.delete | ||
halt 204 | ||
end | ||
|
||
private | ||
|
||
def update_identifiers(identifiers) | ||
Array(identifiers).map do |i| | ||
next nil if i.empty? | ||
|
||
id = i["id"] || LinkedData::Models::AgentIdentifier.generate_identifier(i['notation'], i['schemaAgency']) | ||
identifier = LinkedData::Models::AgentIdentifier.find(RDF::URI.new(id)).first | ||
|
||
if identifier | ||
identifier.bring_remaining | ||
else | ||
identifier = LinkedData::Models::AgentIdentifier.new | ||
end | ||
|
||
i.delete "id" | ||
|
||
next identifier if i.keys.size.zero? | ||
|
||
populate_from_params(identifier, i) | ||
|
||
if identifier.valid? | ||
identifier.save | ||
else | ||
error 400, identifier.errors | ||
end | ||
identifier | ||
end.compact | ||
end | ||
|
||
def update_affiliations(affiliations) | ||
Array(affiliations).map do |aff| | ||
affiliation = aff["id"] ? LinkedData::Models::Agent.find(RDF::URI.new(aff["id"])).first : nil | ||
|
||
if affiliation | ||
affiliation.bring_remaining | ||
affiliation.identifiers.each{|i| i.bring_remaining} | ||
end | ||
|
||
next affiliation if aff.keys.size.eql?(1) && aff["id"] | ||
|
||
if affiliation | ||
affiliation = update_agent(affiliation, aff) | ||
else | ||
affiliation = create_new_agent(aff["id"], aff) | ||
end | ||
|
||
error 400, affiliation.errors unless affiliation.errors.empty? | ||
|
||
affiliation | ||
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 | ||
|
||
if agent.nil? | ||
agent = update_agent(LinkedData::Models::Agent.new, params) | ||
error 400, agent.errors unless agent.errors.empty? | ||
|
||
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 | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
require 'sinatra/base' | ||
|
||
module Sinatra | ||
module Helpers | ||
module SubmissionHelper | ||
|
||
def submission_attributes_all | ||
out = [LinkedData::Models::OntologySubmission.embed_values_hash] | ||
out << {:contact=>[:name, :email]} | ||
out << {:ontology=>[:acronym, :name, :administeredBy, :group, :viewingRestriction, :doNotUpdate, :flat, | ||
:hasDomain, :summaryOnly, :acl, :viewOf, :ontologyType]} | ||
|
||
out | ||
end | ||
|
||
def retrieve_submissions(options) | ||
status = (options[:status] || "RDF").to_s.upcase | ||
status = "RDF" if status.eql?("READY") | ||
any = status.eql?("ANY") | ||
include_views = options[:also_include_views] || false | ||
includes, page, size, order_by, _ = settings_params(LinkedData::Models::OntologySubmission) | ||
includes << :submissionStatus unless includes.include?(:submissionStatus) | ||
|
||
submissions_query = LinkedData::Models::OntologySubmission | ||
if any | ||
submissions_query = submissions_query.where | ||
else | ||
submissions_query = submissions_query.where({ submissionStatus: [code: status] }) | ||
end | ||
|
||
submissions_query = apply_submission_filters(submissions_query) | ||
submissions_query = submissions_query.filter(Goo::Filter.new(ontology: [:viewOf]).unbound) unless include_views | ||
submissions_query = submissions_query.filter(filter) if filter? | ||
|
||
# When asking to display all metadata, we are using bring_remaining on each submission. Slower but best way to retrieve all attrs | ||
if includes_param.first == :all | ||
includes = [:submissionId, { :contact => [:name, :email], | ||
:ontology => [:administeredBy, :acronym, :name, :summaryOnly, :ontologyType, :viewingRestriction, :acl, | ||
:group, :hasDomain, :views, :viewOf, :flat, :notes, :reviews, :projects], | ||
:submissionStatus => [:code], :hasOntologyLanguage => [:acronym], :metrics => [:classes, :individuals, :properties] }, | ||
:submissionStatus] | ||
else | ||
if includes.find { |v| v.is_a?(Hash) && v.keys.include?(:ontology) } | ||
includes << { :ontology => [:administeredBy, :acronym, :name, :viewingRestriction, :group, :hasDomain, :notes, :reviews, :projects, :acl, :viewOf] } | ||
end | ||
|
||
if includes.find { |v| v.is_a?(Hash) && v.keys.include?(:contact) } | ||
includes << { :contact => [:name, :email] } | ||
end | ||
end | ||
|
||
submissions = submissions_query.include(includes) | ||
if page? | ||
submissions.page(page, size).all | ||
else | ||
submissions.to_a | ||
end | ||
end | ||
|
||
def include_ready?(options) | ||
options[:status] && options[:status].to_s.upcase.eql?("READY") | ||
end | ||
|
||
end | ||
end | ||
end | ||
|
||
helpers Sinatra::Helpers::SubmissionHelper |
Oops, something went wrong.