Update to refactor Author and sync modules.
This commit is contained in:
@@ -62,8 +62,18 @@ defmodule DecentralisedBookIndex.Metadata.Author do
|
||||
end
|
||||
end
|
||||
|
||||
create :sync do
|
||||
accept [:id, :name, :description, :avatar_url, :dbi_server_id, :author_alias_registry_id]
|
||||
create :sync_create do
|
||||
accept [
|
||||
:id,
|
||||
:name,
|
||||
:description,
|
||||
:avatar_url,
|
||||
:dbi_server_id,
|
||||
:author_alias_registry_id,
|
||||
:inserted_at,
|
||||
:updated_at,
|
||||
:dbi_server_id
|
||||
]
|
||||
|
||||
change fn changeset, _ ->
|
||||
server_id = Ash.Changeset.get_attribute(changeset, :dbi_server)
|
||||
@@ -128,6 +138,37 @@ defmodule DecentralisedBookIndex.Metadata.Author do
|
||||
pagination offset?: true, default_limit: 10
|
||||
end
|
||||
|
||||
update :sync do
|
||||
require_atomic? false
|
||||
|
||||
accept [
|
||||
:id,
|
||||
:name,
|
||||
:description,
|
||||
:avatar_url,
|
||||
:dbi_server_id,
|
||||
:author_alias_registry_id,
|
||||
:inserted_at,
|
||||
:updated_at,
|
||||
:dbi_server_id
|
||||
]
|
||||
|
||||
change fn changeset, _ ->
|
||||
server_id = Ash.Changeset.get_attribute(changeset, :dbi_server)
|
||||
Ash.Changeset.force_change_attribute(changeset, :author_alias_registry_id, server_id)
|
||||
|
||||
registry_id = Ash.Changeset.get_attribute(changeset, :author_alias_registry_id)
|
||||
|
||||
if registry_id == nil do
|
||||
{:ok, registry} = DecentralisedBookIndex.Metadata.create_author_alias_registry()
|
||||
|
||||
Ash.Changeset.force_change_attribute(changeset, :author_alias_registry_id, registry.id)
|
||||
else
|
||||
changeset
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
update :update do
|
||||
primary? true
|
||||
accept [:name, :description, :avatar_url]
|
||||
@@ -156,7 +197,10 @@ defmodule DecentralisedBookIndex.Metadata.Author do
|
||||
public? true
|
||||
end
|
||||
|
||||
timestamps()
|
||||
timestamps do
|
||||
writable? true
|
||||
public? true
|
||||
end
|
||||
end
|
||||
|
||||
relationships do
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformer do
|
||||
|
||||
alias DecentralisedBookIndex.Metadata.Author
|
||||
|
||||
def from_json(json_body) do
|
||||
author =
|
||||
attrs =
|
||||
if Map.has_key?(json_body, "data") do
|
||||
%{
|
||||
id: get_in(json_body, ["data", "id"]),
|
||||
@@ -20,6 +17,6 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformer do
|
||||
}
|
||||
end
|
||||
|
||||
{:ok, author}
|
||||
{:ok, attrs}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,26 +1,27 @@
|
||||
defmodule DecentralisedBookIndex.Sync.AuthorSync do
|
||||
alias DecentralisedBookIndex.Metadata
|
||||
alias DecentralisedBookIndex.Metadata.Author
|
||||
|
||||
def create_update(author_attrs, server_id) do
|
||||
case DecentralisedBookIndex.Metadata.get_author_by_id(author_attrs.id) do
|
||||
def create_update(attrs, server_id) do
|
||||
case Metadata.get_author_by_id(attrs.id) do
|
||||
{:ok, author} ->
|
||||
author_attrs =
|
||||
author_attrs
|
||||
attrs =
|
||||
attrs
|
||||
|> Map.delete(:id)
|
||||
|> Map.delete(:author_alias_registry_id)
|
||||
|
||||
author
|
||||
|> Ash.Changeset.for_update(:update, author_attrs)
|
||||
|> Ash.Changeset.for_update(:sync, attrs)
|
||||
|> Ash.update!()
|
||||
|
||||
:ok
|
||||
{:error, %Ash.Error.Query.NotFound{}} ->
|
||||
author_attrs =
|
||||
author_attrs
|
||||
attrs =
|
||||
attrs
|
||||
|> Map.put(:dbi_server_id, server_id)
|
||||
|
||||
DecentralisedBookIndex.Metadata.Author
|
||||
|> Ash.Changeset.for_create(:sync, author_attrs)
|
||||
Author
|
||||
|> Ash.Changeset.for_create(:sync_create, attrs)
|
||||
|> Ash.create!()
|
||||
|
||||
:ok
|
||||
|
||||
@@ -2,7 +2,6 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorSyncTest do
|
||||
use DecentralisedBookIndex.DataCase, async: true
|
||||
|
||||
alias DecentralisedBookIndex.Sync.AuthorSync
|
||||
alias DecentralisedBookIndex.Metadata.Author
|
||||
alias DecentralisedBookIndex.Metadata
|
||||
|
||||
alias DecentralisedBookIndex.TestEndpoints
|
||||
|
||||
Reference in New Issue
Block a user