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:
parent
b03d5e6618
commit
f7015d04dd
4 changed files with 35 additions and 3 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
23
app/controllers/concerns/id_resolvable.rb
Normal file
23
app/controllers/concerns/id_resolvable.rb
Normal 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
|
||||
Loading…
Reference in a new issue