From 79bafec6935dadf625bc160fc9d2a4ecb7d06253 Mon Sep 17 00:00:00 2001 From: Malaber Date: Sun, 9 Mar 2025 21:05:41 +0100 Subject: [PATCH] Add advancing teams into tournaments show endpoint --- app/controllers/tournaments_controller.rb | 2 +- app/models/tournament.rb | 12 ++++++++++++ spec/controllers/tournaments_controller_spec.rb | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index a40c6bf..5751202 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -29,7 +29,7 @@ class TournamentsController < ApplicationController if show_params.fetch(:simple, 'false') == 'true' render json: @tournament, serializer: SimpleTournamentSerializer else - render json: @tournament, include: '**' + render json: @tournament.as_json(methods: :teams_advancing_from_group_stage) end end diff --git a/app/models/tournament.rb b/app/models/tournament.rb index d91ec72..1f02015 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -20,6 +20,18 @@ class Tournament < ApplicationRecord [stages.map(&:matches), stages.map { |s| s.groups.map(&:matches) }].flatten 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 def generate_code diff --git a/spec/controllers/tournaments_controller_spec.rb b/spec/controllers/tournaments_controller_spec.rb index ae8f469..9dad030 100644 --- a/spec/controllers/tournaments_controller_spec.rb +++ b/spec/controllers/tournaments_controller_spec.rb @@ -109,6 +109,20 @@ RSpec.describe TournamentsController, type: :controller do body = deserialize_response response expect(body[:stages]).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