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.
make agents routes work for /Agent and /agent
- Loading branch information
1 parent
76db662
commit 49d4ab4
Showing
1 changed file
with
70 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,90 @@ | ||
class AgentsController < ApplicationController | ||
|
||
## | ||
# Ontology agents | ||
get "/ontologies/:acronym/agents" do | ||
end | ||
|
||
namespace "/agents" 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 | ||
%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 | ||
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 | ||
create_agent | ||
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 | ||
put '/:acronym' do | ||
create_agent | ||
end | ||
# Create a agent with the given acronym | ||
post do | ||
create_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 | ||
# Create a agent with the given acronym | ||
put '/:acronym' do | ||
create_agent | ||
end | ||
|
||
if agent.nil? | ||
error 400, "Agent does not exist, please create using HTTP PUT before modifying" | ||
else | ||
populate_from_params(agent, params) | ||
# 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.valid? | ||
agent.save | ||
if agent.nil? | ||
error 400, "Agent does not exist, please create using HTTP PUT before modifying" | ||
else | ||
error 400, agent.errors | ||
populate_from_params(agent, params) | ||
|
||
if agent.valid? | ||
agent.save | ||
else | ||
error 400, agent.errors | ||
end | ||
end | ||
halt 204 | ||
end | ||
halt 204 | ||
end | ||
|
||
# Delete a agent | ||
delete '/:id' do | ||
agent = LinkedData::Models::Agent.find(params["id"]).first | ||
agent.delete | ||
halt 204 | ||
end | ||
# Delete a agent | ||
delete '/:id' do | ||
agent = LinkedData::Models::Agent.find(params["id"]).first | ||
agent.delete | ||
halt 204 | ||
end | ||
|
||
private | ||
private | ||
|
||
def create_agent | ||
params ||= @params | ||
acronym = params["id"] | ||
agent = nil | ||
agent = LinkedData::Models::Agent.find(acronym).include(LinkedData::Models::Agent.goo_attrs_to_load(includes_param)).first if acronym | ||
def create_agent | ||
params ||= @params | ||
acronym = params["id"] | ||
agent = nil | ||
agent = LinkedData::Models::Agent.find(acronym).include(LinkedData::Models::Agent.goo_attrs_to_load(includes_param)).first if acronym | ||
|
||
if agent.nil? | ||
agent = instance_from_params(LinkedData::Models::Agent, params) | ||
else | ||
error 400, "Agent exists, please use HTTP PATCH to update" | ||
end | ||
if agent.nil? | ||
agent = instance_from_params(LinkedData::Models::Agent, params) | ||
else | ||
error 400, "Agent exists, please use HTTP PATCH to update" | ||
end | ||
|
||
if agent.valid? | ||
agent.save | ||
else | ||
error 400, agent.errors | ||
if agent.valid? | ||
agent.save | ||
else | ||
error 400, agent.errors | ||
end | ||
reply 201, agent | ||
end | ||
reply 201, agent | ||
|
||
end | ||
end | ||
|
||
end |