diff --git a/app/controllers/api/v1/search_controller.rb b/app/controllers/api/v1/search_controller.rb index f8b9f27..9f42fc6 100644 --- a/app/controllers/api/v1/search_controller.rb +++ b/app/controllers/api/v1/search_controller.rb @@ -1,133 +1,139 @@ class Api::V1::SearchController < Api::V1::ApiController - def characters - filters = search_params[:filters] - locale = search_params[:locale] || 'en' - conditions = {} + def characters + filters = search_params[:filters] + locale = search_params[:locale] || 'en' + conditions = {} - if filters - conditions[:rarity] = filters['rarity'] unless filters['rarity'].blank? || filters['rarity'].empty? - conditions[:element] = filters['element'] unless filters['element'].blank? || filters['element'].empty? - conditions[:proficiency1] = filters['proficiency1'] unless filters['proficiency1'].blank? || filters['proficiency1'].empty? - conditions[:proficiency2] = filters['proficiency2'] unless filters['proficiency2'].blank? || filters['proficiency2'].empty? - # conditions[:series] = filters['series'] unless filters['series'].blank? || filters['series'].empty? - end - - if search_params[:query].present? && search_params[:query].length >= 2 - if locale == 'ja' - @characters = Character.jp_search(search_params[:query]).where(conditions) - else - @characters = Character.en_search(search_params[:query]).where(conditions) - end - else - @characters = Character.where(conditions) - end - - @count = @characters.length - @characters = @characters.paginate(page: search_params[:page], per_page: 10) + if filters + conditions[:rarity] = filters['rarity'] unless filters['rarity'].blank? || filters['rarity'].empty? + conditions[:element] = filters['element'] unless filters['element'].blank? || filters['element'].empty? + conditions[:proficiency1] = filters['proficiency1'] unless filters['proficiency1'].blank? || filters['proficiency1'].empty? + conditions[:proficiency2] = filters['proficiency2'] unless filters['proficiency2'].blank? || filters['proficiency2'].empty? + # conditions[:series] = filters['series'] unless filters['series'].blank? || filters['series'].empty? end - def weapons - filters = search_params[:filters] - locale = search_params[:locale] || 'en' - conditions = {} + @characters = if search_params[:query].present? && search_params[:query].length >= 2 + if locale == 'ja' + Character.jp_search(search_params[:query]).where(conditions) + else + Character.en_search(search_params[:query]).where(conditions) + end + else + Character.where(conditions) + end - if filters - conditions[:rarity] = filters['rarity'] unless filters['rarity'].blank? || filters['rarity'].empty? - conditions[:element] = filters['element'] unless filters['element'].blank? || filters['element'].empty? - conditions[:proficiency] = filters['proficiency1'] unless filters['proficiency1'].blank? || filters['proficiency1'].empty? - conditions[:series] = filters['series'] unless filters['series'].blank? || filters['series'].empty? - end + @count = @characters.length + @characters = @characters.paginate(page: search_params[:page], per_page: 10) + end - if search_params[:query].present? && search_params[:query].length >= 2 - if locale == 'ja' - @weapons = Weapon.jp_search(search_params[:query]).where(conditions) - else - @weapons = Weapon.en_search(search_params[:query]).where(conditions) - end - else - @weapons = Weapon.where(conditions) - end + def weapons + filters = search_params[:filters] + locale = search_params[:locale] || 'en' + conditions = {} - @count = @weapons.length - @weapons = @weapons.paginate(page: search_params[:page], per_page: 10) + if filters + conditions[:rarity] = filters['rarity'] unless filters['rarity'].blank? || filters['rarity'].empty? + conditions[:element] = filters['element'] unless filters['element'].blank? || filters['element'].empty? + conditions[:proficiency] = filters['proficiency1'] unless filters['proficiency1'].blank? || filters['proficiency1'].empty? + conditions[:series] = filters['series'] unless filters['series'].blank? || filters['series'].empty? end - def summons - filters = search_params[:filters] - locale = search_params[:locale] || 'en' - conditions = {} + @weapons = if search_params[:query].present? && search_params[:query].length >= 2 + if locale == 'ja' + Weapon.jp_search(search_params[:query]).where(conditions) + else + Weapon.en_search(search_params[:query]).where(conditions) + end + else + Weapon.where(conditions) + end - if filters - conditions[:rarity] = filters['rarity'] unless filters['rarity'].blank? || filters['rarity'].empty? - conditions[:element] = filters['element'] unless filters['element'].blank? || filters['element'].empty? - end + @count = @weapons.length + @weapons = @weapons.paginate(page: search_params[:page], per_page: 10) + end - if search_params[:query].present? && search_params[:query].length >= 2 - if locale == 'ja' - @summons = Summon.jp_search(search_params[:query]).where(conditions) - else - @summons = Summon.en_search(search_params[:query]).where(conditions) - end - else - @summons = Summon.where(conditions) - end + def summons + filters = search_params[:filters] + locale = search_params[:locale] || 'en' + conditions = {} - @count = @summons.length - @summons = @summons.paginate(page: search_params[:page], per_page: 10) + if filters + conditions[:rarity] = filters['rarity'] unless filters['rarity'].blank? || filters['rarity'].empty? + conditions[:element] = filters['element'] unless filters['element'].blank? || filters['element'].empty? end - def job_skills - raise Api::V1::NoJobProvidedError unless search_params[:job].present? + @summons = if search_params[:query].present? && search_params[:query].length >= 2 + if locale == 'ja' + Summon.jp_search(search_params[:query]).where(conditions) + else + Summon.en_search(search_params[:query]).where(conditions) + end + else + Summon.where(conditions) + end - # Set up basic parameters we'll use - job = Job.find(search_params[:job]) - locale = search_params[:locale] || 'en' + @count = @summons.length + @summons = @summons.paginate(page: search_params[:page], per_page: 10) + end - # Set the conditions based on the group requested - conditions = {} - if search_params[:filters].present? && search_params[:filters]["group"].present? - group = search_params[:filters]["group"].to_i + def job_skills + raise Api::V1::NoJobProvidedError unless search_params[:job].present? - if (group >= 0 && group < 4) - conditions[:color] = group - conditions[:emp] = false - conditions[:base] = false - elsif (group == 4) - conditions[:emp] = true - elsif (group == 5) - conditions[:base] = true - end - end + # Set up basic parameters we'll use + job = Job.find(search_params[:job]) + locale = search_params[:locale] || 'en' - # Perform the query - if search_params[:query].present? && search_params[:query].length >= 2 - @skills = JobSkill.method("#{locale}_search").(search_params[:query]) - .where(conditions) - .where(job: job.id, main: false) - .or( - JobSkill.method("#{locale}_search").(search_params[:query]) - .where(conditions) - .where(sub: true) - ) - else - @skills = JobSkill.all - .where(conditions) - .where(job: job.id, main: false) - .or( - JobSkill.all - .where(conditions) - .where(sub:true) - ) - end + # Set the conditions based on the group requested + conditions = {} + if search_params[:filters].present? && search_params[:filters]['group'].present? + group = search_params[:filters]['group'].to_i - @count = @skills.length - @skills = @skills.paginate(page: search_params[:page], per_page: 10) + if group >= 0 && group < 4 + conditions[:color] = group + conditions[:emp] = false + conditions[:base] = false + elsif group == 4 + conditions[:emp] = true + elsif group == 5 + conditions[:base] = true + end end - private + # Perform the query + @skills = if search_params[:query].present? && search_params[:query].length >= 2 + JobSkill.method("#{locale}_search").call(search_params[:query]) + .where(conditions) + .where(job: job.id, main: false) + .or( + JobSkill.method("#{locale}_search").call(search_params[:query]) + .where(conditions) + .where(sub: true) + ) + else + JobSkill.all + .where(conditions) + .where(job: job.id, main: false) + .or( + JobSkill.all + .where(conditions) + .where(sub: true) + ) + .or( + JobSkill.all + .where(conditions) + .where(job: job.base_job.id, base: true) + .where.not(job: job.id) + ) + end - # Specify whitelisted properties that can be modified. - def search_params - params.require(:search).permit! - end + @count = @skills.length + @skills = @skills.paginate(page: search_params[:page], per_page: 10) + end + + private + + # Specify whitelisted properties that can be modified. + def search_params + params.require(:search).permit! + end end