This commit is contained in:
@@ -61,7 +61,13 @@ defmodule DecentralisedBookIndex.Metadata do
|
||||
define :destroy_book_editions_registry, action: :destroy
|
||||
end
|
||||
|
||||
resource DecentralisedBookIndex.Metadata.AuthorRole
|
||||
resource DecentralisedBookIndex.Metadata.AuthorRole do
|
||||
define :create_author_role, action: :create, args: [:author_id, :order, :role]
|
||||
define :list_author_roles, action: :read
|
||||
define :get_author_role_by_id, args: [:id], action: :by_id
|
||||
define :update_author_role, action: :update
|
||||
define :destroy_author_role, action: :destroy
|
||||
end
|
||||
end
|
||||
|
||||
json_api do
|
||||
|
||||
@@ -18,6 +18,24 @@ defmodule DecentralisedBookIndex.Metadata.AuthorRole do
|
||||
primary? true
|
||||
accept [:order, :role]
|
||||
argument :author_id, :uuid
|
||||
|
||||
change fn changeset, _ ->
|
||||
author_id = changeset.arguments.author_id
|
||||
|
||||
if author_id != nil do
|
||||
{:ok, author} = DecentralisedBookIndex.Metadata.get_author_by_id(author_id)
|
||||
|
||||
Ash.Changeset.force_change_attribute(changeset, :author_id, author.id)
|
||||
else
|
||||
changeset
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
read :by_id do
|
||||
argument :id, :uuid, allow_nil?: false
|
||||
get? true
|
||||
filter expr(id == ^arg(:id))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
defmodule DecentralisedBookIndex.Metadata.AuthorRolesTest do
|
||||
use DecentralisedBookIndex.DataCase, async: true
|
||||
|
||||
alias DecentralisedBookIndex.Metadata
|
||||
|
||||
describe "create action" do
|
||||
test "an author's role must belongs to an author" do
|
||||
{:ok, author} = Metadata.create_author("Author", "An description")
|
||||
|
||||
assert {:ok, author_role} = Metadata.create_author_role(author.id, 1, "role")
|
||||
|
||||
{:ok, author_role} =
|
||||
author_role
|
||||
|> Ash.load(:author)
|
||||
|
||||
assert author.id == author_role.author_id
|
||||
end
|
||||
|
||||
test "an author's role can be blank" do
|
||||
{:ok, author} = Metadata.create_author("Author", "An description")
|
||||
|
||||
{:ok, author_role} = Metadata.create_author_role(author.id, 1, "")
|
||||
|
||||
refute author_role.role
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user