Update the sync task for Author to link the record with the server.
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -63,9 +63,12 @@ defmodule DecentralisedBookIndex.Metadata.Author do
|
||||
end
|
||||
|
||||
create :sync do
|
||||
accept [:id, :name, :description, :author_alias_registry_id]
|
||||
accept [:id, :name, :description, :dbi_server_id, :author_alias_registry_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
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
defmodule DecentralisedBookIndex.Sync.AuthorSync do
|
||||
alias DecentralisedBookIndex.Metadata.Author
|
||||
|
||||
def create_update(author_attrs) do
|
||||
def create_update(author_attrs, server_id) do
|
||||
case DecentralisedBookIndex.Metadata.get_author_by_id(author_attrs.id) do
|
||||
{:ok, author} ->
|
||||
author_attrs =
|
||||
@@ -15,6 +15,10 @@ defmodule DecentralisedBookIndex.Sync.AuthorSync do
|
||||
|
||||
:ok
|
||||
{:error, %Ash.Error.Query.NotFound{}} ->
|
||||
author_attrs =
|
||||
author_attrs
|
||||
|> Map.put(:dbi_server_id, server_id)
|
||||
|
||||
DecentralisedBookIndex.Metadata.Author
|
||||
|> Ash.Changeset.for_create(:sync, author_attrs)
|
||||
|> Ash.create!()
|
||||
|
||||
@@ -3,16 +3,19 @@ defmodule DecentralisedBookIndex.SyncTasks.SyncAuthorsTask do
|
||||
alias DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformer
|
||||
alias DecentralisedBookIndex.Sync.AuthorSync
|
||||
|
||||
def sync(endpoint) do
|
||||
url = "#{endpoint}/api/v1/json/author"
|
||||
FetchJsons.get(url, &sync_author_chunk/1)
|
||||
endpoint
|
||||
alias DecentralisedBookIndex.Metadata.DBIServer
|
||||
|
||||
def sync(%DBIServer{} = server) do
|
||||
url = "#{server.url}/api/v1/json/author"
|
||||
FetchJsons.get(url, sync_author_closure(server))
|
||||
|
||||
server
|
||||
end
|
||||
|
||||
def sync_author_chunk(json_chunk) do
|
||||
def sync_author_chunk(json_chunk, server_id) do
|
||||
for json <- json_chunk do
|
||||
with {:ok, attrs} <- AuthorTransformer.from_json(json),
|
||||
:ok <- AuthorSync.create_update(attrs) do
|
||||
:ok <- AuthorSync.create_update(attrs, server_id) do
|
||||
:ok
|
||||
else
|
||||
{:error, reason} ->
|
||||
@@ -22,4 +25,10 @@ defmodule DecentralisedBookIndex.SyncTasks.SyncAuthorsTask do
|
||||
|
||||
[]
|
||||
end
|
||||
|
||||
def sync_author_closure(server) do
|
||||
fn json_chunk ->
|
||||
sync_author_chunk(json_chunk, server.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,8 +5,13 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorSyncTest do
|
||||
alias DecentralisedBookIndex.Metadata.Author
|
||||
alias DecentralisedBookIndex.Metadata
|
||||
|
||||
alias DecentralisedBookIndex.TestEndpoints
|
||||
@test_server_endpoint TestEndpoints.test_api_endpoint()
|
||||
|
||||
describe "sync author transformations" do
|
||||
test "a new author will be created" do
|
||||
server = generate(dbi_server(url: @test_server_endpoint))
|
||||
|
||||
author = %{
|
||||
id: "889a323e-d104-4b5d-b276-dad5a9b1da9d",
|
||||
name: "Author",
|
||||
@@ -14,15 +19,18 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorSyncTest do
|
||||
author_alias_registry_id: nil
|
||||
}
|
||||
|
||||
assert :ok = AuthorSync.create_update(author)
|
||||
assert :ok = AuthorSync.create_update(author, server.id)
|
||||
assert {:ok, saved_author} = Metadata.get_author_by_id(author.id)
|
||||
|
||||
assert author.id == saved_author.id
|
||||
assert author.name == saved_author.name
|
||||
assert nil != saved_author.author_alias_registry_id
|
||||
assert server.id == saved_author.dbi_server_id
|
||||
end
|
||||
|
||||
test "update an existing author" do
|
||||
server = generate(dbi_server(url: @test_server_endpoint))
|
||||
|
||||
{:ok, author} = Metadata.create_author("Author", "An description")
|
||||
|
||||
author_attrs = %{
|
||||
@@ -32,7 +40,7 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorSyncTest do
|
||||
author_alias_registry_id: nil
|
||||
}
|
||||
|
||||
assert :ok = AuthorSync.create_update(author_attrs)
|
||||
assert :ok = AuthorSync.create_update(author_attrs, server.id)
|
||||
assert {:ok, saved_author} = Metadata.get_author_by_id(author.id)
|
||||
|
||||
assert author.id == saved_author.id
|
||||
|
||||
@@ -9,12 +9,12 @@ defmodule DecentralisedBookIndex.SyncTasks.SyncAuthorsTaskTest do
|
||||
|
||||
describe "sync authors tasks" do
|
||||
test "sync authors" do
|
||||
server = generate(dbi_server(url: @test_server_endpoint))
|
||||
|
||||
{:ok, author} = Metadata.create_author("Author", "An description")
|
||||
{:ok, author} = Metadata.create_author("Author2", "An description")
|
||||
|
||||
endpoint = @test_server_endpoint
|
||||
|
||||
assert endpoint = SyncAuthorsTask.sync(endpoint)
|
||||
assert server = SyncAuthorsTask.sync(server)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user