Add an action to add author to relative author's alias registry.

This commit is contained in:
2025-03-07 14:15:27 +02:00
parent f2f4990779
commit ef7d1ed057
3 changed files with 35 additions and 7 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ defmodule DecentralisedBookIndex.Metadata do
resource DecentralisedBookIndex.Metadata.Author do
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 :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 :update_author, action: :update
@@ -12,10 +12,6 @@ defmodule DecentralisedBookIndex.Metadata.Author do
actions do
defaults [:read, :update, :destroy]
update :add_author_to_alias_registry do
accept [:author_alias_registry_id]
end
create :create do
primary? true
accept [:name, :description, :author_alias_registry_id]
@@ -33,13 +29,31 @@ defmodule DecentralisedBookIndex.Metadata.Author do
end
end
create :add_author_to_related_alias_registry do
accept [:name, :description]
argument :related_author_id, :uuid do
allow_nil? false
end
change fn changeset, context ->
related_author_id = changeset.arguments.related_author_id
if related_author_id == nil do
Ash.Changeset.add_error(changeset, :related_author_id, "Related author is empty")
else
{:ok, related_author} = DecentralisedBookIndex.Metadata.get_author_by_id(related_author_id)
Ash.Changeset.force_change_attribute(changeset, :author_alias_registry_id, related_author.author_alias_registry_id)
end
end
end
read :by_id do
argument :id, :uuid, allow_nil?: false
get? true
filter expr(id == ^arg(:id))
end
end
end
attributes do