unepwcmc/SAPI

View on GitHub
app/controllers/api/v1/document_geo_entities_controller.rb

Summary

Maintainability
A
0 mins
Test Coverage
class Api::V1::DocumentGeoEntitiesController < ApplicationController
  def index
    @geo_entities = GeoEntity.current.includes(:geo_entity_type).
      order("name_#{I18n.locale}")
    @geo_entities = @geo_entities.
      joins(:geo_entity_type).
      where(:"geo_entity_types.name" => GeoEntityType::SETS['5'])

    if params[:taxon_concept_query].present?
      @species_search = Species::Search.new({
        visibility: :elibrary,
        taxon_concept_query: params[:taxon_concept_query]
      })
      @geo_entities = @geo_entities.joins(
        document_citation_geo_entities: {
          document_citation: :document_citation_taxon_concepts
        }
      ).where(
        'document_citation_taxon_concepts.taxon_concept_id' => @species_search.ids
      )
    end

    render :json => @geo_entities,
      each_serializer: Species::GeoEntitySerializer,
      meta: { total: @geo_entities.count }
  end
end