Add advancing teams into tournaments show endpoint

This commit is contained in:
Daniel Schädler 2025-03-09 21:05:41 +01:00
parent 834b3ca433
commit 79bafec693
3 changed files with 27 additions and 1 deletions

View File

@ -29,7 +29,7 @@ class TournamentsController < ApplicationController
if show_params.fetch(:simple, 'false') == 'true' if show_params.fetch(:simple, 'false') == 'true'
render json: @tournament, serializer: SimpleTournamentSerializer render json: @tournament, serializer: SimpleTournamentSerializer
else else
render json: @tournament, include: '**' render json: @tournament.as_json(methods: :teams_advancing_from_group_stage)
end end
end end

View File

@ -20,6 +20,18 @@ class Tournament < ApplicationRecord
[stages.map(&:matches), stages.map { |s| s.groups.map(&:matches) }].flatten [stages.map(&:matches), stages.map { |s| s.groups.map(&:matches) }].flatten
end end
def teams_advancing_from_group_stage
group_stage = self.group_stage
return [] if group_stage.nil?
GroupStageService.get_advancing_teams(group_stage)
end
def group_stage
# get the stage with level -1 (group stage)
stages.find_by(level: -1)
end
private private
def generate_code def generate_code

View File

@ -109,6 +109,20 @@ RSpec.describe TournamentsController, type: :controller do
body = deserialize_response response body = deserialize_response response
expect(body[:stages]).to be_nil expect(body[:stages]).to be_nil
expect(body[:teams]).to be_nil expect(body[:teams]).to be_nil
expect(body[:advancing_teams]).to be_nil
end
end
context 'on a group stage tournament' do
before do
@group_stage_tournament = create(:group_stage_tournament)
end
it 'includes advancing teams' do
get :show, params: { id: @group_stage_tournament.to_param }
body = deserialize_response response
advancing_teams = body[:teams_advancing_from_group_stage]
expected_advancing_teams = GroupStageService.get_advancing_teams(@group_stage_tournament.group_stage)
expect(advancing_teams).to match_array(expected_advancing_teams)
end end
end end
end end