diff --git a/lib/decentralised_book_index/metadata.ex b/lib/decentralised_book_index/metadata.ex index 20d3013..ce97016 100644 --- a/lib/decentralised_book_index/metadata.ex +++ b/lib/decentralised_book_index/metadata.ex @@ -38,6 +38,7 @@ defmodule DecentralisedBookIndex.Metadata do define :list_authors, action: :read define :get_author_by_id, args: [:id], action: :by_id + define :get_author_ids, args: [:author], action: :get_author_ids define :get_author_alternative_names, args: [:author], action: :get_alternative_names define :search_author, action: :search, args: [:name] define :update_author, action: :update diff --git a/lib/decentralised_book_index/metadata/author.ex b/lib/decentralised_book_index/metadata/author.ex index f73fb77..8b3a096 100644 --- a/lib/decentralised_book_index/metadata/author.ex +++ b/lib/decentralised_book_index/metadata/author.ex @@ -82,6 +82,22 @@ defmodule DecentralisedBookIndex.Metadata.Author do end end + action :get_author_ids, :vector do + argument :author, :struct, allow_nil?: false + + run fn input, _ -> + author = input.arguments.author + + {:ok, ids} = + DecentralisedBookIndex.Metadata.Author + |> Ash.Query.filter(author_alias_registry_id == ^author.author_alias_registry_id) + |> Ash.Query.select([:id]) + |> Ash.read() + + {:ok, Enum.map(ids, & &1.id)} + end + end + read :search do argument :name, :ci_string do constraints allow_empty?: true diff --git a/test/decentralised_book_index/metadata/author_test.exs b/test/decentralised_book_index/metadata/author_test.exs index eb1214c..cb26398 100644 --- a/test/decentralised_book_index/metadata/author_test.exs +++ b/test/decentralised_book_index/metadata/author_test.exs @@ -47,4 +47,27 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do assert alternatives_names = [related_author] end end + + describe "author's ids" do + test "the list has the author's id" do + {:ok, author} = Metadata.create_author("Author", "An description") + + assert {:ok, ids} = Metadata.get_author_ids(author) + assert author.id in ids + end + + test "the list has the aliases' ids" do + {:ok, author} = Metadata.create_author("Author", "An description") + {:ok, alias1} = + Metadata.add_author_to_related_alias_registry("Author2", "An description2", author.id) + {:ok, alias2} = + Metadata.add_author_to_related_alias_registry("Author3", "An description3", author.id) + + assert {:ok, ids} = Metadata.get_author_ids(author) + + assert author.id in ids + assert alias1.id in ids + assert alias2.id in ids + end + end end