- preview_sync endpoint shows what'll get deleted before you commit - import services handle reconciliation (find missing items, delete them) - grid items get flagged as orphaned when their collection source is gone - party exposes has_orphaned_items - blueprints include orphaned field
234 lines
7.8 KiB
Ruby
234 lines
7.8 KiB
Ruby
module Api
|
|
module V1
|
|
class CollectionSummonsController < ApiController
|
|
# Read actions: look up user from params, check privacy
|
|
before_action :set_target_user, only: %i[index show]
|
|
before_action :check_collection_access, only: %i[index show]
|
|
before_action :set_collection_summon_for_read, only: %i[show]
|
|
|
|
# Write actions: require auth, use current_user
|
|
before_action :restrict_access, only: %i[create update destroy batch batch_destroy import preview_sync]
|
|
before_action :set_collection_summon_for_write, only: %i[update destroy]
|
|
|
|
def index
|
|
@collection_summons = @target_user.collection_summons
|
|
.includes(:summon)
|
|
|
|
# Apply filters (array_param splits comma-separated values for OR logic)
|
|
@collection_summons = @collection_summons.by_summon(params[:summon_id]) if params[:summon_id]
|
|
@collection_summons = @collection_summons.by_element(array_param(:element)) if params[:element]
|
|
@collection_summons = @collection_summons.by_rarity(array_param(:rarity)) if params[:rarity]
|
|
|
|
@collection_summons = @collection_summons.paginate(page: params[:page], per_page: params[:limit] || 50)
|
|
|
|
render json: Api::V1::CollectionSummonBlueprint.render(
|
|
@collection_summons,
|
|
root: :summons,
|
|
meta: pagination_meta(@collection_summons)
|
|
)
|
|
end
|
|
|
|
def show
|
|
render json: Api::V1::CollectionSummonBlueprint.render(
|
|
@collection_summon,
|
|
view: :full
|
|
)
|
|
end
|
|
|
|
def create
|
|
@collection_summon = current_user.collection_summons.build(collection_summon_params)
|
|
|
|
if @collection_summon.save
|
|
render json: Api::V1::CollectionSummonBlueprint.render(
|
|
@collection_summon,
|
|
view: :full
|
|
), status: :created
|
|
else
|
|
render_validation_error_response(@collection_summon)
|
|
end
|
|
end
|
|
|
|
def update
|
|
if @collection_summon.update(collection_summon_params)
|
|
render json: Api::V1::CollectionSummonBlueprint.render(
|
|
@collection_summon,
|
|
view: :full
|
|
)
|
|
else
|
|
render_validation_error_response(@collection_summon)
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
@collection_summon.destroy
|
|
head :no_content
|
|
end
|
|
|
|
# POST /collection/summons/batch
|
|
# Creates multiple collection summons in a single request
|
|
# Unlike characters, summons can have duplicates (user can own multiple copies)
|
|
def batch
|
|
items = batch_summon_params[:collection_summons] || []
|
|
created = []
|
|
errors = []
|
|
|
|
ActiveRecord::Base.transaction do
|
|
items.each_with_index do |item_params, index|
|
|
collection_summon = current_user.collection_summons.build(item_params)
|
|
|
|
if collection_summon.save
|
|
created << collection_summon
|
|
else
|
|
errors << {
|
|
index: index,
|
|
summon_id: item_params[:summon_id],
|
|
error: collection_summon.errors.full_messages.join(', ')
|
|
}
|
|
end
|
|
end
|
|
end
|
|
|
|
status = errors.any? ? :multi_status : :created
|
|
|
|
render json: Api::V1::CollectionSummonBlueprint.render(
|
|
created,
|
|
root: :summons,
|
|
meta: { created: created.size, errors: errors }
|
|
), status: status
|
|
end
|
|
|
|
# DELETE /collection/summons/batch_destroy
|
|
# Deletes multiple collection summons in a single request
|
|
def batch_destroy
|
|
ids = batch_destroy_params[:ids] || []
|
|
deleted_count = current_user.collection_summons.where(id: ids).destroy_all.count
|
|
|
|
render json: {
|
|
meta: { deleted: deleted_count }
|
|
}, status: :ok
|
|
end
|
|
|
|
# POST /collection/summons/import
|
|
# Imports summons from game JSON data
|
|
#
|
|
# @param data [Hash] Game data containing summon list
|
|
# @param update_existing [Boolean] Whether to update existing summons (default: false)
|
|
# @param is_full_inventory [Boolean] Whether this represents the user's complete inventory (default: false)
|
|
# @param reconcile_deletions [Boolean] Whether to delete items not in the import (default: false)
|
|
def import
|
|
game_data = import_params[:data]
|
|
|
|
unless game_data.present?
|
|
return render json: { error: 'No data provided' }, status: :bad_request
|
|
end
|
|
|
|
service = SummonImportService.new(
|
|
current_user,
|
|
game_data,
|
|
update_existing: import_params[:update_existing] == true,
|
|
is_full_inventory: import_params[:is_full_inventory] == true,
|
|
reconcile_deletions: import_params[:reconcile_deletions] == true
|
|
)
|
|
|
|
result = service.import
|
|
|
|
status = result.success? ? :created : :multi_status
|
|
|
|
render json: {
|
|
success: result.success?,
|
|
created: result.created.size,
|
|
updated: result.updated.size,
|
|
skipped: result.skipped.size,
|
|
errors: result.errors,
|
|
reconciliation: result.reconciliation
|
|
}, status: status
|
|
end
|
|
|
|
# POST /collection/summons/preview_sync
|
|
# Previews what would be deleted in a full sync operation
|
|
#
|
|
# @param data [Hash] Game data containing summon list
|
|
# @return [JSON] List of items that would be deleted
|
|
def preview_sync
|
|
game_data = import_params[:data]
|
|
|
|
unless game_data.present?
|
|
return render json: { error: 'No data provided' }, status: :bad_request
|
|
end
|
|
|
|
service = SummonImportService.new(current_user, game_data)
|
|
items_to_delete = service.preview_deletions
|
|
|
|
render json: {
|
|
will_delete: items_to_delete.map do |cs|
|
|
{
|
|
id: cs.id,
|
|
game_id: cs.game_id,
|
|
name: cs.summon&.name_en,
|
|
granblue_id: cs.summon&.granblue_id,
|
|
uncap_level: cs.uncap_level,
|
|
transcendence_step: cs.transcendence_step
|
|
}
|
|
end,
|
|
count: items_to_delete.size
|
|
}
|
|
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? # Already handled by set_target_user
|
|
unless @target_user.collection_viewable_by?(current_user)
|
|
render json: { error: "You do not have permission to view this collection" }, status: :forbidden
|
|
end
|
|
end
|
|
|
|
def set_collection_summon_for_read
|
|
@collection_summon = @target_user.collection_summons.find(params[:id])
|
|
rescue ActiveRecord::RecordNotFound
|
|
raise CollectionErrors::CollectionItemNotFound.new('summon', params[:id])
|
|
end
|
|
|
|
def set_collection_summon_for_write
|
|
@collection_summon = current_user.collection_summons.find(params[:id])
|
|
rescue ActiveRecord::RecordNotFound
|
|
raise CollectionErrors::CollectionItemNotFound.new('summon', params[:id])
|
|
end
|
|
|
|
def collection_summon_params
|
|
params.require(:collection_summon).permit(
|
|
:summon_id, :uncap_level, :transcendence_step
|
|
)
|
|
end
|
|
|
|
def batch_summon_params
|
|
params.permit(collection_summons: [
|
|
:summon_id, :uncap_level, :transcendence_step
|
|
])
|
|
end
|
|
|
|
def import_params
|
|
{
|
|
update_existing: params[:update_existing],
|
|
is_full_inventory: params[:is_full_inventory],
|
|
reconcile_deletions: params[:reconcile_deletions],
|
|
data: params[:data]&.to_unsafe_h
|
|
}
|
|
end
|
|
|
|
def batch_destroy_params
|
|
params.permit(ids: [])
|
|
end
|
|
|
|
def array_param(key)
|
|
params[key]&.to_s&.split(',')
|
|
end
|
|
end
|
|
end
|
|
end
|