From 98c3eee313c4dc52cfee5a1216042155dddc34dd Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Thu, 18 Dec 2025 23:15:37 -0800 Subject: [PATCH] add sorting support to search endpoints --- app/controllers/api/v1/search_controller.rb | 46 +++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/v1/search_controller.rb b/app/controllers/api/v1/search_controller.rb index e4339ed..3543ab8 100644 --- a/app/controllers/api/v1/search_controller.rb +++ b/app/controllers/api/v1/search_controller.rb @@ -79,9 +79,16 @@ module Api Character.en_search(search_params[:query]).where(conditions) end 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 + # Filter by series (array overlap) if filters && filters['series'].present? && !filters['series'].empty? series_values = Array(filters['series']).map(&:to_i) @@ -125,9 +132,16 @@ module Api Weapon.en_search(search_params[:query]).where(conditions) end 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 + # Filter by promotions (array overlap) if filters && filters['promotions'].present? && !filters['promotions'].empty? promotions_values = Array(filters['promotions']).map(&:to_i) @@ -161,9 +175,16 @@ module Api Summon.en_search(search_params[:query]).where(conditions) end 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 + # Filter by promotions (array overlap) if filters && filters['promotions'].present? && !filters['promotions'].empty? promotions_values = Array(filters['promotions']).map(&:to_i) @@ -286,6 +307,25 @@ module Api return {} unless params[:search].present? params.require(:search).permit! 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