Update Author to add the search action and refactor the format.
This commit is contained in:
@@ -2,11 +2,19 @@ defmodule DecentralisedBookIndex.Metadata do
|
|||||||
use Ash.Domain,
|
use Ash.Domain,
|
||||||
otp_app: :decentralised_book_index
|
otp_app: :decentralised_book_index
|
||||||
|
|
||||||
|
alias DecentralisedBookIndex.Metadata
|
||||||
|
|
||||||
resources do
|
resources do
|
||||||
resource DecentralisedBookIndex.Metadata.Book do
|
resource DecentralisedBookIndex.Metadata.Book do
|
||||||
define :create_book,
|
define :create_book,
|
||||||
action: :create,
|
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,
|
define :add_book_to_related_editions_registry,
|
||||||
action: :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 :list_authors, action: :read
|
||||||
define :get_author_by_id, args: [:id], action: :by_id
|
define :get_author_by_id, args: [:id], action: :by_id
|
||||||
define :get_author_alternative_names, args: [:author], action: :get_alternative_names
|
define :get_author_alternative_names, args: [:author], action: :get_alternative_names
|
||||||
|
define :search_author, action: :search, args: [:name]
|
||||||
define :update_author, action: :update
|
define :update_author, action: :update
|
||||||
define :destroy_author, action: :destroy
|
define :destroy_author, action: :destroy
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -45,9 +45,14 @@ defmodule DecentralisedBookIndex.Metadata.Author do
|
|||||||
if related_author_id == nil do
|
if related_author_id == nil do
|
||||||
Ash.Changeset.add_error(changeset, :related_author_id, "Related author is empty")
|
Ash.Changeset.add_error(changeset, :related_author_id, "Related author is empty")
|
||||||
else
|
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
|
end
|
||||||
end
|
end
|
||||||
@@ -65,10 +70,23 @@ defmodule DecentralisedBookIndex.Metadata.Author do
|
|||||||
author = query.arguments.author
|
author = query.arguments.author
|
||||||
|
|
||||||
DecentralisedBookIndex.Metadata.Author
|
DecentralisedBookIndex.Metadata.Author
|
||||||
|> Ash.Query.filter(author_alias_registry_id == ^author.author_alias_registry_id
|
|> Ash.Query.filter(
|
||||||
and id != ^author.id)
|
author_alias_registry_id == ^author.author_alias_registry_id and
|
||||||
|
id != ^author.id
|
||||||
|
)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
attributes do
|
attributes do
|
||||||
|
|||||||
Reference in New Issue
Block a user