2014-09-02 18:07:02 +05:30
|
|
|
module API
|
|
|
|
# Projects API
|
|
|
|
class ProjectSnippets < Grape::API
|
|
|
|
before { authenticate! }
|
|
|
|
|
|
|
|
resource :projects do
|
|
|
|
helpers do
|
|
|
|
def handle_project_member_errors(errors)
|
|
|
|
if errors[:project_access].any?
|
|
|
|
error!(errors[:project_access], 422)
|
|
|
|
end
|
|
|
|
not_found!
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
def snippets_for_current_user
|
|
|
|
finder_params = { filter: :by_project, project: user_project }
|
|
|
|
SnippetsFinder.new.execute(current_user, finder_params)
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Get a project snippets
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id/snippets
|
|
|
|
get ":id/snippets" do
|
2016-06-02 11:05:42 +05:30
|
|
|
present paginate(snippets_for_current_user), with: Entities::ProjectSnippet
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Get a project snippet
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# snippet_id (required) - The ID of a project snippet
|
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id/snippets/:snippet_id
|
|
|
|
get ":id/snippets/:snippet_id" do
|
2016-06-02 11:05:42 +05:30
|
|
|
@snippet = snippets_for_current_user.find(params[:snippet_id])
|
2014-09-02 18:07:02 +05:30
|
|
|
present @snippet, with: Entities::ProjectSnippet
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create a new project snippet
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# title (required) - The title of a snippet
|
|
|
|
# file_name (required) - The name of a snippet file
|
|
|
|
# code (required) - The content of a snippet
|
2015-04-26 12:48:37 +05:30
|
|
|
# visibility_level (required) - The snippet's visibility
|
2014-09-02 18:07:02 +05:30
|
|
|
# Example Request:
|
|
|
|
# POST /projects/:id/snippets
|
|
|
|
post ":id/snippets" do
|
2015-09-11 14:41:01 +05:30
|
|
|
authorize! :create_project_snippet, user_project
|
2015-04-26 12:48:37 +05:30
|
|
|
required_attributes! [:title, :file_name, :code, :visibility_level]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
attrs = attributes_for_keys [:title, :file_name, :visibility_level]
|
2014-09-02 18:07:02 +05:30
|
|
|
attrs[:content] = params[:code] if params[:code].present?
|
2015-04-26 12:48:37 +05:30
|
|
|
@snippet = CreateSnippetService.new(user_project, current_user,
|
|
|
|
attrs).execute
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
if @snippet.errors.any?
|
|
|
|
render_validation_error!(@snippet)
|
2014-09-02 18:07:02 +05:30
|
|
|
else
|
2015-04-26 12:48:37 +05:30
|
|
|
present @snippet, with: Entities::ProjectSnippet
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Update an existing project snippet
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# snippet_id (required) - The ID of a project snippet
|
|
|
|
# title (optional) - The title of a snippet
|
|
|
|
# file_name (optional) - The name of a snippet file
|
|
|
|
# code (optional) - The content of a snippet
|
2015-04-26 12:48:37 +05:30
|
|
|
# visibility_level (optional) - The snippet's visibility
|
2014-09-02 18:07:02 +05:30
|
|
|
# Example Request:
|
|
|
|
# PUT /projects/:id/snippets/:snippet_id
|
|
|
|
put ":id/snippets/:snippet_id" do
|
2016-06-02 11:05:42 +05:30
|
|
|
@snippet = snippets_for_current_user.find(params[:snippet_id])
|
2015-09-11 14:41:01 +05:30
|
|
|
authorize! :update_project_snippet, @snippet
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
attrs = attributes_for_keys [:title, :file_name, :visibility_level]
|
2014-09-02 18:07:02 +05:30
|
|
|
attrs[:content] = params[:code] if params[:code].present?
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
UpdateSnippetService.new(user_project, current_user, @snippet,
|
|
|
|
attrs).execute
|
|
|
|
if @snippet.errors.any?
|
|
|
|
render_validation_error!(@snippet)
|
2014-09-02 18:07:02 +05:30
|
|
|
else
|
2015-04-26 12:48:37 +05:30
|
|
|
present @snippet, with: Entities::ProjectSnippet
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Delete a project snippet
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# snippet_id (required) - The ID of a project snippet
|
|
|
|
# Example Request:
|
|
|
|
# DELETE /projects/:id/snippets/:snippet_id
|
|
|
|
delete ":id/snippets/:snippet_id" do
|
|
|
|
begin
|
2016-06-02 11:05:42 +05:30
|
|
|
@snippet = snippets_for_current_user.find(params[:snippet_id])
|
2015-09-11 14:41:01 +05:30
|
|
|
authorize! :update_project_snippet, @snippet
|
2014-09-02 18:07:02 +05:30
|
|
|
@snippet.destroy
|
|
|
|
rescue
|
2015-04-26 12:48:37 +05:30
|
|
|
not_found!('Snippet')
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get a raw project snippet
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# snippet_id (required) - The ID of a project snippet
|
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id/snippets/:snippet_id/raw
|
|
|
|
get ":id/snippets/:snippet_id/raw" do
|
2016-06-02 11:05:42 +05:30
|
|
|
@snippet = snippets_for_current_user.find(params[:snippet_id])
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
env['api.format'] = :txt
|
|
|
|
content_type 'text/plain'
|
|
|
|
present @snippet.content
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|