Skip to content

Commit

Permalink
oni updates
Browse files Browse the repository at this point in the history
  • Loading branch information
johnf committed Oct 25, 2024
1 parent 22ee4b2 commit 7ee3c6f
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions app/controllers/api/v1/oni_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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'
Expand All @@ -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
Expand All @@ -107,16 +114,17 @@ 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: '(.*)'))
unless md
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

Expand Down

0 comments on commit 7ee3c6f

Please sign in to comment.