class ItemBankQuery < ApplicationQuery include CustomSortable attr_reader :params sort_columns :updated_at, default_by: :updated_at, default_direction: :desc, default_table: 'item_banks' def initialize(params) @params = params end def call if params[:public].to_i == 1 items = ItemBank.where(public: 1) elsif params[:public].to_i == 0 items = ItemBank.where(user_id: User.current.id) end if params[:tag_repertoire_id].present? items = items.joins(:item_bank_tag_repertoires).where(item_bank_tag_repertoires: {tag_repertoire_id: params[:tag_repertoire_id]}) elsif params[:sub_repertoire_id].present? items = items.where(sub_repertoire_id: params[:sub_repertoire_id]) elsif params[:repertoire_id].present? items = items.joins(:sub_repertoire).where(sub_repertoires: {repertoire_id: params[:repertoire_id]}) end items = items.where(item_type: params[:item_type].to_i) if params[:item_type].present? items = items.where(difficulty: params[:difficulty].to_i) if params[:difficulty].present? items = items.where("name like ?", "%#{params[:keyword].strip}%") if params[:keyword].present? custom_sort(items, params[:sort_by], params[:sort_direction]) end end