Add the sync task for Book.
This commit is contained in:
@@ -54,6 +54,42 @@ defmodule DecentralisedBookIndex.Metadata.Book do
|
||||
change manage_relationship(:author_roles, type: :direct_control, order_is_key: :order)
|
||||
end
|
||||
|
||||
create :sync_create do
|
||||
accept [
|
||||
:id,
|
||||
:title,
|
||||
:description,
|
||||
:format,
|
||||
:language,
|
||||
:page_count,
|
||||
:published,
|
||||
:publisher_id,
|
||||
:cover_image_url,
|
||||
:book_editions_registry_id,
|
||||
:inserted_at,
|
||||
:updated_at,
|
||||
:dbi_server_id
|
||||
]
|
||||
|
||||
argument :bids, {:array, :map}
|
||||
argument :author_roles, {:array, :map}
|
||||
|
||||
change fn changeset, _ ->
|
||||
registry_id = Ash.Changeset.get_attribute(changeset, :book_editions_registry_id)
|
||||
|
||||
if registry_id == nil do
|
||||
{:ok, registry} = DecentralisedBookIndex.Metadata.create_book_editions_registry()
|
||||
|
||||
Ash.Changeset.force_change_attribute(changeset, :book_editions_registry_id, registry.id)
|
||||
else
|
||||
changeset
|
||||
end
|
||||
end
|
||||
|
||||
change manage_relationship(:bids, type: :direct_control, order_is_key: :order)
|
||||
change manage_relationship(:author_roles, type: :direct_control, order_is_key: :order)
|
||||
end
|
||||
|
||||
create :add_book_to_related_editions_registry do
|
||||
accept [
|
||||
:title,
|
||||
@@ -179,13 +215,51 @@ defmodule DecentralisedBookIndex.Metadata.Book do
|
||||
change manage_relationship(:author_roles, type: :direct_control, order_is_key: :order)
|
||||
end
|
||||
|
||||
update :sync do
|
||||
require_atomic? false
|
||||
|
||||
accept [
|
||||
:id,
|
||||
:title,
|
||||
:description,
|
||||
:format,
|
||||
:language,
|
||||
:page_count,
|
||||
:published,
|
||||
:publisher_id,
|
||||
:cover_image_url,
|
||||
:book_editions_registry_id,
|
||||
:inserted_at,
|
||||
:updated_at,
|
||||
:dbi_server_id
|
||||
]
|
||||
|
||||
argument :bids, {:array, :map}
|
||||
argument :author_roles, {:array, :map}
|
||||
|
||||
change fn changeset, _ ->
|
||||
registry_id = Ash.Changeset.get_attribute(changeset, :book_editions_registry_id)
|
||||
|
||||
if registry_id == nil do
|
||||
{:ok, registry} = DecentralisedBookIndex.Metadata.create_book_editions_registry()
|
||||
|
||||
Ash.Changeset.force_change_attribute(changeset, :book_editions_registry_id, registry.id)
|
||||
else
|
||||
changeset
|
||||
end
|
||||
end
|
||||
|
||||
change manage_relationship(:bids, type: :direct_control, order_is_key: :order)
|
||||
change manage_relationship(:author_roles, type: :direct_control, order_is_key: :order)
|
||||
end
|
||||
|
||||
update :assign_cover_image do
|
||||
accept [:cover_image_url]
|
||||
end
|
||||
end
|
||||
|
||||
attributes do
|
||||
uuid_primary_key :id
|
||||
uuid_primary_key :id, writable?: true
|
||||
|
||||
attribute :title, :string do
|
||||
allow_nil? false
|
||||
@@ -222,7 +296,10 @@ defmodule DecentralisedBookIndex.Metadata.Book do
|
||||
public? true
|
||||
end
|
||||
|
||||
timestamps()
|
||||
timestamps() do
|
||||
writable? true
|
||||
public? true
|
||||
end
|
||||
end
|
||||
|
||||
relationships do
|
||||
@@ -230,7 +307,9 @@ defmodule DecentralisedBookIndex.Metadata.Book do
|
||||
|
||||
belongs_to :book_editions_registry, Metadata.BookEditionsRegistry
|
||||
|
||||
belongs_to :publisher, Metadata.Publisher
|
||||
belongs_to :publisher, Metadata.Publisher do
|
||||
public? true
|
||||
end
|
||||
|
||||
has_many :author_roles, Metadata.AuthorRole do
|
||||
sort order: :asc
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
defmodule DecentralisedBookIndex.Sync.DataTransformers.BookTransformer do
|
||||
def from_json(json_body) do
|
||||
json_body =
|
||||
if Map.has_key?(json_body, "data") do
|
||||
json_body["data"]
|
||||
else
|
||||
json_body
|
||||
end
|
||||
|
||||
attrs =
|
||||
%{
|
||||
id: get_in(json_body, ["id"]),
|
||||
title: get_in(json_body, ["attributes", "title"]),
|
||||
description: get_in(json_body, ["attributes", "description"]),
|
||||
cover_image_url: get_in(json_body, ["attributes", "cover_image_url"]),
|
||||
format: get_in(json_body, ["attributes", "format"]),
|
||||
language: get_in(json_body, ["attributes", "language"]),
|
||||
published: get_in(json_body, ["attributes", "published"]),
|
||||
page_count: get_in(json_body, ["attributes", "page_count"]),
|
||||
publisher_id: get_in(json_body, ["attributes", "publisher_id"]),
|
||||
inserted_at: get_in(json_body, ["attributes", "inserted_at"]),
|
||||
updated_at: get_in(json_body, ["attributes", "updated_at"]),
|
||||
# relationship
|
||||
publisher_id: get_in(json_body, ["attributes", "publisher_id"])
|
||||
}
|
||||
|
||||
{:ok, attrs}
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,31 @@
|
||||
defmodule DecentralisedBookIndex.Sync.BookSync do
|
||||
alias DecentralisedBookIndex.Metadata
|
||||
alias DecentralisedBookIndex.Metadata.Book
|
||||
|
||||
def create_update(attrs, server_id) do
|
||||
case Metadata.get_book_by_id(attrs.id) do
|
||||
{:ok, book} ->
|
||||
attrs =
|
||||
attrs
|
||||
|> Map.delete(:id)
|
||||
|> Map.delete(:book_editions_registry)
|
||||
|> Map.put(:dbi_server_id, server_id)
|
||||
|
||||
book
|
||||
|> Ash.Changeset.for_update(:sync, attrs)
|
||||
|> Ash.update!()
|
||||
|
||||
:ok
|
||||
{:error, %Ash.Error.Query.NotFound{}} ->
|
||||
attrs =
|
||||
attrs
|
||||
|> Map.put(:dbi_server_id, server_id)
|
||||
|
||||
Book
|
||||
|> Ash.Changeset.for_create(:sync_create, attrs)
|
||||
|> Ash.create!()
|
||||
|
||||
:ok
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,36 @@
|
||||
defmodule DecentralisedBookIndex.SyncTasks.SyncBooksTask do
|
||||
alias DecentralisedBookIndex.Sync.ApiClients.FetchJsons
|
||||
alias DecentralisedBookIndex.Sync.DataTransformers.BookTransformer
|
||||
alias DecentralisedBookIndex.Sync.BookSync
|
||||
|
||||
alias DecentralisedBookIndex.Metadata.DBIServer
|
||||
|
||||
require Logger
|
||||
|
||||
def sync(%DBIServer{} = server) do
|
||||
url = "#{server.url}/api/v1/json/books"
|
||||
FetchJsons.get(url, sync_author_closure(server))
|
||||
|
||||
server
|
||||
end
|
||||
|
||||
def sync_author_chunk(json_chunk, server_id) do
|
||||
for json <- json_chunk do
|
||||
with {:ok, attrs} <- BookTransformer.from_json(json),
|
||||
:ok <- BookSync.create_update(attrs, server_id) do
|
||||
:ok
|
||||
else
|
||||
{:error, reason} ->
|
||||
Logger.error("Pipeline error: #{inspect(reason)}")
|
||||
end
|
||||
end
|
||||
|
||||
[]
|
||||
end
|
||||
|
||||
def sync_author_closure(server) do
|
||||
fn json_chunk ->
|
||||
sync_author_chunk(json_chunk, server.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user