Skip to content

Commit e80313f

Browse files
committed
Conditionally destroy a ressource
1 parent 998afa5 commit e80313f

24 files changed

+71
-114
lines changed

lib/api/access_requests.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,12 @@ class AccessRequests < Grape::API
6767
end
6868
delete ":id/access_requests/:user_id" do
6969
source = find_source(source_type, params[:id])
70-
member = source.public_send(:requesters).find_by!(user_id: params[:user_id])
70+
member = source.requesters.find_by!(user_id: params[:user_id])
7171

72-
check_unmodified_since(member.updated_at)
73-
74-
status 204
75-
::Members::DestroyService.new(source, current_user, params)
76-
.execute(:requesters)
72+
destroy_conditionally!(member) do
73+
::Members::DestroyService.new(source, current_user, params)
74+
.execute(:requesters)
75+
end
7776
end
7877
end
7978
end

lib/api/award_emoji.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,10 @@ class AwardEmoji < Grape::API
8585
end
8686
delete "#{endpoint}/:award_id" do
8787
award = awardable.award_emoji.find(params[:award_id])
88-
check_unmodified_since(award.updated_at)
8988

9089
unauthorized! unless award.user == current_user || current_user.admin?
9190

92-
status 204
93-
award.destroy
91+
destroy_conditionally!(award)
9492
end
9593
end
9694
end

lib/api/boards.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,13 @@ def board_lists
122122
end
123123
delete "/lists/:list_id" do
124124
authorize!(:admin_list, user_project)
125-
126125
list = board_lists.find(params[:list_id])
127-
check_unmodified_since(list.updated_at)
128-
129-
service = ::Boards::Lists::DestroyService.new(user_project, current_user)
130126

131-
unless service.execute(list)
132-
render_api_error!({ error: 'List could not be deleted!' }, 400)
127+
destroy_conditionally!(list) do |list|
128+
service = ::Boards::Lists::DestroyService.new(user_project, current_user)
129+
unless service.execute(list)
130+
render_api_error!({ error: 'List could not be deleted!' }, 400)
131+
end
133132
end
134133
end
135134
end

lib/api/broadcast_messages.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,8 @@ def find_message
9090
end
9191
delete ':id' do
9292
message = find_message
93-
check_unmodified_since(message.updated_at)
9493

95-
status 204
96-
message.destroy
94+
destroy_conditionally!(message)
9795
end
9896
end
9997
end

lib/api/deploy_keys.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,7 @@ class DeployKeys < Grape::API
125125
key = user_project.deploy_keys_projects.find_by(deploy_key_id: params[:key_id])
126126
not_found!('Deploy Key') unless key
127127

128-
check_unmodified_since(key.updated_at)
129-
130-
status 204
131-
key.destroy
128+
destroy_conditionally!(key)
132129
end
133130
end
134131
end

lib/api/environments.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,8 @@ class Environments < Grape::API
7878
authorize! :update_environment, user_project
7979

8080
environment = user_project.environments.find(params[:environment_id])
81-
check_unmodified_since(environment.updated_at)
8281

83-
status 204
84-
environment.destroy
82+
destroy_conditionally!(environment)
8583
end
8684

8785
desc 'Stops an existing environment' do

lib/api/groups.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,10 @@ def present_groups(groups, options = {})
117117
delete ":id" do
118118
group = find_group!(params[:id])
119119
authorize! :admin_group, group
120-
121-
check_unmodified_since(group.updated_at)
122120

123-
status 204
124-
::Groups::DestroyService.new(group, current_user).execute
121+
destroy_conditionally!(group) do |group|
122+
::Groups::DestroyService.new(group, current_user).execute
123+
end
125124
end
126125

127126
desc 'Get a list of projects in this group.' do

lib/api/helpers.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,25 @@ def declared_params(options = {})
1111
declared(params, options).to_h.symbolize_keys
1212
end
1313

14-
def check_unmodified_since(last_modified)
15-
if_unmodified_since = Time.parse(headers['If-Unmodified-Since']) if headers.key?('If-Unmodified-Since') rescue nil
14+
def check_unmodified_since!(last_modified)
15+
if_unmodified_since = Time.parse(headers['If-Unmodified-Since']) rescue nil
1616

17-
if if_unmodified_since && if_unmodified_since < last_modified
17+
if if_unmodified_since && last_modified > if_unmodified_since
1818
render_api_error!('412 Precondition Failed', 412)
1919
end
2020
end
2121

22+
def destroy_conditionally!(resource, last_update_field: :updated_at)
23+
check_unmodified_since!(resource.public_send(last_update_field))
24+
25+
status 204
26+
if block_given?
27+
yield resource
28+
else
29+
resource.destroy
30+
end
31+
end
32+
2233
def current_user
2334
return @current_user if defined?(@current_user)
2435

lib/api/issues.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,8 @@ def find_issues(args = {})
230230
not_found!('Issue') unless issue
231231

232232
authorize!(:destroy_issue, issue)
233-
check_unmodified_since(issue.updated_at)
234233

235-
status 204
236-
issue.destroy
234+
destroy_conditionally!(issue)
237235
end
238236

239237
desc 'List merge requests closing issue' do

lib/api/labels.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ class Labels < Grape::API
5656
label = user_project.labels.find_by(title: params[:name])
5757
not_found!('Label') unless label
5858

59-
check_unmodified_since(label.updated_at)
60-
61-
status 204
62-
label.destroy
59+
destroy_conditionally!(label)
6360
end
6461

6562
desc 'Update an existing label. At least one optional parameter is required.' do

0 commit comments

Comments
 (0)