feat: Add match edit and update actions with authorization

This commit is contained in:
2026-03-27 13:10:08 -07:00
parent 9372069926
commit 1d3f128cd5
5 changed files with 200 additions and 0 deletions
+42
View File
@@ -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
+55
View File
@@ -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
+76
View File
@@ -0,0 +1,76 @@
<h1>Edit Match #<%= match[:id] %></h1>
<div class="match-edit-form">
<form action="/admin/matches/<%= match[:id] %>" method="POST">
<input type="hidden" name="_method" value="PATCH">
<div class="form-section">
<h3>Team 1</h3>
<div class="form-group">
<label>Player 1</label>
<select name="match[team_1_p1_id]" required>
<option value="">Select player...</option>
<% all_players.each do |player| %>
<option value="<%= player[:id] %>" <%= 'selected' if player[:id] == match[:team_1_p1_id] %>>
<%= player[:name] %>
</option>
<% end %>
</select>
</div>
<div class="form-group">
<label>Player 2</label>
<select name="match[team_1_p2_id]" required>
<option value="">Select player...</option>
<% all_players.each do |player| %>
<option value="<%= player[:id] %>" <%= 'selected' if player[:id] == match[:team_1_p2_id] %>>
<%= player[:name] %>
</option>
<% end %>
</select>
</div>
<div class="form-group">
<label>Score</label>
<input type="number" name="match[team_1_score]" value="<%= match[:team_1_score] %>" min="0" max="10" required>
</div>
</div>
<div class="form-section">
<h3>Team 2</h3>
<div class="form-group">
<label>Player 1</label>
<select name="match[team_2_p1_id]" required>
<option value="">Select player...</option>
<% all_players.each do |player| %>
<option value="<%= player[:id] %>" <%= 'selected' if player[:id] == match[:team_2_p1_id] %>>
<%= player[:name] %>
</option>
<% end %>
</select>
</div>
<div class="form-group">
<label>Player 2</label>
<select name="match[team_2_p2_id]" required>
<option value="">Select player...</option>
<% all_players.each do |player| %>
<option value="<%= player[:id] %>" <%= 'selected' if player[:id] == match[:team_2_p2_id] %>>
<%= player[:name] %>
</option>
<% end %>
</select>
</div>
<div class="form-group">
<label>Score</label>
<input type="number" name="match[team_2_score]" value="<%= match[:team_2_score] %>" min="0" max="10" required>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Update Match</button>
<a href="/admin/matches" class="btn">Cancel</a>
</div>
</form>
</div>
<p class="help-text">
<strong>Note:</strong> Match scores can only be edited within 5 minutes of recording, or by a tournament/club administrator.
</p>
+14
View File
@@ -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
+13
View File
@@ -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