Validate update parameters

playoff_teams_amount, instant_finalists_amount and
intermediate_round_participants_amount need to make sense together
This commit is contained in:
Daniel Schädler 2019-06-10 21:39:04 +02:00
parent 36bdfbae28
commit 6a060651a4
1 changed files with 14 additions and 0 deletions

View File

@ -5,6 +5,7 @@ class TournamentsController < ApplicationController
before_action :authenticate_user!, only: %i[create update destroy]
before_action -> { require_owner! @tournament.owner }, only: %i[update destroy]
before_action :validate_create_params, only: %i[create]
before_action :validate_update_params, only: %i[update]
rescue_from ActiveRecord::RecordNotFound, with: :render_not_found_error
# GET /tournaments
@ -113,4 +114,17 @@ class TournamentsController < ApplicationController
render json: { error: 'Invalid teams array' }, status: :unprocessable_entity
end
def validate_update_params
playoff_teams_amount = params['playoff_teams_amount'] || @tournament.playoff_teams_amount
instant_finalists_amount = params['instant_finalists_amount'] || @tournament.instant_finalists_amount
intermediate_round_participants_amount = params['intermediate_round_participants_amount'] ||
@tournament.intermediate_round_participants_amount
return if instant_finalists_amount + (intermediate_round_participants_amount / 2) ==
playoff_teams_amount
render json: {
error: 'playoff_teams_amount, instant_finalists_amount and intermediate_round_participants_amount don\'t match'
}, status: :unprocessable_entity
end
end