# 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