From aa198f072b6dcdb5fb89459e081b1c047c6d071c Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Sat, 13 Dec 2025 20:10:18 -0800 Subject: [PATCH] add import endpoints to collection controllers --- .../v1/collection_characters_controller.rb | 37 ++++++++++++++++++- .../api/v1/collection_summons_controller.rb | 37 ++++++++++++++++++- .../api/v1/collection_weapons_controller.rb | 37 ++++++++++++++++++- 3 files changed, 108 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/v1/collection_characters_controller.rb b/app/controllers/api/v1/collection_characters_controller.rb index 0662261..ae0738a 100644 --- a/app/controllers/api/v1/collection_characters_controller.rb +++ b/app/controllers/api/v1/collection_characters_controller.rb @@ -7,7 +7,7 @@ module Api before_action :set_collection_character_for_read, only: %i[show] # Write actions: require auth, use current_user - before_action :restrict_access, only: %i[create update destroy batch] + before_action :restrict_access, only: %i[create update destroy batch import] before_action :set_collection_character_for_write, only: %i[update destroy] def index @@ -113,6 +113,37 @@ module Api ), status: status end + # POST /collection/characters/import + # Imports characters from game JSON data + # + # @param data [Hash] Game data containing character list + # @param update_existing [Boolean] Whether to update existing characters (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 = CharacterImportService.new( + current_user, + game_data, + update_existing: import_params[:update_existing] == 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 + }, status: status + end + private def set_target_user @@ -163,6 +194,10 @@ module Api earring: %i[modifier strength] ]) end + + def import_params + params.permit(:update_existing, data: {}) + end end end end diff --git a/app/controllers/api/v1/collection_summons_controller.rb b/app/controllers/api/v1/collection_summons_controller.rb index 6a68ecb..b9739f8 100644 --- a/app/controllers/api/v1/collection_summons_controller.rb +++ b/app/controllers/api/v1/collection_summons_controller.rb @@ -7,7 +7,7 @@ module Api 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] + before_action :restrict_access, only: %i[create update destroy batch import] before_action :set_collection_summon_for_write, only: %i[update destroy] def index @@ -96,6 +96,37 @@ module Api ), status: status 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) + 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 + ) + + 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 + }, status: status + end + private def set_target_user @@ -134,6 +165,10 @@ module Api :summon_id, :uncap_level, :transcendence_step ]) end + + def import_params + params.permit(:update_existing, data: {}) + end end end end diff --git a/app/controllers/api/v1/collection_weapons_controller.rb b/app/controllers/api/v1/collection_weapons_controller.rb index f0d814b..fb77b75 100644 --- a/app/controllers/api/v1/collection_weapons_controller.rb +++ b/app/controllers/api/v1/collection_weapons_controller.rb @@ -7,7 +7,7 @@ module Api before_action :set_collection_weapon_for_read, only: %i[show] # Write actions: require auth, use current_user - before_action :restrict_access, only: %i[create update destroy batch] + before_action :restrict_access, only: %i[create update destroy batch import] before_action :set_collection_weapon_for_write, only: %i[update destroy] def index @@ -98,6 +98,37 @@ module Api ), status: status end + # POST /collection/weapons/import + # Imports weapons from game JSON data + # + # @param data [Hash] Game data containing weapon list + # @param update_existing [Boolean] Whether to update existing weapons (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 = WeaponImportService.new( + current_user, + game_data, + update_existing: import_params[:update_existing] == 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 + }, status: status + end + private def set_target_user @@ -144,6 +175,10 @@ module Api :element ]) end + + def import_params + params.permit(:update_existing, data: {}) + end end end end