Class: ForestAdminAgent::Routes::Resources::Count

Inherits:
AbstractAuthenticatedRoute show all
Includes:
Builder, QueryHandler, ForestAdminDatasourceToolkit::Components::Query::ConditionTree
Defined in:
lib/forest_admin_agent/routes/resources/count.rb

Instance Method Summary collapse

Methods included from QueryHandler

#execute_query, #inject_context_variables, #parse_query_segment

Methods inherited from AbstractAuthenticatedRoute

#build, #format_attributes, #redacted_full_projection, #redacted_projection_with_pks

Methods inherited from AbstractRoute

#add_route, #build, #initialize, #routes

Constructor Details

This class inherits a constructor from ForestAdminAgent::Routes::AbstractRoute

Instance Method Details

#handle_request(args = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/forest_admin_agent/routes/resources/count.rb', line 17

def handle_request(args = {})
  context = build(args)
  context.permissions.can?(:browse, context.collection)

  if context.collection.is_countable?
    condition_tree = ForestAdminAgent::Utils::QueryStringParser.parse_condition_tree(
      context.collection, args
    )
    search = QueryStringParser.parse_search(context.collection, args)
    search_extended = QueryStringParser.parse_search_extended(args)
    context.permissions.assert_can_read_query_fields(
      context.collection,
      condition_tree: condition_tree, search: search, search_extended: search_extended
    )

    filter = ForestAdminDatasourceToolkit::Components::Query::Filter.new(
      condition_tree: ConditionTreeFactory.intersect(
        [
          context.permissions.get_scope(context.collection),
          parse_query_segment(context.collection, args, context.permissions, context.caller),
          condition_tree
        ]
      ),
      search: search,
      search_extended: search_extended,
      segment: QueryStringParser.parse_segment(context.collection, args)
    )
    aggregation = ForestAdminDatasourceToolkit::Components::Query::Aggregation.new(operation: 'Count')
    result = context.collection.aggregate(context.caller, filter, aggregation)

    return {
      name: args[:params]['collection_name'],
      content: {
        count: result.empty? ? 0 : result[0]['value']
      }
    }
  end

  {
    name: args[:params]['collection_name'],
    content: {
      count: 'deactivated'
    }
  }
end

#setup_routesObject



11
12
13
14
15
# File 'lib/forest_admin_agent/routes/resources/count.rb', line 11

def setup_routes
  add_route('forest_count', 'get', '/:collection_name/count', ->(args) { handle_request(args) })

  self
end