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
This commit is contained in:
Justin Edmund 2025-09-16 03:26:15 -07:00
parent b03d5e6618
commit f7015d04dd
4 changed files with 35 additions and 3 deletions

View file

@ -3,6 +3,8 @@
module Api module Api
module V1 module V1
class CharactersController < Api::V1::ApiController class CharactersController < Api::V1::ApiController
include IdResolvable
before_action :set before_action :set
def show def show
@ -12,7 +14,8 @@ module Api
private private
def set 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 end
end end

View file

@ -3,6 +3,8 @@
module Api module Api
module V1 module V1
class SummonsController < Api::V1::ApiController class SummonsController < Api::V1::ApiController
include IdResolvable
before_action :set before_action :set
def show def show
@ -12,7 +14,8 @@ module Api
private private
def set 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 end
end end

View file

@ -3,6 +3,8 @@
module Api module Api
module V1 module V1
class WeaponsController < Api::V1::ApiController class WeaponsController < Api::V1::ApiController
include IdResolvable
before_action :set before_action :set
def show def show
@ -12,7 +14,8 @@ module Api
private private
def set 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 end
end end

View file

@ -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