Skip to content

Instantly share code, notes, and snippets.

@elrayle
Created July 14, 2020 22:08
Show Gist options
  • Save elrayle/246a8bc4b118ee66660259d8a55fecee to your computer and use it in GitHub Desktop.
Save elrayle/246a8bc4b118ee66660259d8a55fecee to your computer and use it in GitHub Desktop.
# Wings (aka ActiveFedora) specific implementation that finds a publication given its unique identifier.
module CustomQueries
module Wings
class FindPublicationByIdentifier
# Define the queries that can be fulfilled by this custom query.
def self.queries
[:find_publication_by_identifier]
end
attr_reader :query_service
delegate :resource_factory, to: :query_service
def initialize(query_service:)
@query_service = query_service
end
##
# Find by identifier (e.g. slug_id)
# @param identifier [String] agency identifier (e.g. slug_id, other string assigned by agency)
# @return [Array<Valkyrie::Resource>]
def find_publication_by_identifier(identifier:)
af_find_publication_by_identifier(identifier: identifier)
end
private
# Find by identifier (e.g. slug_id)
# @param identifier [String] agency identifier (e.g. slug_id, other string assigned by agency)
# @return [Array<Valkyrie::Resource>]
# @raise [Hyrax::ObjectNotFoundError]
def af_find_publication_by_identifier(identifier:)
results = Publication.where(identifier: [identifier])
raise Hyrax::ObjectNotFoundError, "Publication with identifier '#{identifier}' not found." unless results.count.positive?
resource_factory.to_resource(object: results.first)
rescue ActiveFedora::ObjectNotFoundError, Ldp::Gone
raise Hyrax::ObjectNotFoundError, "Publication with identifier '#{identifier}' not found." unless results.count.positive?
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment