From 5ea64aed0291b7e5d8a91cfa76f858661b97849b Mon Sep 17 00:00:00 2001 From: Thor77 Date: Thu, 10 Oct 2019 19:30:49 +0200 Subject: [PATCH] Add /tournaments/:id/stages endpoint --- app/controllers/stages_controller.rb | 28 ++++++++++++++++++++++++++++ config/routes.rb | 1 + 2 files changed, 29 insertions(+) diff --git a/app/controllers/stages_controller.rb b/app/controllers/stages_controller.rb index cdc5f1b6..53d10fd8 100644 --- a/app/controllers/stages_controller.rb +++ b/app/controllers/stages_controller.rb @@ -4,6 +4,7 @@ class StagesController < ApplicationController before_action :set_stage, only: %i[show update] before_action :authenticate_user!, only: %i[update] before_action -> { require_owner! @stage.owner }, only: %i[update] + rescue_from ActiveRecord::RecordNotFound, with: :render_not_found_error # GET /stages/1 def show @@ -35,6 +36,29 @@ def update end end + def index + tournament = Tournament.find(params[:tournament_id]) + level_param = index_params[:level] + if level_param + if level_param == 'current' + # TODO: find current stage + elsif level_param == 'group' + group_stage = tournament.stages.find_by(level: -1) + if group_stage + render json: group_stage + else + render json: { error: 'There\'s no group stage for this tournament' }, status: :not_found + end + elsif level_param.to_i.to_s == level_param + render json: tournament.stages.find_by!(level: level_param.to_i) + else + render json: { error: 'Invalid level parameter' }, status: unprocessable_entity + end + else + render json: tournament.stages unless level_param + end + end + private def handle_group_stage_end @@ -59,4 +83,8 @@ def set_stage def stage_params params.slice(:state).permit! end + + def index_params + params.permit(:level) + end end diff --git a/config/routes.rb b/config/routes.rb index 61901bd6..329de5ed 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,6 +13,7 @@ resources :teams, only: %i[show update] resources :tournaments do resources :statistics, only: %i[index] + resources :stages, only: %i[index] end resources :match_scores, only: %i[show update] resources :groups, only: %i[show]