This commit is contained in:
@@ -18,7 +18,7 @@ defmodule DecentralisedBookIndex.Metadata.Author do
|
||||
end
|
||||
|
||||
actions do
|
||||
defaults [:read, :update, :destroy]
|
||||
defaults [:read, :destroy]
|
||||
|
||||
create :create do
|
||||
primary? true
|
||||
@@ -62,6 +62,22 @@ defmodule DecentralisedBookIndex.Metadata.Author do
|
||||
end
|
||||
end
|
||||
|
||||
create :sync do
|
||||
accept [:id, :name, :description, :author_alias_registry_id]
|
||||
|
||||
change fn changeset, _ ->
|
||||
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
|
||||
|
||||
read :by_id do
|
||||
argument :id, :uuid, allow_nil?: false
|
||||
get? true
|
||||
@@ -109,13 +125,18 @@ defmodule DecentralisedBookIndex.Metadata.Author do
|
||||
pagination offset?: true, default_limit: 10
|
||||
end
|
||||
|
||||
update :update do
|
||||
primary? true
|
||||
accept [:name, :description, :avatar_url]
|
||||
end
|
||||
|
||||
update :assign_avatar_image do
|
||||
accept [:avatar_url]
|
||||
end
|
||||
end
|
||||
|
||||
attributes do
|
||||
uuid_primary_key :id
|
||||
uuid_primary_key :id, writable?: true
|
||||
|
||||
attribute :name, :string do
|
||||
allow_nil? false
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user