This commit is contained in:
@@ -318,7 +318,9 @@ defmodule DecentralisedBookIndex.Metadata.Book do
|
||||
relationships do
|
||||
belongs_to :dbi_server, Metadata.DBIServer
|
||||
|
||||
belongs_to :book_editions_registry, Metadata.BookEditionsRegistry
|
||||
belongs_to :book_editions_registry, Metadata.BookEditionsRegistry do
|
||||
public? true
|
||||
end
|
||||
|
||||
belongs_to :publisher, Metadata.Publisher do
|
||||
public? true
|
||||
|
||||
@@ -14,6 +14,14 @@ defmodule DecentralisedBookIndex.Metadata.BookEditionsRegistry do
|
||||
actions do
|
||||
defaults [:read, :create, :update, :destroy]
|
||||
|
||||
create :sync_create do
|
||||
accept [:id, :dbi_server_id]
|
||||
end
|
||||
|
||||
update :sync do
|
||||
accept [:id, :dbi_server_id]
|
||||
end
|
||||
|
||||
read :by_id do
|
||||
argument :id, :uuid, allow_nil?: false
|
||||
get? true
|
||||
@@ -22,7 +30,7 @@ defmodule DecentralisedBookIndex.Metadata.BookEditionsRegistry do
|
||||
end
|
||||
|
||||
attributes do
|
||||
uuid_primary_key :id
|
||||
uuid_primary_key :id, writable?: true
|
||||
|
||||
timestamps()
|
||||
end
|
||||
@@ -30,6 +38,6 @@ defmodule DecentralisedBookIndex.Metadata.BookEditionsRegistry do
|
||||
relationships do
|
||||
belongs_to :dbi_server, Metadata.DBIServer
|
||||
|
||||
has_many :authors, DecentralisedBookIndex.Metadata.Book
|
||||
has_many :books, DecentralisedBookIndex.Metadata.Book
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
defmodule DecentralisedBookIndex.Sync.BookEditionsRegistrySync do
|
||||
alias DecentralisedBookIndex.Metadata
|
||||
alias DecentralisedBookIndex.Metadata.BookEditionsRegistry
|
||||
|
||||
def create_update(attrs, server_id) do
|
||||
case Metadata.get_book_editions_registry_by_id(attrs.id) do
|
||||
{:ok, editions_registry} ->
|
||||
attrs =
|
||||
attrs
|
||||
|> Map.delete(:id)
|
||||
|> Map.put(:dbi_server_id, server_id)
|
||||
|
||||
editions_registry
|
||||
|> Ash.Changeset.for_update(:sync, attrs)
|
||||
|> Ash.update!()
|
||||
|
||||
:ok
|
||||
|
||||
{:error, %Ash.Error.Query.NotFound{}} ->
|
||||
attrs =
|
||||
attrs
|
||||
|> Map.put(:dbi_server_id, server_id)
|
||||
|
||||
BookEditionsRegistry
|
||||
|> Ash.Changeset.for_create(:sync_create, attrs)
|
||||
|> Ash.create!()
|
||||
|
||||
:ok
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,14 +1,17 @@
|
||||
defmodule DecentralisedBookIndex.Sync.BookSync do
|
||||
alias DecentralisedBookIndex.Metadata
|
||||
alias DecentralisedBookIndex.Metadata.Book
|
||||
alias DecentralisedBookIndex.Sync.BookEditionsRegistrySync
|
||||
|
||||
def create_update(attrs, server_id) do
|
||||
registry_info = sync_registry(attrs, server_id)
|
||||
|
||||
case Metadata.get_book_by_id(attrs.id) do
|
||||
{:ok, book} ->
|
||||
attrs =
|
||||
attrs
|
||||
|> Map.delete(:id)
|
||||
|> Map.delete(:book_editions_registry)
|
||||
|> update_registry(registry_info)
|
||||
|> Map.put(:dbi_server_id, server_id)
|
||||
|
||||
book
|
||||
@@ -19,6 +22,7 @@ defmodule DecentralisedBookIndex.Sync.BookSync do
|
||||
{:error, %Ash.Error.Query.NotFound{}} ->
|
||||
attrs =
|
||||
attrs
|
||||
|> update_registry(registry_info)
|
||||
|> Map.put(:dbi_server_id, server_id)
|
||||
|
||||
Book
|
||||
@@ -28,4 +32,29 @@ defmodule DecentralisedBookIndex.Sync.BookSync do
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
defp sync_registry(attrs, server_id) do
|
||||
id = Map.get(attrs, :book_editions_registry_id, nil)
|
||||
|
||||
if id != nil do
|
||||
%{id: id}
|
||||
|> BookEditionsRegistrySync.create_update(server_id)
|
||||
|
||||
{:ok, id}
|
||||
else
|
||||
{:error, :no_book_editions_registry_id}
|
||||
end
|
||||
end
|
||||
|
||||
defp update_registry(attrs, registry_info) do
|
||||
case registry_info do
|
||||
{:ok, id} ->
|
||||
attrs
|
||||
|> Map.replace(:book_editions_registry_id, id)
|
||||
|
||||
{:error, _} ->
|
||||
attrs
|
||||
|> Map.delete(:book_editions_registry_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user