Add the action to get author's ids.

dev
KKlochko 4 months ago
parent c546c61268
commit d6eef962de

@ -38,6 +38,7 @@ defmodule DecentralisedBookIndex.Metadata do
define :list_authors, action: :read define :list_authors, action: :read
define :get_author_by_id, args: [:id], action: :by_id 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 :get_author_alternative_names, args: [:author], action: :get_alternative_names
define :search_author, action: :search, args: [:name] define :search_author, action: :search, args: [:name]
define :update_author, action: :update define :update_author, action: :update

@ -82,6 +82,22 @@ defmodule DecentralisedBookIndex.Metadata.Author do
end end
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 read :search do
argument :name, :ci_string do argument :name, :ci_string do
constraints allow_empty?: true constraints allow_empty?: true

@ -47,4 +47,27 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do
assert alternatives_names = [related_author] assert alternatives_names = [related_author]
end end
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 end

Loading…
Cancel
Save