Update SearchController for blueprinter

This commit is contained in:
Justin Edmund 2022-12-21 22:10:31 -08:00
parent 9a27ac853d
commit 89e666219a

View file

@ -2,6 +2,8 @@
module Api module Api
module V1 module V1
PER_PAGE = 10
class SearchController < Api::V1::ApiController class SearchController < Api::V1::ApiController
def characters def characters
filters = search_params[:filters] filters = search_params[:filters]
@ -22,18 +24,23 @@ module Api
# conditions[:series] = filters['series'] unless filters['series'].blank? || filters['series'].empty? # conditions[:series] = filters['series'] unless filters['series'].blank? || filters['series'].empty?
end end
@characters = if search_params[:query].present? && search_params[:query].length >= 2 characters = if search_params[:query].present? && search_params[:query].length >= 2
if locale == 'ja' if locale == 'ja'
Character.jp_search(search_params[:query]).where(conditions) Character.jp_search(search_params[:query]).where(conditions)
else else
Character.en_search(search_params[:query]).where(conditions) Character.en_search(search_params[:query]).where(conditions)
end end
else else
Character.where(conditions) Character.where(conditions)
end end
@count = @characters.length count = characters.length
@characters = @characters.paginate(page: search_params[:page], per_page: 10) paginated = characters.paginate(page: search_params[:page], per_page: PER_PAGE)
render json: CharacterBlueprint.render(paginated, meta: {
count: count,
total_pages: total_pages(count)
})
end end
def weapons def weapons
@ -51,18 +58,23 @@ module Api
conditions[:series] = filters['series'] unless filters['series'].blank? || filters['series'].empty? conditions[:series] = filters['series'] unless filters['series'].blank? || filters['series'].empty?
end end
@weapons = if search_params[:query].present? && search_params[:query].length >= 2 weapons = if search_params[:query].present? && search_params[:query].length >= 2
if locale == 'ja' if locale == 'ja'
Weapon.jp_search(search_params[:query]).where(conditions) Weapon.jp_search(search_params[:query]).where(conditions)
else else
Weapon.en_search(search_params[:query]).where(conditions) Weapon.en_search(search_params[:query]).where(conditions)
end end
else else
Weapon.where(conditions) Weapon.where(conditions)
end end
@count = @weapons.length count = weapons.length
@weapons = @weapons.paginate(page: search_params[:page], per_page: 10) paginated = weapons.paginate(page: search_params[:page], per_page: PER_PAGE)
render json: WeaponBlueprint.render(paginated, meta: {
count: count,
total_pages: total_pages(count)
})
end end
def summons def summons
@ -75,18 +87,23 @@ module Api
conditions[:element] = filters['element'] unless filters['element'].blank? || filters['element'].empty? conditions[:element] = filters['element'] unless filters['element'].blank? || filters['element'].empty?
end end
@summons = if search_params[:query].present? && search_params[:query].length >= 2 summons = if search_params[:query].present? && search_params[:query].length >= 2
if locale == 'ja' if locale == 'ja'
Summon.jp_search(search_params[:query]).where(conditions) Summon.jp_search(search_params[:query]).where(conditions)
else else
Summon.en_search(search_params[:query]).where(conditions) Summon.en_search(search_params[:query]).where(conditions)
end end
else else
Summon.where(conditions) Summon.where(conditions)
end end
@count = @summons.length count = summons.length
@summons = @summons.paginate(page: search_params[:page], per_page: 10) paginated = summons.paginate(page: search_params[:page], per_page: PER_PAGE)
render json: SummonBlueprint.render(paginated, meta: {
count: count,
total_pages: total_pages(count)
})
end end
def job_skills def job_skills
@ -113,38 +130,47 @@ module Api
end end
# Perform the query # Perform the query
@skills = if search_params[:query].present? && search_params[:query].length >= 2 skills = if search_params[:query].present? && search_params[:query].length >= 2
JobSkill.method("#{locale}_search").call(search_params[:query]) JobSkill.method("#{locale}_search").call(search_params[:query])
.where(conditions) .where(conditions)
.where(job: job.id, main: false) .where(job: job.id, main: false)
.or( .or(
JobSkill.method("#{locale}_search").call(search_params[:query]) JobSkill.method("#{locale}_search").call(search_params[:query])
.where(conditions) .where(conditions)
.where(sub: true) .where(sub: true)
) )
else else
JobSkill.all JobSkill.all
.where(conditions) .where(conditions)
.where(job: job.id, main: false) .where(job: job.id, main: false)
.or( .or(
JobSkill.all JobSkill.all
.where(conditions) .where(conditions)
.where(sub: true) .where(sub: true)
) )
.or( .or(
JobSkill.all JobSkill.all
.where(conditions) .where(conditions)
.where(job: job.base_job.id, base: true) .where(job: job.base_job.id, base: true)
.where.not(job: job.id) .where.not(job: job.id)
) )
end end
@count = @skills.length count = skills.length
@skills = @skills.paginate(page: search_params[:page], per_page: 10) paginated = skills.paginate(page: search_params[:page], per_page: PER_PAGE)
render json: JobSkillBlueprint.render(paginated, meta: {
count: count,
total_pages: total_pages(count)
})
end end
private private
def total_pages(count)
count.to_f / PER_PAGE > 1 ? (count.to_f / PER_PAGE).ceil : 1
end
# Specify whitelisted properties that can be modified. # Specify whitelisted properties that can be modified.
def search_params def search_params
params.require(:search).permit! params.require(:search).permit!