Update the create action for Author to set AuthorAliasRegistry.

dev
KKlochko 4 months ago
parent 580d1d2d66
commit f2f4990779

@ -6,7 +6,7 @@ defmodule DecentralisedBookIndex.Metadata do
resource DecentralisedBookIndex.Metadata.Book
resource DecentralisedBookIndex.Metadata.Author do
define :create_author, action: :create, args: [:name, :description, :author_alias_registry_id]
define :create_author, action: :create, args: [:name, :description, {:optional, :author_alias_registry_id}]
define :add_author_to_alias_registry, action: :add_author_to_alias_registry, args: [:author_alias_registry_id]
define :list_authors, action: :read
define :get_author_by_id, args: [:id], action: :by_id

@ -20,6 +20,25 @@ defmodule DecentralisedBookIndex.Metadata.Author do
primary? true
accept [:name, :description, :author_alias_registry_id]
change fn changeset, _ ->
registry_id = Ash.Changeset.get_attribute(changeset, :author_alias_registry_id)
if registry_id == nil do
{:ok, registry} = DecentralisedBookIndex.Metadata.create_author_alias_registry()
Ash.Changeset.force_change_attribute(changeset, :author_alias_registry_id, registry.id)
else
changeset
end
end
end
read :by_id do
argument :id, :uuid, allow_nil?: false
get? true
filter expr(id == ^arg(:id))
end
end
end

@ -0,0 +1,18 @@
defmodule DecentralisedBookIndex.Metadata.AuthorTest do
use DecentralisedBookIndex.DataCase, async: true
alias DecentralisedBookIndex.Metadata
describe "authors and registries relationship" do
test "a new author get new registry by default" do
assert {:ok, author} = Metadata.create_author("Author", "An description")
assert author.author_alias_registry_id != nil
end
test "a new author belongs to a registry if specified" do
assert {:ok, registry} = Metadata.create_author_alias_registry()
assert {:ok, author} = Metadata.create_author("Author", "An description", registry.id)
assert author.author_alias_registry_id == registry.id
end
end
end
Loading…
Cancel
Save