Forked from
PortalMEC / portalmec
524 commits behind the upstream repository.
-
Israel Barreto Sant'Anna authoredIsrael Barreto Sant'Anna authored
search_controller.rb 2.24 KiB
# Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana
#
# This file is part of portalmec.
#
# portalmec is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# portalmec is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with portalmec. If not, see <http://www.gnu.org/licenses/>.
include TagSearchService
class V1::SearchController < ApplicationController
include ::Paginator
before_action :set_search, except: [:tag]
# GET v1/search
# GET v1/search.json
def index
search = SearchService.instance(@search, current_user).search
headers['X-Total-Count'] = search.total_count
render json: search.results, status: :ok
rescue SearchService::InvalidSearchError
render json: @search.errors, status: :bad_request
end
# GET v1/search/autocomplete
# GET v1/search/autocomplete.json
def autocomplete
render json: SearchService.autocomplete(@search, current_user), status: :ok
rescue SearchService::InvalidSearchError
render json: @search.errors, status: :bad_request
end
# GET v1/search/tag
# GET v1/search/tag.json
def tag
t = Tag.find_by_name(tag_search_params[:name])
if t.nil?
render json: {"error": "Tag not found."}, status: :unprocessable_entity
else
results = TagSearchService.search(t)
render json: paginate(results), status: :ok
end
end
private
def set_search
@search = Search.new(search_params)
end
# Never trust parameters from the scary internet, only allow the white list through.
def search_params
params.permit(:page, :results_per_page, :order, :query, :search_class, tags: [], subjects: [], educational_stages: [], object_types: [])
end
def tag_search_params
params.permit(:page, :results_per_page, :name)
end
end