Add actions to support books' editions.
This commit is contained in:
@@ -3,7 +3,21 @@ defmodule DecentralisedBookIndex.Metadata do
|
||||
otp_app: :decentralised_book_index
|
||||
|
||||
resources do
|
||||
resource DecentralisedBookIndex.Metadata.Book
|
||||
resource DecentralisedBookIndex.Metadata.Book do
|
||||
define :create_book,
|
||||
action: :create,
|
||||
args: [:title, :isbn, :description, {:optional, :book_editions_registry_id}]
|
||||
|
||||
define :add_book_to_related_editions_registry,
|
||||
action: :add_book_to_related_editions_registry,
|
||||
args: [:title, :isbn, :description, :related_book_id]
|
||||
|
||||
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 :update_book, action: :update
|
||||
define :destroy_book, action: :destroy
|
||||
end
|
||||
|
||||
resource DecentralisedBookIndex.Metadata.Author do
|
||||
define :create_author,
|
||||
@@ -31,6 +45,12 @@ defmodule DecentralisedBookIndex.Metadata do
|
||||
define :destroy_author_alias_registry, action: :destroy
|
||||
end
|
||||
|
||||
resource DecentralisedBookIndex.Metadata.BookEditionsRegistry
|
||||
resource DecentralisedBookIndex.Metadata.BookEditionsRegistry do
|
||||
define :create_book_editions_registry, action: :create
|
||||
define :list_book_editions_registries, action: :read
|
||||
define :get_book_editions_registry_by_id, args: [:id], action: :by_id
|
||||
define :update_book_editions_registry, action: :update
|
||||
define :destroy_book_editions_registry, action: :destroy
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,13 +4,70 @@ defmodule DecentralisedBookIndex.Metadata.Book do
|
||||
domain: DecentralisedBookIndex.Metadata,
|
||||
data_layer: AshPostgres.DataLayer
|
||||
|
||||
require Ash.Query
|
||||
|
||||
postgres do
|
||||
table "books"
|
||||
repo DecentralisedBookIndex.Repo
|
||||
end
|
||||
|
||||
actions do
|
||||
defaults [:read]
|
||||
defaults [:read, :update, :destroy]
|
||||
|
||||
create :create do
|
||||
primary? true
|
||||
accept [:title, :isbn, :description, :book_editions_registry_id]
|
||||
|
||||
change fn changeset, _ ->
|
||||
registry_id = Ash.Changeset.get_attribute(changeset, :book_editions_registry_id)
|
||||
|
||||
if registry_id == nil do
|
||||
{:ok, registry} = DecentralisedBookIndex.Metadata.create_book_editions_registry()
|
||||
|
||||
Ash.Changeset.force_change_attribute(changeset, :book_editions_registry_id, registry.id)
|
||||
else
|
||||
changeset
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
create :add_book_to_related_editions_registry do
|
||||
accept [:title, :isbn, :description]
|
||||
|
||||
argument :related_book_id, :uuid do
|
||||
allow_nil? false
|
||||
end
|
||||
|
||||
change fn changeset, context ->
|
||||
related_book_id = changeset.arguments.related_book_id
|
||||
|
||||
if related_book_id == nil do
|
||||
Ash.Changeset.add_error(changeset, :related_book_id, "Related book is empty")
|
||||
else
|
||||
{:ok, related_book} = DecentralisedBookIndex.Metadata.get_book_by_id(related_book_id)
|
||||
|
||||
Ash.Changeset.force_change_attribute(changeset, :book_editions_registry_id, related_book.book_editions_registry_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
read :by_id do
|
||||
argument :id, :uuid, allow_nil?: false
|
||||
get? true
|
||||
filter expr(id == ^arg(:id))
|
||||
end
|
||||
|
||||
read :get_alternative_editions do
|
||||
argument :book, :struct, allow_nil?: false
|
||||
|
||||
prepare fn query, context ->
|
||||
book = query.arguments.book
|
||||
|
||||
DecentralisedBookIndex.Metadata.Book
|
||||
|> Ash.Query.filter(book_editions_registry_id == ^book.book_editions_registry_id
|
||||
and id != ^book.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
attributes do
|
||||
|
||||
Reference in New Issue
Block a user