Skip to content

Commit

Permalink
Add counts to oni query
Browse files Browse the repository at this point in the history
  • Loading branch information
johnf committed Nov 10, 2024
1 parent f7bfb41 commit 165144f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
12 changes: 10 additions & 2 deletions app/controllers/api/v1/oni_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,16 @@ def objects
collection_ids = ids.select { |id| id['type'] == 'collection' }.pluck('id')
item_ids = ids.select { |id| id['type'] == 'item' }.pluck('id')

collections = Collection.where(id: collection_ids).includes(:access_condition)
items = Item.where(id: item_ids).includes(:collection, :access_condition, :content_languages)
collections = Collection.where(id: collection_ids)
.select('collections.*, COUNT(DISTINCT items.id) AS items_count, COUNT(essences.id) AS essences_count')
.left_joins(items: :essences)
.group('collections.id')
.includes(:access_condition, :languages)
items = Item.where(id: item_ids)
.select('items.*, COUNT(essences.id) AS essences_count')
.left_joins(:essences)
.group('items.id')
.includes(:collection, :access_condition, :content_languages)

@objects = ids.map do |id|
if id['type'] == 'collection'
Expand Down
12 changes: 11 additions & 1 deletion app/views/api/v1/oni/objects.json.jb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
total: @total,

objects: @objects.map do |object|
class_name = data.class.name
class_name = object.class.name
is_item = class_name == 'Item'

response = {
Expand All @@ -21,6 +21,16 @@
response[:root] = repository_collection_url(object.collection)
end

if object.has_attribute?(:items_count)
response[:metadata] ||= {}
response[:metadata][:objectsCount] = object.items_count
end

if object.has_attribute?(:essences_count)
response[:metadata] ||= {}
response[:metadata][:filesCount] = object.essences_count
end

response
end
}

0 comments on commit 165144f

Please sign in to comment.