This commit is contained in:
@@ -22,6 +22,7 @@ defmodule DecentralisedBookIndex.Metadata do
|
||||
define :list_books, action: :read
|
||||
define :get_book_by_id, args: [:id], action: :by_id
|
||||
define :get_book_alternative_editions, args: [:book], action: :get_alternative_editions
|
||||
define :get_author_books, args: [:author], action: :get_author_books
|
||||
define :update_book, action: :update
|
||||
define :destroy_book, action: :destroy
|
||||
end
|
||||
|
||||
@@ -76,6 +76,17 @@ defmodule DecentralisedBookIndex.Metadata.Book do
|
||||
and id != ^book.id)
|
||||
end
|
||||
end
|
||||
|
||||
read :get_author_books do
|
||||
argument :author, :struct, allow_nil?: false
|
||||
|
||||
prepare fn query, context ->
|
||||
author = query.arguments.author
|
||||
|
||||
Metadata.Book
|
||||
|> Ash.Query.filter(expr(exists(author_roles, author_id == ^author.id)))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
attributes do
|
||||
|
||||
@@ -59,4 +59,18 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
|
||||
assert alternatives_names = [related_book]
|
||||
end
|
||||
end
|
||||
|
||||
describe "author's books" do
|
||||
test "get the list via author's id" do
|
||||
{:ok, author} = Metadata.create_author("Author", "An description")
|
||||
|
||||
author_roles = [
|
||||
%{order: 1, author_id: author.id, role: ""}
|
||||
]
|
||||
|
||||
{:ok, book} = Metadata.create_book("Book", "1234567890", "An description", author_roles)
|
||||
|
||||
assert {:ok, books} = Metadata.get_author_books(author)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user