Class: ForestAdminRpcAgent::Routes::Aggregate

Inherits:
BaseRoute
  • Object
show all
Includes:
ForestAdminAgent::Routes::QueryHandler, ForestAdminAgent::Utils, ForestAdminDatasourceToolkit::Components::Query
Defined in:
lib/forest_admin_rpc_agent/routes/aggregate.rb

Instance Method Summary collapse

Methods inherited from BaseRoute

#build_rails_response, #register_rails, #register_sinatra, #registered

Constructor Details

#initializeAggregate

Returns a new instance of Aggregate.



10
11
12
# File 'lib/forest_admin_rpc_agent/routes/aggregate.rb', line 10

def initialize
  super('rpc/:collection_name/aggregate', 'post', 'rpc_aggregate')
end

Instance Method Details

#handle_request(args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/forest_admin_rpc_agent/routes/aggregate.rb', line 14

def handle_request(args)
  return {} unless args[:params]['collection_name']

  datasource = ForestAdminRpcAgent::Facades::Container.datasource
  collection = get_collection_safe(datasource, args[:params]['collection_name'])

  aggregation = Aggregation.new(
    operation: args[:params]['aggregation']['operation'],
    field: args[:params]['aggregation']['field'],
    groups: args[:params]['aggregation']['groups']
  )
  filter = FilterFactory.from_plain_object(args[:params]['filter'])

  collection.aggregate(args[:caller], filter, aggregation, args[:params]['limit'])
end