Add AuthorSync to sync author's attributes.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
1042ac4830
commit
e08372102f
@ -0,0 +1,25 @@
|
||||
defmodule DecentralisedBookIndex.Sync.AuthorSync do
|
||||
alias DecentralisedBookIndex.Metadata.Author
|
||||
|
||||
def create_update(author_attrs) do
|
||||
case DecentralisedBookIndex.Metadata.get_author_by_id(author_attrs.id) do
|
||||
{:ok, author} ->
|
||||
author_attrs =
|
||||
author_attrs
|
||||
|> Map.delete(:id)
|
||||
|> Map.delete(:author_alias_registry_id)
|
||||
|
||||
author
|
||||
|> Ash.Changeset.for_update(:update, author_attrs)
|
||||
|> Ash.update!()
|
||||
|
||||
:ok
|
||||
{:error, %Ash.Error.Query.NotFound{}} ->
|
||||
DecentralisedBookIndex.Metadata.Author
|
||||
|> Ash.Changeset.for_create(:sync, author_attrs)
|
||||
|> Ash.create!()
|
||||
|
||||
:ok
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,43 @@
|
||||
defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorSyncTest do
|
||||
use DecentralisedBookIndex.DataCase, async: true
|
||||
|
||||
alias DecentralisedBookIndex.Sync.AuthorSync
|
||||
alias DecentralisedBookIndex.Metadata.Author
|
||||
alias DecentralisedBookIndex.Metadata
|
||||
|
||||
describe "sync author transformations" do
|
||||
test "a new author will be created" do
|
||||
author = %{
|
||||
id: "889a323e-d104-4b5d-b276-dad5a9b1da9d",
|
||||
name: "Author",
|
||||
description: "Something",
|
||||
author_alias_registry_id: nil
|
||||
}
|
||||
|
||||
assert :ok = AuthorSync.create_update(author)
|
||||
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
|
||||
end
|
||||
|
||||
test "update an existing author" do
|
||||
{:ok, author} = Metadata.create_author("Author", "An description")
|
||||
|
||||
author_attrs = %{
|
||||
id: author.id,
|
||||
name: "Author2",
|
||||
description: "Something2",
|
||||
author_alias_registry_id: nil
|
||||
}
|
||||
|
||||
assert :ok = AuthorSync.create_update(author_attrs)
|
||||
assert {:ok, saved_author} = Metadata.get_author_by_id(author.id)
|
||||
|
||||
assert author.id == saved_author.id
|
||||
assert author_attrs.name == saved_author.name
|
||||
assert author_attrs.description == saved_author.description
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in new issue