From 97280df139bba1c03346c4509b7edc0ed70e70e2 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Wed, 29 May 2019 16:27:22 +0200 Subject: [PATCH] first try --- app/controllers/matches_controller.rb | 32 ++++++++++++++------------- app/services/playoff_stage_service.rb | 2 +- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb index b978852..6da2fb9 100644 --- a/app/controllers/matches_controller.rb +++ b/app/controllers/matches_controller.rb @@ -13,27 +13,29 @@ class MatchesController < ApplicationController # PATCH/PUT /matches/1 def update 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' }, - status: :unprocessable_entity - return - end - if @match.update(match_params) - if new_state == 'finished' - result = PopulateMatchBelowAndSave.call(match: @match) unless @match.group_match? - unless result.success? - render json: { error: 'Moving Team one stage down failed' }, status: :unprocessable_entity - return - end + + if new_state == 'finished' + if @match.current_leading_team.nil? # TODO: handle group matches differently + return render_unprocessable_entity(error: 'Stopping undecided Matches isn\'t allowed in playoff stage') + end + + unless @match.group_match? + result = PopulateMatchBelowAndSave.call(match: @match) + return render_unprocessable_entity(error: 'Moving Team one stage down failed') unless result.success? end - render json: @match - else - render json: @match.errors, status: :unprocessable_entity end + + return render json: @match if @match.update(match_params) + + render_unprocessable_entity(@match.errors) end private + def render_unprocessable_entity(error_message) + render json: error_message, status: :unprocessable_entity + end + def validate_params case match_params['state'] when 'in_progress' diff --git a/app/services/playoff_stage_service.rb b/app/services/playoff_stage_service.rb index 38ccabc..3821caf 100644 --- a/app/services/playoff_stage_service.rb +++ b/app/services/playoff_stage_service.rb @@ -143,7 +143,7 @@ class PlayoffStageService def get_winners_of(companion_match, current_match) matches = [current_match, companion_match].sort_by(&:position) - matches.map(&:winner) + matches.map(&:current_leading_team) end end end