add collection counts endpoint

This commit is contained in:
Justin Edmund 2025-12-19 15:52:40 -08:00
parent 5da86c5405
commit 7222353d29
2 changed files with 35 additions and 0 deletions

View file

@ -0,0 +1,34 @@
module Api
module V1
class CollectionController < ApiController
before_action :set_target_user
before_action :check_collection_access
# GET /api/v1/users/:user_id/collection/counts
# Returns total counts for all collection entity types
def counts
render json: {
characters: @target_user.collection_characters.count,
weapons: @target_user.collection_weapons.count,
summons: @target_user.collection_summons.count,
artifacts: @target_user.collection_artifacts.count
}
end
private
def set_target_user
@target_user = User.find(params[:user_id])
rescue ActiveRecord::RecordNotFound
render json: { error: "User not found" }, status: :not_found
end
def check_collection_access
return if @target_user.nil?
unless @target_user.collection_viewable_by?(current_user)
render json: { error: "You do not have permission to view this collection" }, status: :forbidden
end
end
end
end
end

View file

@ -250,6 +250,7 @@ Rails.application.routes.draw do
# Reading collections - works for any user with privacy check
scope 'users/:user_id' do
namespace :collection do
get :counts, controller: '/api/v1/collection'
resources :characters, only: [:index, :show], controller: '/api/v1/collection_characters'
resources :weapons, only: [:index, :show], controller: '/api/v1/collection_weapons'
resources :summons, only: [:index, :show], controller: '/api/v1/collection_summons'