Implements group stage tournament creation

This commit is contained in:
Daniel Schädler 2019-04-24 12:33:43 +02:00
parent c9093dca0b
commit 7130c3acf5
2 changed files with 82 additions and 12 deletions

View File

@ -31,25 +31,47 @@ class TournamentsController < ApplicationController
def create def create
params = tournament_params params = tournament_params
params.require(:teams) params.require(:teams)
# convert teams parameter into Team objects group_stage = params.delete(:group_stage)
teams = params.delete('teams').map do |team| teams = params.delete('teams')
if team[:id]
Team.find team[:id]
elsif team[:name]
Team.create name: team[:name]
end
end
# create tournament # create tournament
tournament = current_user.tournaments.new params tournament = current_user.tournaments.new params
# associate provided teams with tournament if group_stage
tournament.teams = teams groups = {}
teams.each do |team|
team_id = team[:id]
team_name = team[:name]
group = team[:group]
unless team_id.nil?
team = Team.find team_id
put_team_into_groups_hash(groups, team, group)
end
unless team_name.nil?
team = Team.create name: team_name
put_team_into_groups_hash(groups, team, group)
end
end
# add groups to tournament
result = AddGroupStageToTournamentAndSaveTournamentToDatabase.call(tournament: tournament, groups: groups.values)
else
# convert teams parameter into Team objects
teams = teams.map do |team|
if team[:id]
Team.find team[:id]
elsif team[:name]
Team.create name: team[:name]
end
end
# associate provided teams with tournament
tournament.teams = teams
# add playoff stage to tournament
result = AddPlayoffsToTournamentAndSaveTournamentToDatabase.call(tournament: tournament)
end
# validate tournament # validate tournament
unless tournament.valid? unless tournament.valid?
render json: tournament.errors, status: :unprocessable_entity render json: tournament.errors, status: :unprocessable_entity
return return
end end
# add playoff stage to tournament
result = AddPlayoffsToTournamentAndSaveTournamentToDatabase.call(tournament: tournament)
# return appropriate result # return appropriate result
if result.success? if result.success?
render json: result.tournament, status: :created, location: result.tournament render json: result.tournament, status: :created, location: result.tournament
@ -74,6 +96,14 @@ class TournamentsController < ApplicationController
private private
def put_team_into_groups_hash(groups, team, group)
if groups[group].is_a?(Array)
groups[group] << team
else
groups[group] = [team]
end
end
def set_tournament def set_tournament
@tournament = Tournament.find(params[:id]) @tournament = Tournament.find(params[:id])
end end

View File

@ -9,6 +9,8 @@ RSpec.describe TournamentsController, type: :controller do
@another_user = create(:user) @another_user = create(:user)
@private_tournament = create(:tournament, user: @another_user, public: false) @private_tournament = create(:tournament, user: @another_user, public: false)
@teams = create_list(:detached_team, 4) @teams = create_list(:detached_team, 4)
@teams16 = create_list(:detached_team, 16)
@groups = create_list(:group, 4)
end end
describe 'GET #index' do describe 'GET #index' do
@ -112,6 +114,17 @@ RSpec.describe TournamentsController, type: :controller do
} }
end end
let(:create_group_tournament_data) do
teams_with_groups = @teams16.each_with_index.map { |team, i| { id: team.id, group: (i / 4).floor } }
{
name: Faker::TvShows::FamilyGuy.character,
description: Faker::Movies::HarryPotter.quote,
public: false,
group_stage: true,
teams: teams_with_groups
}
end
context 'without authentication headers' do context 'without authentication headers' do
it 'renders an unauthorized error response' do it 'renders an unauthorized error response' do
put :create, params: create_playoff_tournament_data put :create, params: create_playoff_tournament_data
@ -159,6 +172,24 @@ RSpec.describe TournamentsController, type: :controller do
expect(included_teams).to match_array(@teams) expect(included_teams).to match_array(@teams)
end end
context 'generates a group stage tournament' do
before(:each) do
post :create, params: create_group_tournament_data
body = deserialize_response response
@group_stage_tournament = Tournament.find(body[:id])
end
it 'with all given teams in their respective groups' do
included_teams = @group_stage_tournament.stages.find_by(level: -1).teams
expect(included_teams).to match_array(@teams16)
end
it 'with stage level -1 for the group stage' do
group_stage = @group_stage_tournament.stages.find_by(level: -1)
expect(group_stage).to be_a(Stage)
end
end
it 'renders a JSON response with the new tournament' do it 'renders a JSON response with the new tournament' do
post :create, params: create_playoff_tournament_data post :create, params: create_playoff_tournament_data
expect(response).to have_http_status(:created) expect(response).to have_http_status(:created)
@ -176,6 +207,15 @@ RSpec.describe TournamentsController, type: :controller do
end end
end end
context 'with unequal group sizes' do
it 'returns an error response' do
data = create_group_tournament_data
data[:teams].pop
post :create, params: data
expect(response).to have_http_status(:unprocessable_entity)
end
end
context 'with team names' do context 'with team names' do
it 'creates teams for given names' do it 'creates teams for given names' do
data = create_playoff_tournament_data data = create_playoff_tournament_data