Simplify controller code
Matches update now gets rolled back via a Transaction
This commit is contained in:
parent
15ff7bb09c
commit
10ade8efb9
|
|
@ -13,27 +13,37 @@ class MatchesController < ApplicationController
|
||||||
# PATCH/PUT /matches/1
|
# PATCH/PUT /matches/1
|
||||||
def update
|
def update
|
||||||
new_state = match_params['state']
|
new_state = match_params['state']
|
||||||
if new_state == 'finished' && @match.current_leading_team.nil?
|
|
||||||
render json: { error: 'Stopping undecided Matches isn\'t allowed in playoff stage' },
|
Match.transaction do
|
||||||
status: :unprocessable_entity
|
if @match.update(match_params)
|
||||||
return
|
handle_match_end if new_state == 'finished'
|
||||||
end
|
|
||||||
if @match.update(match_params)
|
render json: @match
|
||||||
if new_state == 'finished'
|
else
|
||||||
result = PopulateMatchBelowAndSave.call(match: @match) unless @match.group_match?
|
render json: @match.errors, status: :unprocessable_entity
|
||||||
unless result.success?
|
raise ActiveRecord::Rollback
|
||||||
render json: { error: 'Moving Team one stage down failed' }, status: :unprocessable_entity
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
render json: @match
|
|
||||||
else
|
|
||||||
render json: @match.errors, status: :unprocessable_entity
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def handle_match_end
|
||||||
|
return if @match.group_match?
|
||||||
|
|
||||||
|
if @match.winner.nil?
|
||||||
|
render json: { error: 'Stopping undecided Matches isn\'t allowed in playoff stage' },
|
||||||
|
status: :unprocessable_entity
|
||||||
|
raise ActiveRecord::Rollback
|
||||||
|
end
|
||||||
|
|
||||||
|
return if PopulateMatchBelowAndSave.call(match: @match).success?
|
||||||
|
|
||||||
|
render json: { error: 'Moving Team one stage down failed' },
|
||||||
|
status: :unprocessable_entity
|
||||||
|
raise ActiveRecord::Rollback
|
||||||
|
end
|
||||||
|
|
||||||
def validate_params
|
def validate_params
|
||||||
case match_params['state']
|
case match_params['state']
|
||||||
when 'in_progress'
|
when 'in_progress'
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue