From f7015d04dd07bb75ed04c316e1e60a5bec14971b Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Tue, 16 Sep 2025 03:26:15 -0700 Subject: [PATCH] Add UUID and granblue_id resolution support - Create IdResolvable concern for flexible ID lookups - Update character/summon/weapon controllers to use concern - Support both UUID and granblue_id in API calls --- .../api/v1/characters_controller.rb | 5 +++- app/controllers/api/v1/summons_controller.rb | 5 +++- app/controllers/api/v1/weapons_controller.rb | 5 +++- app/controllers/concerns/id_resolvable.rb | 23 +++++++++++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 app/controllers/concerns/id_resolvable.rb diff --git a/app/controllers/api/v1/characters_controller.rb b/app/controllers/api/v1/characters_controller.rb index 354f524..94de793 100644 --- a/app/controllers/api/v1/characters_controller.rb +++ b/app/controllers/api/v1/characters_controller.rb @@ -3,6 +3,8 @@ module Api module V1 class CharactersController < Api::V1::ApiController + include IdResolvable + before_action :set def show @@ -12,7 +14,8 @@ module Api private def set - @character = Character.where(granblue_id: params[:id]).first + @character = find_by_any_id(Character, params[:id]) + render_not_found_response('character') unless @character end end end diff --git a/app/controllers/api/v1/summons_controller.rb b/app/controllers/api/v1/summons_controller.rb index f4ba9ea..9227602 100644 --- a/app/controllers/api/v1/summons_controller.rb +++ b/app/controllers/api/v1/summons_controller.rb @@ -3,6 +3,8 @@ module Api module V1 class SummonsController < Api::V1::ApiController + include IdResolvable + before_action :set def show @@ -12,7 +14,8 @@ module Api private def set - @summon = Summon.where(granblue_id: params[:id]).first + @summon = find_by_any_id(Summon, params[:id]) + render_not_found_response('summon') unless @summon end end end diff --git a/app/controllers/api/v1/weapons_controller.rb b/app/controllers/api/v1/weapons_controller.rb index 2f163b4..17d2490 100644 --- a/app/controllers/api/v1/weapons_controller.rb +++ b/app/controllers/api/v1/weapons_controller.rb @@ -3,6 +3,8 @@ module Api module V1 class WeaponsController < Api::V1::ApiController + include IdResolvable + before_action :set def show @@ -12,7 +14,8 @@ module Api private def set - @weapon = Weapon.where(granblue_id: params[:id]).first + @weapon = find_by_any_id(Weapon, params[:id]) + render_not_found_response('weapon') unless @weapon end end end diff --git a/app/controllers/concerns/id_resolvable.rb b/app/controllers/concerns/id_resolvable.rb new file mode 100644 index 0000000..d97d5eb --- /dev/null +++ b/app/controllers/concerns/id_resolvable.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module IdResolvable + extend ActiveSupport::Concern + + UUID_REGEX = /\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/i + + private + + def uuid_format?(id) + id.to_s.match?(UUID_REGEX) + end + + def find_by_any_id(model_class, id) + return nil if id.blank? + + if uuid_format?(id) + model_class.find_by(id: id) + else + model_class.find_by(granblue_id: id) + end + end +end \ No newline at end of file