Browse Source

Implement Doctrine API

pull/17/head
Angel Aviel Domaoan 4 years ago committed by Angel Aviel Domaoan
parent
commit
fb9e17bdc6
  1. 16
      app/controllers/api/doctrines_controller.rb
  2. 15
      app/controllers/api/documents_controller.rb
  3. 9
      app/models/doctrine.rb
  4. 3
      app/views/api/doctrines/index.json.jbuilder
  5. 1
      app/views/api/doctrines/show.json.jbuilder
  6. 6
      config/application.yml
  7. 2
      config/routes.rb

16
app/controllers/api/doctrines_controller.rb

@ -1,6 +1,7 @@
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
@ -11,25 +12,34 @@ module Api
respond_with @doctrines
end
def show
respond_with @doctrine
end
private
def doctrine_search(search_params)
search = Sunspot.search(Doctrine) do
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)
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(subject_ids: [], user_ids: [], :created_at)
params.permit(:created_at, :q, :page, :per_page, subject_ids: [], user_ids: [])
end
end
end

15
app/controllers/api/documents_controller.rb

@ -1,15 +0,0 @@
module Api
class DocumentsController < BaseController
include JurisprudenceSearch
authorize_resource :document, class: "Cdao::Document", only: %i[index]
def index
search = jurisprudence_search(search_params)
@jurisprudences = search.results
respond_with @jurisprudences
end
end
end

9
app/models/doctrine.rb

@ -44,9 +44,16 @@ class Doctrine < ApplicationRecord
end
searchable do
text :content
integer :document_id
integer :subject_ids, multiple: true
text :content
integer :user_ids, multiple: true do
paper_trail.originator.to_i
end
date :created_at
end
end

3
app/views/api/doctrines/index.json.jbuilder

@ -0,0 +1,3 @@
json.array!(@doctrines) do |doctrine|
json.extract! doctrine, *%i[id content subject_ids created_at updated_at]
end

1
app/views/api/doctrines/show.json.jbuilder

@ -0,0 +1 @@
json.(@doctrine, *%i[id content subject_ids created_at updated_at])

6
config/application.yml

@ -1,7 +1,7 @@
DATABASE_URL: '<%= Rails.application.credentials.DATABASE_URL %>'
DATABASE_URL_CDAO: '<%= Rails.application.credentials.DATABASE_URL_CDAO %>'
SOLR_URL: '<%= Rails.application.credentials.SOLR_URL %>'
# SOLR_URL: '<%= Rails.application.credentials.SOLR_URL %>'
SENTRY_DSN: '<%= Rails.application.credentials.SENTRY_DSN %>'
REPORT_URI_URL: '<%= Rails.application.credentials.REPORT_URI_URL %>'
@ -9,3 +9,7 @@ REPORT_URI_URL: '<%= Rails.application.credentials.REPORT_URI_URL %>'
RECAPTCHA_ENABLED: '<%= Rails.application.credentials.RECAPTCHA_ENABLED %>'
RAILS_SERVE_STATIC_FILES: "true"
REDIS_URL: "redis://redis:6379/1"
SOLR_URL: "http://localhost:8983/solr/default"

2
config/routes.rb

@ -32,6 +32,8 @@ Rails.application.routes.draw do
end
namespace :api, defaults: { format: :json } do
resources :doctrines, only: %i[index show]
resources :jurisprudences, only: %i[index show] do
get :years, on: :collection

Loading…
Cancel
Save