chore: save current Ruby implementation state before Next.js rewrite

This commit is contained in:
2026-03-27 14:36:22 -07:00
parent e9365faa10
commit 96f7cb86d3
20 changed files with 894 additions and 106 deletions
+24 -60
View File
@@ -1,83 +1,47 @@
# frozen_string_literal: true
require "csv"
module EuchreCamp
module Actions
module Admin
module Matches
class ProcessUpload < EuchreCamp::Action
include Deps[repo: 'repositories.matches']
include Deps[events_repo: 'repositories.events']
def handle(request, response)
# In Hanami, file uploads are in request.params
file = request.params[:csv_file]
event_id = request.params[:event_id]&.to_i
unless file && file[:tempfile]
response.flash[:error] = "No file uploaded"
response.redirect_to("/admin/matches/upload")
return
end
matches_created = 0
errors = []
# Parse CSV
CSV.foreach(file[:tempfile].path, headers: true, header_converters: :symbol) do |row|
begin
# Map CSV columns to our database structure
# Expected columns: played_at, team_1_p1_name, team_1_p2_name, team_1_score, team_2_p1_name, team_2_p2_name, team_2_score
# Find or create players by name
player_1 = find_or_create_player(row[:team_1_p1_name])
player_2 = find_or_create_player(row[:team_1_p2_name])
player_3 = find_or_create_player(row[:team_2_p1_name])
player_4 = find_or_create_player(row[:team_2_p2_name])
played_at = begin
Time.parse(row[:played_at])
rescue
Time.now
end
match_data = {
played_at: played_at,
team_1_p1_id: player_1[:id],
team_1_p2_id: player_2[:id],
team_1_score: row[:team_1_score].to_i,
team_2_p1_id: player_3[:id],
team_2_p2_id: player_4[:id],
team_2_score: row[:team_2_score].to_i,
status: 'completed'
}
match = repo.create(match_data)
EuchreCamp::Jobs::CalculateEloJob.perform_later(match[:id])
matches_created += 1
rescue => e
errors << "Row #{row}: #{e.message}"
end
unless event_id
response.flash[:error] = "Please select a tournament"
response.redirect_to("/admin/matches/upload")
return
end
response.flash[:message] = "Created #{matches_created} matches.#{' Errors: ' + errors.join(', ') if errors.any?}"
response.redirect_to("/admin/matches")
end
private
def find_or_create_player(name)
rom = EuchreCamp::App["persistence.rom"]
return nil unless name
name = name.strip
player = rom.relations[:players].where(name: name).one
if player
player
else
rom.relations[:players].insert(name: name, rating: 0, current_elo: 1000)
rom.relations[:players].where(name: name).one
# Verify event exists
begin
events_repo.by_id(event_id)
rescue
response.flash[:error] = "Invalid tournament ID"
response.redirect_to("/admin/matches/upload")
return
end
# Use the specialized importer for Euchre tournament results
importer = EuchreCamp::Services::TournamentCsvImporter.new(event_id: event_id)
results = importer.import(file[:tempfile].path)
message = "Created #{results[:matches_created]} matches, #{results[:teams_created]} teams"
message += ". Errors: #{results[:errors].join(', ')}" if results[:errors].any?
response.flash[:message] = message
response.redirect_to("/admin/tournaments/#{event_id}")
end
end
end