6 changed files with 61 additions and 2 deletions
@ -0,0 +1,21 @@
|
||||
module Api |
||||
class SubjectsController < BaseController |
||||
include SubjectSearch |
||||
|
||||
authorize_resource :doctrine, class: "Cdao::Subject", only: %i[index] |
||||
|
||||
def index |
||||
@search = search_subject(search_params) |
||||
|
||||
@subjects = @search.results |
||||
|
||||
respond_with @subjects |
||||
end |
||||
|
||||
private |
||||
def search_params |
||||
params.permit(:name, :parent_id, :state, doctrine_ids: [], exclude_ids: []) |
||||
end |
||||
end |
||||
|
||||
end |
||||
@ -0,0 +1,26 @@
|
||||
module SubjectSearch |
||||
def search_subject(search_params) |
||||
fulltext_fields = %i[name lineage_name].freeze |
||||
|
||||
search = ::Cdao::Subject.search do |
||||
fulltext search_params[:q], fields: fulltext_fields if search_params[:q].present? |
||||
|
||||
fulltext_fields.each do |field| |
||||
fulltext search_params[field], fields: %i[field], query_phrase_slop: 0, minimum_match: 1 |
||||
end |
||||
|
||||
with :parent_id, search_params[:parent_id].to_i if search_params[:parent_id].present? |
||||
|
||||
without :parent_id if search_params[:is_root].present? && search_params[:is_root].to_s.eql?("true") |
||||
|
||||
without(:id).any_of(search_params[:exclude_ids]) if search_params[:exclude_ids].present? |
||||
|
||||
order_by :lineage_name, :asc |
||||
order_by :name, :asc |
||||
|
||||
paginate page: search_params[:page] || 1, per_page: search_params[:per_page] || 20 |
||||
end |
||||
|
||||
search |
||||
end |
||||
end |
||||
Loading…
Reference in new issue