module Api module Doctrine class AnnotationsController < ::Api::BaseController include AnnotationSearch load_and_authorize_resource :doctrine, class: "Doctrine" load_and_authorize_resource :annotation, class: "Annotation", through: :doctrine def index search = annotation_search(search_params) @annotations = search.results respond_with @annotations end def search search = annotation_search(search_params) @annotations = search.results respond_with @annotations end def show respond_with @annotation end def create attrs = resource_params.to_unsafe_h.deep_symbolize_keys document_id = attrs.delete(:document_id) document_ids = params[:document_ids].split(",") @annotation = @doctrine.annotations.new(attrs) if document_id.present? @annotation.document = Cdao::Document.find(document_id) end if document_ids.present? @documents = Cdao::Document.where(id: document_ids) end if @annotation.save @documents.each { |document| @annotation.add_document(document) } if @documents.present? respond_with @annotation else render errors: @annotation.errors, status: 422 end end def update attrs = resource_params.to_unsafe_h.deep_symbolize_keys document_id = attrs.delete(:document_id) document_ids = params[:document_ids].split(",") if document_ids.present? @documents = Cdao::Document.where(id: document_ids) end attrs[:document] = Cdao::Document.find(document_id) if document_id.present? if @annotation.update(attrs) @annotation.annotation_documents.each do |annotation_document| @annotation.remove_document(annotation_document.document) end @documents.each { |document| @annotation.add_document(document) } if @documents.present? respond_with @annotation else render errors: @annotation.errors, status: 422 end end def destroy @annotation.destroy respond_with @annotation end private def resource_params params.permit(:document_id, :content, :phil_rep, :editor_notes, :rank, annomark_ids: []) end def search_params params.permit(:doctrine_id, :document_id, :phil_rep, :q, :page, :per_page) end end end end