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