Implements group stage tournament creation
This commit is contained in:
parent
300f16b4c2
commit
acfc388da4
|
|
@ -31,25 +31,47 @@ class TournamentsController < ApplicationController
|
|||
def create
|
||||
params = tournament_params
|
||||
params.require(:teams)
|
||||
# convert teams parameter into Team objects
|
||||
teams = params.delete('teams').map do |team|
|
||||
if team[:id]
|
||||
Team.find team[:id]
|
||||
elsif team[:name]
|
||||
Team.create name: team[:name]
|
||||
end
|
||||
end
|
||||
group_stage = params.delete(:group_stage)
|
||||
teams = params.delete('teams')
|
||||
# create tournament
|
||||
tournament = current_user.tournaments.new params
|
||||
# associate provided teams with tournament
|
||||
tournament.teams = teams
|
||||
if group_stage
|
||||
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
|
||||
unless tournament.valid?
|
||||
render json: tournament.errors, status: :unprocessable_entity
|
||||
return
|
||||
end
|
||||
# add playoff stage to tournament
|
||||
result = AddPlayoffsToTournamentAndSaveTournamentToDatabase.call(tournament: tournament)
|
||||
# return appropriate result
|
||||
if result.success?
|
||||
render json: result.tournament, status: :created, location: result.tournament
|
||||
|
|
@ -74,6 +96,14 @@ class TournamentsController < ApplicationController
|
|||
|
||||
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
|
||||
@tournament = Tournament.find(params[:id])
|
||||
end
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ RSpec.describe TournamentsController, type: :controller do
|
|||
@another_user = create(:user)
|
||||
@private_tournament = create(:tournament, user: @another_user, public: false)
|
||||
@teams = create_list(:detached_team, 4)
|
||||
@teams16 = create_list(:detached_team, 16)
|
||||
@groups = create_list(:group, 4)
|
||||
end
|
||||
|
||||
describe 'GET #index' do
|
||||
|
|
@ -112,6 +114,17 @@ RSpec.describe TournamentsController, type: :controller do
|
|||
}
|
||||
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
|
||||
it 'renders an unauthorized error response' do
|
||||
put :create, params: create_playoff_tournament_data
|
||||
|
|
@ -159,6 +172,24 @@ RSpec.describe TournamentsController, type: :controller do
|
|||
expect(included_teams).to match_array(@teams)
|
||||
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 'which is a 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
|
||||
post :create, params: create_playoff_tournament_data
|
||||
expect(response).to have_http_status(:created)
|
||||
|
|
@ -176,6 +207,15 @@ RSpec.describe TournamentsController, type: :controller do
|
|||
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
|
||||
it 'creates teams for given names' do
|
||||
data = create_playoff_tournament_data
|
||||
|
|
|
|||
Loading…
Reference in New Issue