From ef7d1ed0577b1d2296833cc0fe03fe0cb2b987a0 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Fri, 7 Mar 2025 14:12:16 +0200 Subject: [PATCH] Add an action to add author to relative author's alias registry. --- lib/decentralised_book_index/metadata.ex | 2 +- .../metadata/author.ex | 26 ++++++++++++++----- .../metadata/author_test.exs | 14 ++++++++++ 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/lib/decentralised_book_index/metadata.ex b/lib/decentralised_book_index/metadata.ex index c5663b1..cf519a3 100644 --- a/lib/decentralised_book_index/metadata.ex +++ b/lib/decentralised_book_index/metadata.ex @@ -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 diff --git a/lib/decentralised_book_index/metadata/author.ex b/lib/decentralised_book_index/metadata/author.ex index a880cae..0bc32d8 100644 --- a/lib/decentralised_book_index/metadata/author.ex +++ b/lib/decentralised_book_index/metadata/author.ex @@ -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 diff --git a/test/decentralised_book_index/metadata/author_test.exs b/test/decentralised_book_index/metadata/author_test.exs index 413a4ca..8647301 100644 --- a/test/decentralised_book_index/metadata/author_test.exs +++ b/test/decentralised_book_index/metadata/author_test.exs @@ -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