diff --git a/app/controllers/api/v1/collection_controller.rb b/app/controllers/api/v1/collection_controller.rb new file mode 100644 index 0000000..080acd0 --- /dev/null +++ b/app/controllers/api/v1/collection_controller.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index db71b63..536d5c3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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'