From 1d3f128cd5df8e9981ec555f59730aba38a1c85f Mon Sep 17 00:00:00 2001 From: David Gwilliam Date: Fri, 27 Mar 2026 13:10:08 -0700 Subject: [PATCH] feat: Add match edit and update actions with authorization --- app/actions/admin/matches/edit.rb | 42 +++++++++++++ app/actions/admin/matches/update.rb | 55 ++++++++++++++++ app/templates/admin/matches/edit.html.erb | 76 +++++++++++++++++++++++ app/views/admin/matches/edit.rb | 14 +++++ app/views/admin/matches/update.rb | 13 ++++ 5 files changed, 200 insertions(+) create mode 100644 app/actions/admin/matches/edit.rb create mode 100644 app/actions/admin/matches/update.rb create mode 100644 app/templates/admin/matches/edit.html.erb create mode 100644 app/views/admin/matches/edit.rb create mode 100644 app/views/admin/matches/update.rb diff --git a/app/actions/admin/matches/edit.rb b/app/actions/admin/matches/edit.rb new file mode 100644 index 0000000..d86e395 --- /dev/null +++ b/app/actions/admin/matches/edit.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +module EuchreCamp + module Actions + module Admin + module Matches + class Edit < EuchreCamp::Action + include Deps[matches_repo: 'repositories.matches', players_repo: 'repositories.players'] + + def handle(request, response) + match_id = request.params[:id] + match = matches_repo.by_id(match_id) + user = current_user(request) + + # Check authorization + if user + # Check if user can edit this match (within 5 minutes or admin) + unless EuchreCamp::Services::Authorization.can?(user, :edit_own_match_within_5_min, match) || + EuchreCamp::Services::Authorization.can?(user, :edit_any_record) + response.flash[:error] = 'Match cannot be edited (too old or insufficient permissions)' + response.redirect_to '/admin/matches' + return + end + else + response.flash[:error] = 'Please login to edit matches' + response.redirect_to '/login' + return + end + + # Get available players for the form + all_players = players_repo.all + + render_with_player(request, response, + match: match, + all_players: all_players + ) + end + end + end + end + end +end diff --git a/app/actions/admin/matches/update.rb b/app/actions/admin/matches/update.rb new file mode 100644 index 0000000..632f68b --- /dev/null +++ b/app/actions/admin/matches/update.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +module EuchreCamp + module Actions + module Admin + module Matches + class Update < EuchreCamp::Action + include Deps[matches_repo: 'repositories.matches'] + + def handle(request, response) + match_id = request.params[:id] + match = matches_repo.by_id(match_id) + user = current_user(request) + + # Check authorization + if user + # Check if user can edit this match (within 5 minutes or admin) + unless EuchreCamp::Services::Authorization.can?(user, :edit_own_match_within_5_min, match) || + EuchreCamp::Services::Authorization.can?(user, :edit_any_record) + response.flash[:error] = 'Match cannot be edited (too old or insufficient permissions)' + response.redirect_to '/admin/matches' + return + end + else + response.flash[:error] = 'Please login to edit matches' + response.redirect_to '/login' + return + end + + # Update the match + match_data = request.params[:match] + begin + rom = EuchreCamp::App["persistence.rom"] + rom.relations[:matches].where(id: match_id).update( + team_1_p1_id: match_data[:team_1_p1_id], + team_1_p2_id: match_data[:team_1_p2_id], + team_2_p1_id: match_data[:team_2_p1_id], + team_2_p2_id: match_data[:team_2_p2_id], + team_1_score: match_data[:team_1_score], + team_2_score: match_data[:team_2_score], + status: 'completed' + ) + + response.flash[:success] = 'Match updated successfully' + response.redirect_to '/admin/matches' + rescue => e + response.flash[:error] = "Failed to update match: #{e.message}" + response.redirect_to "/admin/matches/#{match_id}/edit" + end + end + end + end + end + end +end diff --git a/app/templates/admin/matches/edit.html.erb b/app/templates/admin/matches/edit.html.erb new file mode 100644 index 0000000..05ec228 --- /dev/null +++ b/app/templates/admin/matches/edit.html.erb @@ -0,0 +1,76 @@ +

Edit Match #<%= match[:id] %>

+ +
+
+ + +
+

Team 1

+
+ + +
+
+ + +
+
+ + +
+
+ +
+

Team 2

+
+ + +
+
+ + +
+
+ + +
+
+ +
+ + Cancel +
+
+
+ +

+ Note: Match scores can only be edited within 5 minutes of recording, or by a tournament/club administrator. +

diff --git a/app/views/admin/matches/edit.rb b/app/views/admin/matches/edit.rb new file mode 100644 index 0000000..c4fdd80 --- /dev/null +++ b/app/views/admin/matches/edit.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module EuchreCamp + module Views + module Admin + module Matches + class Edit < EuchreCamp::View + expose :match + expose :all_players + end + end + end + end +end diff --git a/app/views/admin/matches/update.rb b/app/views/admin/matches/update.rb new file mode 100644 index 0000000..71e52d3 --- /dev/null +++ b/app/views/admin/matches/update.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module EuchreCamp + module Views + module Admin + module Matches + class Update < EuchreCamp::View + # No template needed - this action redirects + end + end + end + end +end