diff --git a/lib/decentralised_book_index/sync/sync_tasks/sync_authors_task.ex b/lib/decentralised_book_index/sync/sync_tasks/sync_authors_task.ex new file mode 100644 index 0000000..5375611 --- /dev/null +++ b/lib/decentralised_book_index/sync/sync_tasks/sync_authors_task.ex @@ -0,0 +1,25 @@ +defmodule DecentralisedBookIndex.SyncTasks.SyncAuthorsTask do + alias DecentralisedBookIndex.Sync.ApiClients.FetchJsons + 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 + end + + def sync_author_chunk(json_chunk) do + for json <- json_chunk do + with {:ok, attrs} <- AuthorTransformer.from_json(json), + :ok <- AuthorSync.create_update(attrs) do + :ok + else + {:error, reason} -> + Logger.error("Pipeline error: #{inspect(reason)}") + end + end + + [] + end +end diff --git a/test/decentralised_book_index/sync/sync_tasks/sync_authors_task_test.exs b/test/decentralised_book_index/sync/sync_tasks/sync_authors_task_test.exs new file mode 100644 index 0000000..b1b959a --- /dev/null +++ b/test/decentralised_book_index/sync/sync_tasks/sync_authors_task_test.exs @@ -0,0 +1,20 @@ +defmodule DecentralisedBookIndex.SyncTasks.SyncAuthorsTaskTest do + use DecentralisedBookIndex.DataCase + + alias DecentralisedBookIndex.SyncTasks.SyncAuthorsTask + alias DecentralisedBookIndex.Metadata + + alias DecentralisedBookIndex.TestEndpoints + @test_server_endpoint TestEndpoints.test_api_endpoint() + + describe "sync authors tasks" do + test "sync authors" do + {: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) + end + end +end