Skip to content
This repository has been archived by the owner on May 17, 2022. It is now read-only.

WIP: Add /tournaments/:id/stages endpoint #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions app/controllers/stages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -35,6 +36,29 @@ def update
end
end

def index

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/CyclomaticComplexity: Cyclomatic complexity for index is too high. [7/6]
Metrics/PerceivedComplexity: Perceived complexity for index is too high. [10/7]

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about .is_a? Integer

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Rails doesn't do any auto-conversion magic, level_param is always a String... but maybe it does, will try

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
Expand All @@ -59,4 +83,8 @@ def set_stage
def stage_params
params.slice(:state).permit!
end

def index_params
params.permit(:level)
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down