Merge pull request #11 from jedmund/search
Implement filtering objects on the backend
This commit is contained in:
commit
c027a473ea
9 changed files with 112 additions and 37 deletions
4
Gemfile
4
Gemfile
|
|
@ -37,8 +37,12 @@ gem 'awesome_nested_set'
|
|||
# An email validator for Rails
|
||||
gem "email_validator"
|
||||
|
||||
# pg_search builds ActiveRecord named scopes that take advantage of PostgreSQL’s full text search
|
||||
gem 'pg_search'
|
||||
|
||||
# Pagination library
|
||||
gem 'will_paginate', '~> 3.3'
|
||||
|
||||
group :doc do
|
||||
gem 'sdoc'
|
||||
gem 'apipie-rails'
|
||||
|
|
|
|||
|
|
@ -272,6 +272,7 @@ GEM
|
|||
websocket-driver (0.7.5)
|
||||
websocket-extensions (>= 0.1.0)
|
||||
websocket-extensions (0.1.5)
|
||||
will_paginate (3.3.1)
|
||||
yard (0.9.26)
|
||||
zeitwerk (2.5.1)
|
||||
|
||||
|
|
@ -313,6 +314,7 @@ DEPENDENCIES
|
|||
solargraph
|
||||
spring
|
||||
spring-commands-rspec
|
||||
will_paginate (~> 3.3)
|
||||
|
||||
RUBY VERSION
|
||||
ruby 3.0.0p0
|
||||
|
|
|
|||
|
|
@ -1,44 +1,85 @@
|
|||
class Api::V1::SearchController < Api::V1::ApiController
|
||||
def characters
|
||||
locale = params[:locale] || 'en'
|
||||
filters = search_params[:filters]
|
||||
locale = search_params[:locale] || 'en'
|
||||
conditions = {}
|
||||
|
||||
if params[:query].present?
|
||||
if locale == 'ja'
|
||||
@characters = Character.jp_search(params[:query]).limit(10)
|
||||
else
|
||||
# @characters = Character.where("name_en ILIKE ? AND name_en NOT ILIKE ALL(ARRAY[?])", "%#{params[:query]}%", excludes).limit(10)
|
||||
@characters = Character.en_search(params[:query]).limit(10)
|
||||
end
|
||||
else
|
||||
@characters = Character.all
|
||||
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)
|
||||
end
|
||||
|
||||
def weapons
|
||||
locale = params[:locale] || 'en'
|
||||
filters = search_params[:filters]
|
||||
locale = search_params[:locale] || 'en'
|
||||
conditions = {}
|
||||
|
||||
if params[:query].present?
|
||||
if locale == 'ja'
|
||||
@weapons = Weapon.jp_search(params[:query]).limit(10)
|
||||
else
|
||||
@weapons = Weapon.en_search(params[:query]).limit(10)
|
||||
end
|
||||
else
|
||||
@weapons = Weapon.all
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
@count = @weapons.length
|
||||
@weapons = @weapons.paginate(page: search_params[:page], per_page: 10)
|
||||
end
|
||||
|
||||
def summons
|
||||
locale = params[:locale] || 'en'
|
||||
filters = search_params[:filters]
|
||||
locale = search_params[:locale] || 'en'
|
||||
conditions = {}
|
||||
|
||||
if params[:query].present?
|
||||
if locale == 'ja'
|
||||
@summons = Summon.jp_search(params[:query]).limit(10)
|
||||
else
|
||||
@summons = Summon.en_search(params[:query]).limit(10)
|
||||
end
|
||||
else
|
||||
@summons = Summon.all
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
@count = @summons.length
|
||||
@summons = @summons.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
|
||||
|
|
@ -1,3 +1,11 @@
|
|||
collection @characters, :object_root => false
|
||||
node :count do
|
||||
@count
|
||||
end
|
||||
|
||||
extends 'characters/base'
|
||||
node :total_pages do
|
||||
(@count.to_f / 10 > 1) ? (@count.to_f / 10).ceil() : 1
|
||||
end
|
||||
|
||||
node(:results) {
|
||||
partial('characters/base', object: @characters)
|
||||
} unless @characters.empty?
|
||||
|
|
|
|||
|
|
@ -1,3 +1,11 @@
|
|||
collection @summons, :object_root => false
|
||||
node :count do
|
||||
@count
|
||||
end
|
||||
|
||||
extends 'summons/base'
|
||||
node :total_pages do
|
||||
(@count.to_f / 10 > 1) ? (@count.to_f / 10).ceil() : 1
|
||||
end
|
||||
|
||||
node(:results) {
|
||||
partial('summons/base', object: @summons)
|
||||
} unless @summons.empty?
|
||||
|
|
|
|||
|
|
@ -1,3 +1,11 @@
|
|||
collection @weapons, :object_root => false
|
||||
node :count do
|
||||
@count
|
||||
end
|
||||
|
||||
extends 'weapons/base'
|
||||
node :total_pages do
|
||||
(@count.to_f / 10 > 1) ? (@count.to_f / 10).ceil() : 1
|
||||
end
|
||||
|
||||
node(:results) {
|
||||
partial('weapons/base', object: @weapons)
|
||||
} unless @weapons.empty?
|
||||
|
|
|
|||
|
|
@ -22,9 +22,9 @@ Rails.application.routes.draw do
|
|||
post 'check/email', to: 'users#check_email'
|
||||
post 'check/username', to: 'users#check_username'
|
||||
|
||||
get 'search/characters', to: 'search#characters'
|
||||
get 'search/weapons', to: 'search#weapons'
|
||||
get 'search/summons', to: 'search#summons'
|
||||
post 'search/characters', to: 'search#characters'
|
||||
post 'search/weapons', to: 'search#weapons'
|
||||
post 'search/summons', to: 'search#summons'
|
||||
|
||||
get 'raids', to: 'raids#all'
|
||||
get 'weapon_keys', to: 'weapon_keys#all'
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ ActiveRecord::Schema.define(version: 2022_03_09_013333) do
|
|||
enable_extension "pg_trgm"
|
||||
enable_extension "pgcrypto"
|
||||
enable_extension "plpgsql"
|
||||
enable_extension "timescaledb"
|
||||
|
||||
create_table "characters", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
|
||||
t.string "name_en"
|
||||
|
|
@ -99,6 +100,9 @@ ActiveRecord::Schema.define(version: 2022_03_09_013333) do
|
|||
t.integer "element"
|
||||
t.index ["party_id"], name: "index_grid_weapons_on_party_id"
|
||||
t.index ["weapon_id"], name: "index_grid_weapons_on_weapon_id"
|
||||
t.index ["weapon_key1_id"], name: "index_grid_weapons_on_weapon_key1_id"
|
||||
t.index ["weapon_key2_id"], name: "index_grid_weapons_on_weapon_key2_id"
|
||||
t.index ["weapon_key3_id"], name: "index_grid_weapons_on_weapon_key3_id"
|
||||
end
|
||||
|
||||
create_table "oauth_access_grants", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
|
||||
|
|
|
|||
Loading…
Reference in a new issue