diff --git a/app/controllers/api/v1/oni_controller.rb b/app/controllers/api/v1/oni_controller.rb index f44e4fc7..d9345f6d 100644 --- a/app/controllers/api/v1/oni_controller.rb +++ b/app/controllers/api/v1/oni_controller.rb @@ -5,6 +5,7 @@ def objects limit = (params[:limit] || 5_000).to_i # FIXME: Better way for all record offset = (params[:offset] || 0).to_i sortBy = params[:sortBy] || 'identifier' + memberOf = params[:memberOf] unless %w[identifier title created_at].include?(sortBy) orderBy = 'identifier' end @@ -28,25 +29,31 @@ def objects .project(items_table[:id], items_table[:created_at], item_identifier, items_table[:title], item_label.as('type')) .where(items_table[:private].eq(false)) - if params[:memberOf] + if memberOf md = params[:memberOf].match(repository_collection_url(collection_identifier: '(.*)')) unless md render json: { error: 'Invalid memberOf parameter' }, status: :bad_request return end - collections_query = collections_query.where(collections_table[:identifier].eq(md[1])).project(:id) - combined_query = items_query.where(items_table[:collection_id].in(collections_query)) - else - collections_query = collections_query.project(:id, :created_at, :identifier, :title, collection_label.as('type')) - combined_query = case conforms_to - when 'https://purl.archive.org/language-data-commons/profile#Collection' - collections_query - when 'https://purl.archive.org/language-data-commons/profile#Item' - items_query + collections_query = collections_query.where(collections_table[:identifier].eq(md[1])) + items_query = items_query.where(items_table[:collection_id].in(collections_query.clone.project(collections_table[:id]))) + end + + collections_query = collections_query.project(:id, :created_at, :identifier, :title, collection_label.as('type')) + + combined_query = case conforms_to + when 'https://w3id.org/ldac/profile#Collection' + # A bit hacky but we dont' have colletctions of collections + if memberOf + collections_query.where(collections_table[:identifier].eq('DUMMYsajkdhakshfvksfslkj')) else - collections_query.union(items_query) + collections_query end + when 'https://w3id.org/ldac/profile#Item' + items_query + else + collections_query.union(items_query) end # Count query to get the total number of records @@ -66,7 +73,7 @@ def objects 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) + items = Item.where(id: item_ids).includes(:collection, :access_condition, :content_languages) @data = ids.map do |id| if id['type'] == 'collection' @@ -86,8 +93,8 @@ def object md = params[:id].match(repository_item_url(collection_identifier: '(.*)', item_identifier: '(.*)')) if md - @collection = Collection.find_by(identifier: md[1]) - @data = @collection.items.find_by(identifier: md[2]) + @collection = Collection.where(private: false).find_by(identifier: md[1]) + @data = @collection.items.where(private: false).find_by(identifier: md[2]) else md = params[:id].match(repository_collection_url(collection_identifier: '(.*)')) unless md @@ -107,8 +114,9 @@ def object_meta md = params[:id].match(repository_item_url(collection_identifier: '(.*)', item_identifier: '(.*)')) if md - @collection = Collection.find_by(identifier: md[1]) - @data = @collection.items.includes(:content_languages, :subject_languages, item_agents: %i[agent_role user]).find_by(identifier: md[2]) + @collection = Collection.where(private: false).find_by(identifier: md[1]) + @data = @collection.items.where(private: false).includes(:content_languages, :subject_languages, +item_agents: %i[agent_role user]).find_by(identifier: md[2]) @is_item = true else md = params[:id].match(repository_collection_url(collection_identifier: '(.*)')) @@ -116,7 +124,7 @@ def object_meta render json: { error: 'Invalid id parameter' }, status: :bad_request return end - @data = Collection.find_by(identifier: md[1]) + @data = Collection.where(private: false).find_by(identifier: md[1]) @is_item = false end