Update the create action for Author to set AuthorAliasRegistry.

This commit is contained in:
2025-03-07 13:47:15 +02:00
parent 580d1d2d66
commit f2f4990779
3 changed files with 38 additions and 1 deletions
+1 -1
View File
@@ -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