Merge pull request #1480 from AlexDenisov/api_attrs_refactoring

API attributes refactoring
This commit is contained in:
Dmitriy Zaporozhets 2012-09-16 12:27:48 -07:00
commit eed1b52f18
4 changed files with 36 additions and 52 deletions

View file

@ -28,6 +28,14 @@ module Gitlab
end
end
def attributes_for_keys(keys)
attrs = {}
keys.each do |key|
attrs[key] = params[key] if params[key].present?
end
attrs
end
# error helpers
def forbidden!

View file

@ -48,15 +48,10 @@ module Gitlab
# Example Request:
# POST /projects/:id/issues
post ":id/issues" do
@issue = user_project.issues.new(
title: params[:title],
description: params[:description],
assignee_id: params[:assignee_id],
milestone_id: params[:milestone_id],
label_list: params[:labels]
)
attrs = attributes_for_keys [:title, :description, :assignee_id, :milestone_id]
attrs[:label_list] = params[:labels] if params[:labels].present?
@issue = user_project.issues.new attrs
@issue.author = current_user
if @issue.save
present @issue, with: Entities::Issue
else
@ -81,16 +76,9 @@ module Gitlab
@issue = user_project.issues.find(params[:issue_id])
authorize! :modify_issue, @issue
parameters = {
title: (params[:title] || @issue.title),
description: (params[:description] || @issue.description),
assignee_id: (params[:assignee_id] || @issue.assignee_id),
milestone_id: (params[:milestone_id] || @issue.milestone_id),
label_list: (params[:labels] || @issue.label_list),
closed: (params[:closed] || @issue.closed)
}
if @issue.update_attributes(parameters)
attrs = attributes_for_keys [:title, :description, :assignee_id, :milestone_id, :closed]
attrs[:label_list] = params[:labels] if params[:labels].present?
if @issue.update_attributes attrs
present @issue, with: Entities::Issue
else
not_found!

View file

@ -36,12 +36,8 @@ module Gitlab
# Example Request:
# POST /projects/:id/milestones
post ":id/milestones" do
@milestone = user_project.milestones.new(
title: params[:title],
description: params[:description],
due_date: params[:due_date]
)
attrs = attributes_for_keys [:title, :description, :due_date]
@milestone = user_project.milestones.new attrs
if @milestone.save
present @milestone, with: Entities::Milestone
else
@ -64,14 +60,8 @@ module Gitlab
authorize! :admin_milestone, user_project
@milestone = user_project.milestones.find(params[:milestone_id])
parameters = {
title: (params[:title] || @milestone.title),
description: (params[:description] || @milestone.description),
due_date: (params[:due_date] || @milestone.due_date),
closed: (params[:closed] || @milestone.closed)
}
if @milestone.update_attributes(parameters)
attrs = attributes_for_keys [:title, :description, :due_date, :closed]
if @milestone.update_attributes attrs
present @milestone, with: Entities::Milestone
else
not_found!

View file

@ -40,13 +40,16 @@ module Gitlab
post do
params[:code] ||= params[:name]
params[:path] ||= params[:name]
project_attrs = {}
params.each_pair do |k ,v|
if Project.attribute_names.include? k
project_attrs[k] = v
end
end
@project = Project.create_by_user(project_attrs, current_user)
attrs = attributes_for_keys [:code,
:path,
:name,
:description,
:default_branch,
:issues_enabled,
:wall_enabled,
:merge_requests_enabled,
:wiki_enabled]
@project = Project.create_by_user(attrs, current_user)
if @project.saved?
present @project, with: Entities::Project
else
@ -204,12 +207,10 @@ module Gitlab
# Example Request:
# POST /projects/:id/snippets
post ":id/snippets" do
@snippet = user_project.snippets.new(
title: params[:title],
file_name: params[:file_name],
expires_at: params[:lifetime],
content: params[:code]
)
attrs = attributes_for_keys [:title, :file_name]
attrs[:expires_at] = params[:lifetime] if params[:lifetime].present?
attrs[:content] = params[:code] if params[:code].present?
@snippet = user_project.snippets.new attrs
@snippet.author = current_user
if @snippet.save
@ -234,14 +235,11 @@ module Gitlab
@snippet = user_project.snippets.find(params[:snippet_id])
authorize! :modify_snippet, @snippet
parameters = {
title: (params[:title] || @snippet.title),
file_name: (params[:file_name] || @snippet.file_name),
expires_at: (params[:lifetime] || @snippet.expires_at),
content: (params[:code] || @snippet.content)
}
attrs = attributes_for_keys [:title, :file_name]
attrs[:expires_at] = params[:lifetime] if params[:lifetime].present?
attrs[:content] = params[:code] if params[:code].present?
if @snippet.update_attributes(parameters)
if @snippet.update_attributes attrs
present @snippet, with: Entities::ProjectSnippet
else
not_found!