Call tournament generation for create endpoint

and associate given team ids with the new tournament
This commit is contained in:
Thor77 2019-04-07 19:24:17 +02:00
parent 12c14303f2
commit ec4e2797c2
No known key found for this signature in database
GPG Key ID: 5051E71B46AA669A
1 changed files with 14 additions and 5 deletions

View File

@ -18,12 +18,21 @@ class TournamentsController < ApplicationController
# POST /tournaments # POST /tournaments
def create def create
tournament = current_user.tournaments.new tournament_params params = tournament_params
teams = params.delete 'teams'
if tournament.save tournament = current_user.tournaments.new params
render json: tournament, status: :created, location: tournament tournament.teams = Team.find(teams.map { |t| t[:id] })
else unless tournament.valid?
render json: tournament.errors, status: :unprocessable_entity 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
else
render json: { error: 'Tournament generation failed' }, status: :unprocessable_entity
end end
end end