diff --git a/lib/decentralised_book_index/metadata.ex b/lib/decentralised_book_index/metadata.ex index e8000e6..d150d67 100644 --- a/lib/decentralised_book_index/metadata.ex +++ b/lib/decentralised_book_index/metadata.ex @@ -2,11 +2,19 @@ defmodule DecentralisedBookIndex.Metadata do use Ash.Domain, otp_app: :decentralised_book_index + alias DecentralisedBookIndex.Metadata + resources do resource DecentralisedBookIndex.Metadata.Book do define :create_book, action: :create, - args: [:title, :isbn, :description, :author_roles, {:optional, :book_editions_registry_id}] + args: [ + :title, + :isbn, + :description, + :author_roles, + {:optional, :book_editions_registry_id} + ] define :add_book_to_related_editions_registry, action: :add_book_to_related_editions_registry, @@ -31,6 +39,7 @@ defmodule DecentralisedBookIndex.Metadata do define :list_authors, action: :read define :get_author_by_id, args: [:id], action: :by_id define :get_author_alternative_names, args: [:author], action: :get_alternative_names + define :search_author, action: :search, args: [:name] define :update_author, action: :update define :destroy_author, action: :destroy end diff --git a/lib/decentralised_book_index/metadata/author.ex b/lib/decentralised_book_index/metadata/author.ex index 478a2ab..22d8036 100644 --- a/lib/decentralised_book_index/metadata/author.ex +++ b/lib/decentralised_book_index/metadata/author.ex @@ -45,9 +45,14 @@ defmodule DecentralisedBookIndex.Metadata.Author do if related_author_id == nil do Ash.Changeset.add_error(changeset, :related_author_id, "Related author is empty") else - {:ok, related_author} = DecentralisedBookIndex.Metadata.get_author_by_id(related_author_id) - - Ash.Changeset.force_change_attribute(changeset, :author_alias_registry_id, related_author.author_alias_registry_id) + {:ok, related_author} = + DecentralisedBookIndex.Metadata.get_author_by_id(related_author_id) + + Ash.Changeset.force_change_attribute( + changeset, + :author_alias_registry_id, + related_author.author_alias_registry_id + ) end end end @@ -65,9 +70,22 @@ defmodule DecentralisedBookIndex.Metadata.Author do author = query.arguments.author DecentralisedBookIndex.Metadata.Author - |> Ash.Query.filter(author_alias_registry_id == ^author.author_alias_registry_id - and id != ^author.id) + |> Ash.Query.filter( + author_alias_registry_id == ^author.author_alias_registry_id and + id != ^author.id + ) + end + end + + read :search do + argument :name, :ci_string do + constraints allow_empty?: true + default "" end + + filter expr(contains(name, ^arg(:name))) + + pagination offset?: true, default_limit: 10 end end