Call tournament generation for create endpoint
and associate given team ids with the new tournament
This commit is contained in:
parent
12c14303f2
commit
ec4e2797c2
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue