Update Author to add the search action and refactor the format.

dev
KKlochko 4 months ago
parent e3da0dc129
commit 040d2771f0

@ -2,11 +2,19 @@ defmodule DecentralisedBookIndex.Metadata do
use Ash.Domain,
otp_app: :decentralised_book_index
alias DecentralisedBookIndex.Metadata
resources do
resource DecentralisedBookIndex.Metadata.Book do
define :create_book,
action: :create,
args: [:title, :isbn, :description, :author_roles, {:optional, :book_editions_registry_id}]
args: [
:title,
:isbn,
:description,
:author_roles,
{:optional, :book_editions_registry_id}
]
define :add_book_to_related_editions_registry,
action: :add_book_to_related_editions_registry,
@ -31,6 +39,7 @@ defmodule DecentralisedBookIndex.Metadata do
define :list_authors, action: :read
define :get_author_by_id, args: [:id], action: :by_id
define :get_author_alternative_names, args: [:author], action: :get_alternative_names
define :search_author, action: :search, args: [:name]
define :update_author, action: :update
define :destroy_author, action: :destroy
end

@ -45,9 +45,14 @@ defmodule DecentralisedBookIndex.Metadata.Author do
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)
{: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)
Ash.Changeset.force_change_attribute(
changeset,
:author_alias_registry_id,
related_author.author_alias_registry_id
)
end
end
end
@ -65,10 +70,23 @@ defmodule DecentralisedBookIndex.Metadata.Author do
author = query.arguments.author
DecentralisedBookIndex.Metadata.Author
|> Ash.Query.filter(author_alias_registry_id == ^author.author_alias_registry_id
and id != ^author.id)
|> Ash.Query.filter(
author_alias_registry_id == ^author.author_alias_registry_id and
id != ^author.id
)
end
end
read :search do
argument :name, :ci_string do
constraints allow_empty?: true
default ""
end
filter expr(contains(name, ^arg(:name)))
pagination offset?: true, default_limit: 10
end
end
attributes do

Loading…
Cancel
Save