add sorting support to search endpoints

This commit is contained in:
Justin Edmund 2025-12-18 23:15:37 -08:00
parent b3dadf24ef
commit 98c3eee313

View file

@ -79,7 +79,14 @@ module Api
Character.en_search(search_params[:query]).where(conditions) Character.en_search(search_params[:query]).where(conditions)
end end
else else
Character.where(conditions).order(Arel.sql('greatest(release_date, flb_date, ulb_date) desc')) Character.where(conditions)
end
# Apply sorting if specified, otherwise use default
if search_params[:sort].present?
characters = apply_sort(characters, search_params[:sort], search_params[:order], locale)
elsif search_params[:query].blank?
characters = characters.order(Arel.sql('greatest(release_date, flb_date, ulb_date) desc'))
end end
# Filter by series (array overlap) # Filter by series (array overlap)
@ -125,7 +132,14 @@ module Api
Weapon.en_search(search_params[:query]).where(conditions) Weapon.en_search(search_params[:query]).where(conditions)
end end
else else
Weapon.where(conditions).order(Arel.sql('greatest(release_date, flb_date, ulb_date, transcendence_date) desc')) Weapon.where(conditions)
end
# Apply sorting if specified, otherwise use default
if search_params[:sort].present?
weapons = apply_sort(weapons, search_params[:sort], search_params[:order], locale)
elsif search_params[:query].blank?
weapons = weapons.order(Arel.sql('greatest(release_date, flb_date, ulb_date, transcendence_date) desc'))
end end
# Filter by promotions (array overlap) # Filter by promotions (array overlap)
@ -161,7 +175,14 @@ module Api
Summon.en_search(search_params[:query]).where(conditions) Summon.en_search(search_params[:query]).where(conditions)
end end
else else
Summon.where(conditions).order(release_date: :desc).order(Arel.sql('greatest(release_date, flb_date, ulb_date, transcendence_date) desc')) Summon.where(conditions)
end
# Apply sorting if specified, otherwise use default
if search_params[:sort].present?
summons = apply_sort(summons, search_params[:sort], search_params[:order], locale)
elsif search_params[:query].blank?
summons = summons.order(Arel.sql('greatest(release_date, flb_date, ulb_date, transcendence_date) desc'))
end end
# Filter by promotions (array overlap) # Filter by promotions (array overlap)
@ -286,6 +307,25 @@ module Api
return {} unless params[:search].present? return {} unless params[:search].present?
params.require(:search).permit! params.require(:search).permit!
end end
# Apply sorting based on column name and order
def apply_sort(scope, column, order, locale)
sort_dir = order == 'desc' ? :desc : :asc
case column
when 'name'
name_col = locale == 'ja' ? :name_ja : :name_en
scope.order(name_col => sort_dir)
when 'element'
scope.order(element: sort_dir)
when 'rarity'
scope.order(rarity: sort_dir)
when 'last_updated'
scope.order(updated_at: sort_dir)
else
scope
end
end
end end
end end
end end