You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

53 lines
1.6 KiB

module Api
class DocumentsController < BaseController
load_and_authorize_resource :document, class: "Cdao::Document", only: %i[show]
authorize_resource :document, class: "Cdao::Document", only: %i[index]
def index
search = document_search(search_params)
@documents = search.results
respond_with @documents
end
def show
respond_with @document
end
private
def document_search(search_params)
fulltext_fields = %i[reference_number title short_title].freeze
search = Cdao::Document.search do
fulltext search_params[:q], fields: fulltext_fields, query_phrase_slop: 1, minimum_match: 1 if search_params[:q].present?
fulltext_fields.each do |field|
fulltext search_params[field], fields: [field], query_phrase_slop: 1, minimum_match: 1 if search_params[field].present?
end
with(:year, search_params[:year].to_i) if search_params[:year].present?
without(:id).any_of(search_params[:exclude_ids]) if search_params[:exclude_ids].present?
with(:citation_finders_names).any_of(search_params[:citation_finder]) if search_params[:citation_finder].present?
with(:is_only_in_premium_libraries, false)
order_by :doc_date, :desc
order_by :year, :desc
paginate page: search_params[:page] || 1, per_page: search_params[:per_page] || 20
end
search
end
def search_params
params.permit(:reference_number, :title, :short_title, :year, :citation_finder,
:q, :page, :per_page,
exclude_ids: [])
end
end
end