Files
euchre_camp/app/actions/admin/matches/create.rb
T
david 5bb3bc4e5f feat: enhance match entry form with validation and data
Add improved match entry form with player selection validation and
better user experience for recording match results.

Changes:
- Add player validation to prevent same player on both teams
- Add team score validation (must have winner)
- Improve form layout and player selection UI
- Add helper methods for player lookup and team building
2026-03-27 12:24:53 -07:00

79 lines
2.7 KiB
Ruby

# frozen_string_literal: true
module EuchreCamp
module Actions
module Admin
module Matches
class Create < EuchreCamp::Action
include Deps[
matches_repo: 'repositories.matches',
teams_repo: 'repositories.teams',
brackets_repo: 'repositories.bracket_matchups'
]
def handle(request, response)
params = request.params.to_h
tournament_id = params[:event_id]&.to_i
# Build match parameters
match_params = {
played_at: params[:played_at] ? Time.parse(params[:played_at]) : Time.now
}
if tournament_id && params[:team_1_id] && params[:team_2_id]
# Tournament match - use team IDs
team_1 = teams_repo.teams.by_pk(params[:team_1_id].to_i).one
team_2 = teams_repo.teams.by_pk(params[:team_2_id].to_i).one
match_params.merge!(
event_id: tournament_id,
team_1_p1_id: team_1.player_1_id,
team_1_p2_id: team_1.player_2_id,
team_2_p1_id: team_2.player_1_id,
team_2_p2_id: team_2.player_2_id,
team_1_score: params[:team_1_score]&.to_i || 0,
team_2_score: params[:team_2_score]&.to_i || 0,
status: 'completed'
)
else
# Non-tournament match - use individual players
match_params.merge!(
team_1_p1_id: params[:team_1_p1_id]&.to_i,
team_1_p2_id: params[:team_1_p2_id]&.to_i,
team_1_score: params[:team_1_score]&.to_i || 0,
team_2_p1_id: params[:team_2_p1_id]&.to_i,
team_2_p2_id: params[:team_2_p2_id]&.to_i,
team_2_score: params[:team_2_score]&.to_i || 0,
status: 'completed'
)
end
# Create the match
match = matches_repo.create(match_params)
# Enqueue background job to calculate Elo
EuchreCamp::Jobs::CalculateEloJob.perform_later(match[:id])
# Redirect based on context
if tournament_id
response.redirect_to("/admin/tournaments/#{tournament_id}")
else
response.redirect_to("/admin/matches")
end
rescue StandardError => e
# Handle validation errors or other issues
puts "Error: #{e.message}"
puts e.backtrace.join("\n")
if tournament_id
response.redirect_to("/admin/tournaments/#{tournament_id}")
else
response.redirect_to("/admin/matches/new")
end
end
end
end
end
end
end