Update to sync also AuthorAliasRegistry.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user