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

dev
KKlochko 4 months ago
parent f2f4990779
commit ef7d1ed057

@ -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

@ -14,5 +14,19 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do
assert {:ok, author} = Metadata.create_author("Author", "An description", registry.id)
assert author.author_alias_registry_id == registry.id
end
test "a new author to a registry via a related author record" do
{: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)
assert related_author.author_alias_registry_id == author.author_alias_registry_id
end
test "a new author to a registry via a empty related author record" do
assert {:error, _} =
Metadata.add_author_to_related_alias_registry("Author2", "An description2", nil)
end
end
end

Loading…
Cancel
Save