add match history
This commit is contained in:
@@ -2,6 +2,29 @@
|
||||
# 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
|
||||
|
||||
@@ -22,6 +22,13 @@ class User < ApplicationRecord
|
||||
.order(finished_at: :desc)
|
||||
end
|
||||
|
||||
def avatar_url
|
||||
return nil unless avatar.attached?
|
||||
|
||||
options = Rails.application.routes.default_url_options.presence || { host: "localhost", port: 3000 }
|
||||
Rails.application.routes.url_helpers.rails_blob_url(avatar, options)
|
||||
end
|
||||
|
||||
private
|
||||
def avatar_content_type
|
||||
unless avatar.blob.content_type.in?(%w[image/png image/jpeg image/gif image/webp])
|
||||
|
||||
@@ -32,6 +32,7 @@ class MatchSerializer
|
||||
{
|
||||
id: user.id,
|
||||
nickname: user.nickname,
|
||||
avatar_url: user.avatar_url,
|
||||
rating_before: rating_before,
|
||||
rating_change: rating_change
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user