Add an action to get author's alternatives names.

dev
KKlochko 4 months ago
parent f0f7037447
commit ee72055f81

@ -10,6 +10,7 @@ defmodule DecentralisedBookIndex.Metadata do
define :add_author_to_related_alias_registry, action: :add_author_to_related_alias_registry, args: [:name, :description, :related_author_id] define :add_author_to_related_alias_registry, action: :add_author_to_related_alias_registry, args: [:name, :description, :related_author_id]
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 :update_author, action: :update define :update_author, action: :update
define :destroy_author, action: :destroy define :destroy_author, action: :destroy
end end

@ -4,6 +4,8 @@ defmodule DecentralisedBookIndex.Metadata.Author do
domain: DecentralisedBookIndex.Metadata, domain: DecentralisedBookIndex.Metadata,
data_layer: AshPostgres.DataLayer data_layer: AshPostgres.DataLayer
require Ash.Query
postgres do postgres do
table "authors" table "authors"
repo DecentralisedBookIndex.Repo repo DecentralisedBookIndex.Repo
@ -54,6 +56,18 @@ defmodule DecentralisedBookIndex.Metadata.Author do
get? true get? true
filter expr(id == ^arg(:id)) filter expr(id == ^arg(:id))
end end
read :get_alternative_names do
argument :author, :struct, allow_nil?: false
prepare fn query, context ->
author = query.arguments.author
DecentralisedBookIndex.Metadata.Author
|> Ash.Query.filter(author_alias_registry_id == ^author.author_alias_registry_id
and id != ^author.id)
end
end
end end
attributes do attributes do

@ -29,4 +29,22 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do
Metadata.add_author_to_related_alias_registry("Author2", "An description2", nil) Metadata.add_author_to_related_alias_registry("Author2", "An description2", nil)
end end
end end
describe "authors alternatives names" do
test "new author has no alternatives names" do
{:ok, author} = Metadata.create_author("Author", "An description")
assert {:ok, alternatives_names} = Metadata.get_author_alternative_names(author)
assert alternatives_names = []
end
test "author has related author so they has one alternative name" do
{:ok, related_author} = Metadata.create_author("Author", "An description")
{:ok, author} =
Metadata.add_author_to_related_alias_registry("Author2", "An description2", related_author.id)
assert {:ok, alternatives_names} = Metadata.get_author_alternative_names(author)
assert alternatives_names = [related_author]
end
end
end end

Loading…
Cancel
Save