You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.0 KiB
81 lines
2.0 KiB
defmodule DecentralisedBookIndex.Metadata.Author do
|
|
use Ash.Resource,
|
|
otp_app: :decentralised_book_index,
|
|
domain: DecentralisedBookIndex.Metadata,
|
|
data_layer: AshPostgres.DataLayer
|
|
|
|
postgres do
|
|
table "authors"
|
|
repo DecentralisedBookIndex.Repo
|
|
end
|
|
|
|
actions do
|
|
defaults [:read, :update, :destroy]
|
|
|
|
create :create do
|
|
primary? true
|
|
accept [: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
|
|
|
|
create :add_author_to_related_alias_registry do
|
|
accept [:name, :description]
|
|
|
|
argument :related_author_id, :uuid do
|
|
allow_nil? false
|
|
end
|
|
|
|
change fn changeset, context ->
|
|
related_author_id = changeset.arguments.related_author_id
|
|
|
|
if related_author_id == nil do
|
|
Ash.Changeset.add_error(changeset, :related_author_id, "Related author is empty")
|
|
else
|
|
{:ok, related_author} = DecentralisedBookIndex.Metadata.get_author_by_id(related_author_id)
|
|
|
|
Ash.Changeset.force_change_attribute(changeset, :author_alias_registry_id, related_author.author_alias_registry_id)
|
|
end
|
|
end
|
|
end
|
|
|
|
read :by_id do
|
|
argument :id, :uuid, allow_nil?: false
|
|
get? true
|
|
filter expr(id == ^arg(:id))
|
|
end
|
|
end
|
|
|
|
attributes do
|
|
uuid_primary_key :id
|
|
|
|
attribute :name, :string do
|
|
allow_nil? false
|
|
public? true
|
|
end
|
|
|
|
attribute :description, :string do
|
|
allow_nil? false
|
|
public? true
|
|
end
|
|
|
|
timestamps()
|
|
end
|
|
|
|
relationships do
|
|
belongs_to :author_alias_registry, DecentralisedBookIndex.Metadata.AuthorAliasRegistry do
|
|
attribute_writable? true
|
|
end
|
|
end
|
|
end
|