# frozen_string_literal: true module EuchreCamp module Actions module Admin class Index < EuchreCamp::Action include Deps[ players_repo: 'repositories.players', events_repo: 'repositories.events', matches_repo: 'repositories.matches' ] def handle(request, response) # Check if user has permission to view admin panel user = current_user(request) unless user && EuchreCamp::Services::Authorization.can?(user, :view_admin_panel) response.flash[:error] = 'Access denied' response.redirect_to '/rankings' return end # Get dashboard statistics total_players = players_repo.all.length active_tournaments = events_repo.upcoming.length completed_tournaments = events_repo.completed.length recent_matches = get_recent_matches user = current_user(request) render_with_player(request, response, total_players: total_players, active_tournaments: active_tournaments, completed_tournaments: completed_tournaments, recent_matches: recent_matches, current_user: user ) end private def get_recent_matches rom = EuchreCamp::App["persistence.rom"] matches = rom.relations[:matches] .order(Sequel.desc(:id)) .limit(5) .to_a # Get player names all_players = rom.relations[:players].to_a.to_h { |p| [p[:id], p] } matches.map do |match| team_1 = [ all_players[match[:team_1_p1_id]], all_players[match[:team_1_p2_id]] ].compact team_2 = [ all_players[match[:team_2_p1_id]], all_players[match[:team_2_p2_id]] ].compact { id: match[:id], team_1: team_1, team_2: team_2, score: "#{match[:team_1_score]}-#{match[:team_2_score]}", status: match[:status], played_at: match[:played_at] } end end end end end end