From 1a326cc8aab317165a0dd347175a6cf4c9ab912e Mon Sep 17 00:00:00 2001 From: KKlochko Date: Wed, 9 Apr 2025 21:53:03 +0300 Subject: [PATCH] Update to sync also AuthorAliasRegistry. --- .../metadata/author.ex | 1 + .../metadata/author_alias_registry.ex | 18 +++++++++- .../data_transformers/author_transformer.ex | 3 +- .../sync/sync/author_alias_registry_sync.ex | 31 +++++++++++++++++ .../sync/sync/author_sync.ex | 34 ++++++++++++++++++- 5 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 lib/decentralised_book_index/sync/sync/author_alias_registry_sync.ex diff --git a/lib/decentralised_book_index/metadata/author.ex b/lib/decentralised_book_index/metadata/author.ex index dbe0272..b74ad28 100644 --- a/lib/decentralised_book_index/metadata/author.ex +++ b/lib/decentralised_book_index/metadata/author.ex @@ -208,6 +208,7 @@ defmodule DecentralisedBookIndex.Metadata.Author do belongs_to :author_alias_registry, Metadata.AuthorAliasRegistry do attribute_writable? true + public? true end has_many :author_role, Metadata.AuthorRole diff --git a/lib/decentralised_book_index/metadata/author_alias_registry.ex b/lib/decentralised_book_index/metadata/author_alias_registry.ex index cd10dbe..4e254b6 100644 --- a/lib/decentralised_book_index/metadata/author_alias_registry.ex +++ b/lib/decentralised_book_index/metadata/author_alias_registry.ex @@ -14,6 +14,22 @@ defmodule DecentralisedBookIndex.Metadata.AuthorAliasRegistry do actions do defaults [:read, :create, :update, :destroy] + create :sync_create do + accept [:id] + + argument :dbi_server_id, :uuid do + allow_nil? false + end + end + + update :sync do + accept [:id] + + argument :dbi_server_id, :uuid do + allow_nil? false + end + end + read :by_id do argument :id, :uuid, allow_nil?: false get? true @@ -22,7 +38,7 @@ defmodule DecentralisedBookIndex.Metadata.AuthorAliasRegistry do end attributes do - uuid_primary_key :id + uuid_primary_key :id, writable?: true timestamps() end diff --git a/lib/decentralised_book_index/sync/data_transformers/author_transformer.ex b/lib/decentralised_book_index/sync/data_transformers/author_transformer.ex index 59838e2..d574adb 100644 --- a/lib/decentralised_book_index/sync/data_transformers/author_transformer.ex +++ b/lib/decentralised_book_index/sync/data_transformers/author_transformer.ex @@ -12,7 +12,8 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformer do id: get_in(json_body, ["id"]), name: get_in(json_body, ["attributes", "name"]), description: get_in(json_body, ["attributes", "description"]), - avatar_url: get_in(json_body, ["attributes", "avatar_url"]) + avatar_url: get_in(json_body, ["attributes", "avatar_url"]), + author_alias_registry_id: get_in(json_body, ["attributes", "author_alias_registry_id"]), } {:ok, attrs} diff --git a/lib/decentralised_book_index/sync/sync/author_alias_registry_sync.ex b/lib/decentralised_book_index/sync/sync/author_alias_registry_sync.ex new file mode 100644 index 0000000..8fec777 --- /dev/null +++ b/lib/decentralised_book_index/sync/sync/author_alias_registry_sync.ex @@ -0,0 +1,31 @@ +defmodule DecentralisedBookIndex.Sync.AuthorAliasRegistrySync do + alias DecentralisedBookIndex.Metadata + alias DecentralisedBookIndex.Metadata.AuthorAliasRegistry + + def create_update(attrs, server_id) do + case Metadata.get_author_alias_registry_by_id(attrs.id) do + {:ok, author} -> + attrs = + attrs + |> Map.delete(:id) + |> Map.put(:dbi_server_id, server_id) + + author + |> Ash.Changeset.for_update(:sync, attrs) + |> Ash.update!() + + :ok + + {:error, %Ash.Error.Query.NotFound{}} -> + attrs = + attrs + |> Map.put(:dbi_server_id, server_id) + + AuthorAliasRegistry + |> Ash.Changeset.for_create(:sync_create, attrs) + |> Ash.create!() + + :ok + end + end +end diff --git a/lib/decentralised_book_index/sync/sync/author_sync.ex b/lib/decentralised_book_index/sync/sync/author_sync.ex index ab5dd19..144aaf3 100644 --- a/lib/decentralised_book_index/sync/sync/author_sync.ex +++ b/lib/decentralised_book_index/sync/sync/author_sync.ex @@ -1,14 +1,17 @@ defmodule DecentralisedBookIndex.Sync.AuthorSync do alias DecentralisedBookIndex.Metadata alias DecentralisedBookIndex.Metadata.Author + alias DecentralisedBookIndex.Sync.AuthorAliasRegistrySync def create_update(attrs, server_id) do + registry_info = sync_registry(attrs, server_id) + case Metadata.get_author_by_id(attrs.id) do {:ok, author} -> attrs = attrs |> Map.delete(:id) - |> Map.delete(:author_alias_registry_id) + |> update_registry(registry_info) |> Map.put(:dbi_server_id, server_id) author @@ -16,10 +19,12 @@ defmodule DecentralisedBookIndex.Sync.AuthorSync do |> Ash.update!() :ok + {:error, %Ash.Error.Query.NotFound{}} -> attrs = attrs |> Map.put(:dbi_server_id, server_id) + |> update_registry(registry_info) Author |> Ash.Changeset.for_create(:sync_create, attrs) @@ -28,4 +33,31 @@ defmodule DecentralisedBookIndex.Sync.AuthorSync do :ok end end + + defp sync_registry(attrs, server_id) do + id = Map.get(attrs, :author_alias_registry_id, nil) + + if id != nil do + Map.get(%{c: 1, b: 3}, :a, nil) + + %{id: id} + |> AuthorAliasRegistrySync.create_update(server_id) + + {:ok, id} + else + {:error, :no_author_alias_registry_id} + end + end + + defp update_registry(attrs, registry_info) do + case registry_info do + {:ok, id} -> + attrs + |> Map.replace(:author_alias_registry_id, id) + + {:error, _} -> + attrs + |> Map.delete(:author_alias_registry_id) + end + end end