diff --git a/lib/decentralised_book_index/metadata.ex b/lib/decentralised_book_index/metadata.ex index cf519a3..3d66f2f 100644 --- a/lib/decentralised_book_index/metadata.ex +++ b/lib/decentralised_book_index/metadata.ex @@ -10,6 +10,7 @@ defmodule DecentralisedBookIndex.Metadata do define :add_author_to_related_alias_registry, action: :add_author_to_related_alias_registry, args: [:name, :description, :related_author_id] 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 :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 0bc32d8..57366e9 100644 --- a/lib/decentralised_book_index/metadata/author.ex +++ b/lib/decentralised_book_index/metadata/author.ex @@ -4,6 +4,8 @@ defmodule DecentralisedBookIndex.Metadata.Author do domain: DecentralisedBookIndex.Metadata, data_layer: AshPostgres.DataLayer + require Ash.Query + postgres do table "authors" repo DecentralisedBookIndex.Repo @@ -54,6 +56,18 @@ defmodule DecentralisedBookIndex.Metadata.Author do get? true filter expr(id == ^arg(:id)) end + + read :get_alternative_names do + argument :author, :struct, allow_nil?: false + + prepare fn query, context -> + author = query.arguments.author + + DecentralisedBookIndex.Metadata.Author + |> Ash.Query.filter(author_alias_registry_id == ^author.author_alias_registry_id + and id != ^author.id) + end + end end attributes do diff --git a/test/decentralised_book_index/metadata/author_test.exs b/test/decentralised_book_index/metadata/author_test.exs index 8647301..eb1214c 100644 --- a/test/decentralised_book_index/metadata/author_test.exs +++ b/test/decentralised_book_index/metadata/author_test.exs @@ -16,7 +16,7 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do end test "a new author to a registry via a related author record" do - {:ok, related_author} = Metadata.create_author("Author", "An description") + {:ok, related_author} = Metadata.create_author("Author", "An description") assert {:ok, author} = Metadata.add_author_to_related_alias_registry("Author2", "An description2", related_author.id) @@ -29,4 +29,22 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do Metadata.add_author_to_related_alias_registry("Author2", "An description2", nil) end end + + describe "authors alternatives names" do + test "new author has no alternatives names" do + {:ok, author} = Metadata.create_author("Author", "An description") + assert {:ok, alternatives_names} = Metadata.get_author_alternative_names(author) + assert alternatives_names = [] + end + + test "author has related author so they has one alternative name" do + {:ok, related_author} = Metadata.create_author("Author", "An description") + + {:ok, author} = + Metadata.add_author_to_related_alias_registry("Author2", "An description2", related_author.id) + + assert {:ok, alternatives_names} = Metadata.get_author_alternative_names(author) + assert alternatives_names = [related_author] + end + end end