Filtra le partite visibili lato client/server, corregge il parsing date Rails su Android, semplifica lo step Partita del wizard e restringe l'API ai campi effettivamente usati (campionato, set e scoring_rules). Co-authored-by: Cursor <cursoragent@cursor.com>
99 lines
2.9 KiB
Ruby
99 lines
2.9 KiB
Ruby
module Api
|
|
module V1
|
|
class MatchesController < BaseController
|
|
before_action :set_team, only: %i[index create]
|
|
before_action :set_match, only: %i[show update destroy]
|
|
|
|
def index
|
|
matches = @team.matches
|
|
.includes(:team, :stream_sessions)
|
|
.order(Arel.sql("scheduled_at ASC NULLS LAST"), created_at: :desc)
|
|
.select(&:coach_hub_visible?)
|
|
render json: matches.map { |m| match_json(m) }
|
|
end
|
|
|
|
def create
|
|
match = @team.matches.create!(match_params)
|
|
render json: match_json(match), status: :created
|
|
end
|
|
|
|
def show
|
|
render json: match_json(@match, detail: true)
|
|
end
|
|
|
|
def update
|
|
attrs = match_params.to_h
|
|
normalize_scoring_rules!(attrs)
|
|
@match.update!(attrs)
|
|
render json: match_json(@match)
|
|
end
|
|
|
|
def destroy
|
|
active = active_session_for(@match)
|
|
if active&.resumable?
|
|
return render json: {
|
|
error: "Chiudi la diretta prima di eliminare questa partita"
|
|
}, status: :unprocessable_entity
|
|
end
|
|
|
|
@match.destroy!
|
|
head :no_content
|
|
end
|
|
|
|
private
|
|
|
|
def set_team
|
|
@team = current_user.streamable_teams.find { |t| t.id == params[:team_id] }
|
|
raise ActiveRecord::RecordNotFound unless @team
|
|
end
|
|
|
|
def set_match
|
|
team_ids = current_user.streamable_teams.map(&:id)
|
|
@match = Match.where(team_id: team_ids).find(params[:id])
|
|
end
|
|
|
|
def match_params
|
|
params.require(:match).permit(
|
|
:opponent_name, :location, :scheduled_at, :sport, :sets_to_win,
|
|
:category,
|
|
scoring_rules: %i[points_per_set points_deciding_set min_point_lead]
|
|
)
|
|
end
|
|
|
|
# Regolamento standard: scoring_rules assente o {} → default FIPAV lato Scoring::Rules.
|
|
def normalize_scoring_rules!(attrs)
|
|
return unless attrs.key?("scoring_rules")
|
|
|
|
rules = attrs["scoring_rules"]
|
|
attrs["scoring_rules"] = {} if rules.blank?
|
|
end
|
|
|
|
def match_json(match, detail: false)
|
|
active = active_session_for(match)
|
|
{
|
|
id: match.id,
|
|
team_id: match.team_id,
|
|
team_name: match.team.name,
|
|
opponent_name: match.opponent_name,
|
|
location: match.location,
|
|
scheduled_at: match.scheduled_at,
|
|
sport: match.sport,
|
|
sets_to_win: match.sets_to_win,
|
|
scoring_rules: match.scoring_rules.presence,
|
|
category: match.category,
|
|
active_session_id: active&.id,
|
|
active_session_status: active&.status,
|
|
stream_completed: match.stream_completed?,
|
|
coach_hub_visible: match.coach_hub_visible?
|
|
}
|
|
end
|
|
|
|
def active_session_for(match)
|
|
match.stream_sessions
|
|
.order(created_at: :desc)
|
|
.find { |s| !%w[ended error].include?(s.status) }
|
|
end
|
|
end
|
|
end
|
|
end
|