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.
46 lines
1.3 KiB
46 lines
1.3 KiB
|
|
module Api |
|
class DoctrinesController < BaseController |
|
load_and_authorize_resource :doctrine, class: "Doctrine", only: %i[show] |
|
authorize_resource :doctrine, class: "Doctrine", only: %i[index] |
|
|
|
def index |
|
search = doctrine_search(search_params) |
|
|
|
@doctrines = search.results |
|
|
|
respond_with @doctrines |
|
end |
|
|
|
def show |
|
respond_with @doctrine |
|
end |
|
|
|
private |
|
|
|
def doctrine_search(search_params) |
|
fulltext_fields = %i[content].freeze |
|
|
|
search = ::Doctrine.search do |
|
fulltext search_params[:q], fields: fulltext_fields if search_params[:q].present? |
|
|
|
with(:subject_ids).any_of(search_params[:subject_ids]) if search_params[:subject_ids].present? |
|
|
|
with(:user_ids).any_of(search_params[:user_ids]) if search_params[:user_ids].present? |
|
|
|
if search_params[:created_at].present? |
|
with(:created_at).between(Date.parse(search_params[:created_at])..Time.zone.today.to_date) |
|
end |
|
|
|
paginate page: search_params[:page] || 1, per_page: search_params[:per_page] || 20 |
|
end |
|
|
|
search |
|
end |
|
|
|
def search_params |
|
params.permit(:created_at, :q, :page, :per_page, subject_ids: [], user_ids: []) |
|
end |
|
end |
|
end |
|
|