GrapeDSL for variables

This commit is contained in:
Z.J. van de Weg 2016-10-12 20:38:33 +02:00
parent 5aa7b13c2e
commit 3d6f18cec5

View file

@ -4,27 +4,29 @@ module API
before { authenticate! } before { authenticate! }
before { authorize! :admin_build, user_project } before { authorize! :admin_build, user_project }
params do
requires :id, type: String, desc: 'The ID of a project'
end
resource :projects do resource :projects do
# Get project variables desc 'Get project variables' do
# success Entities::Variable
# Parameters: end
# id (required) - The ID of a project params do
# page (optional) - The page number for pagination optional :page, type: Integer, desc: 'The page number for pagination'
# per_page (optional) - The value of items per page to show optional :per_page, type: Integer, desc: 'The value of items per page to show'
# Example Request: end
# GET /projects/:id/variables
get ':id/variables' do get ':id/variables' do
variables = user_project.variables variables = user_project.variables
present paginate(variables), with: Entities::Variable present paginate(variables), with: Entities::Variable
end end
# Get specific variable of a project desc 'Get a specific variable from a project' do
# success Entities::Variable
# Parameters: end
# id (required) - The ID of a project params do
# key (required) - The `key` of variable requires :key, type: String, desc: 'The key of the variable'
# Example Request: end
# GET /projects/:id/variables/:key
get ':id/variables/:key' do get ':id/variables/:key' do
key = params[:key] key = params[:key]
variable = user_project.variables.find_by(key: key.to_s) variable = user_project.variables.find_by(key: key.to_s)
@ -34,18 +36,15 @@ module API
present variable, with: Entities::Variable present variable, with: Entities::Variable
end end
# Create a new variable in project desc 'Create a new variable in a project' do
# success Entities::Variable
# Parameters: end
# id (required) - The ID of a project params do
# key (required) - The key of variable requires :key, type: String, desc: 'The key of the variable'
# value (required) - The value of variable requires :value, type: String, desc: 'The value of the variable'
# Example Request: end
# POST /projects/:id/variables
post ':id/variables' do post ':id/variables' do
required_attributes! [:key, :value] variable = user_project.variables.create(declared(params, include_parent_namespaces: false).to_h)
variable = user_project.variables.create(key: params[:key], value: params[:value])
if variable.valid? if variable.valid?
present variable, with: Entities::Variable present variable, with: Entities::Variable
@ -54,41 +53,37 @@ module API
end end
end end
# Update existing variable of a project desc 'Update an existing variable from a project' do
# success Entities::Variable
# Parameters: end
# id (required) - The ID of a project params do
# key (optional) - The `key` of variable optional :key, type: String, desc: 'The key of the variable'
# value (optional) - New value for `value` field of variable optional :value, type: String, desc: 'The value of the variable'
# Example Request: end
# PUT /projects/:id/variables/:key
put ':id/variables/:key' do put ':id/variables/:key' do
variable = user_project.variables.find_by(key: params[:key].to_s) variable = user_project.variables.find_by(key: params[:key])
return not_found!('Variable') unless variable return not_found!('Variable') unless variable
attrs = attributes_for_keys [:value] if variable.update(value: params[:value])
if variable.update(attrs)
present variable, with: Entities::Variable present variable, with: Entities::Variable
else else
render_validation_error!(variable) render_validation_error!(variable)
end end
end end
# Delete existing variable of a project desc 'Delete an existing variable from a project' do
# success Entities::Variable
# Parameters: end
# id (required) - The ID of a project params do
# key (required) - The ID of a variable requires :key, type: String, desc: 'The key of the variable'
# Example Request: end
# DELETE /projects/:id/variables/:key
delete ':id/variables/:key' do delete ':id/variables/:key' do
variable = user_project.variables.find_by(key: params[:key].to_s) variable = user_project.variables.find_by(key: params[:key])
return not_found!('Variable') unless variable return not_found!('Variable') unless variable
variable.destroy
present variable, with: Entities::Variable present variable.destroy, with: Entities::Variable
end end
end end
end end