From 165144f1118ad9be7743cb99198a2635ed4be0b8 Mon Sep 17 00:00:00 2001 From: John Ferlito Date: Sun, 10 Nov 2024 19:09:48 +1100 Subject: [PATCH] Add counts to oni query --- app/controllers/api/v1/oni_controller.rb | 12 ++++++++++-- app/views/api/v1/oni/objects.json.jb | 12 +++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/v1/oni_controller.rb b/app/controllers/api/v1/oni_controller.rb index dc031191..49a200e3 100644 --- a/app/controllers/api/v1/oni_controller.rb +++ b/app/controllers/api/v1/oni_controller.rb @@ -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' diff --git a/app/views/api/v1/oni/objects.json.jb b/app/views/api/v1/oni/objects.json.jb index 708c6745..a4e1814d 100644 --- a/app/views/api/v1/oni/objects.json.jb +++ b/app/views/api/v1/oni/objects.json.jb @@ -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 = { @@ -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 }