Files
ruby-sea-battle/main-server/app/controllers/api/matches_controller.rb
T
2026-11-15 18:27:05 +03:00

35 lines
1.2 KiB
Ruby

# Player-facing match details. Only participants of a match can view it:
# a non-participant (or an unknown uuid) gets 404 to avoid leaking
# information about other players' matches.
class Api::MatchesController < ApplicationController
def index
offset = (params[:start] || params[:from] || 0).to_i
offset = 0 if offset < 0
count = params[:count].present? ? params[:count].to_i : 20
count = 20 if count <= 0
count = 100 if count > 100
scope = Match.for_user(current_user)
.includes(player1: { avatar_attachment: :blob }, player2: { avatar_attachment: :blob })
.order(started_at: :desc, id: :desc)
total = scope.count
matches = scope.offset(offset).limit(count)
render json: {
matches: matches.map { |match| MatchSerializer.as_json(match, viewer: current_user) },
total: total,
start: offset,
count: count
}
end
def show
match = Match.for_user(current_user).find_by(uuid: params[:uuid])
return render json: { error: "Match not found" }, status: :not_found unless match
render json: { match: MatchSerializer.as_json(match, viewer: current_user) }
end
end