Add AuthorRole to manage relationship between Author and Book.

This commit is contained in:
2025-03-09 22:03:51 +02:00
parent d4c73cb647
commit 9cdedf0c7c
9 changed files with 494 additions and 4 deletions
+4 -2
View File
@@ -6,11 +6,11 @@ defmodule DecentralisedBookIndex.Metadata do
resource DecentralisedBookIndex.Metadata.Book do
define :create_book,
action: :create,
args: [:title, :isbn, :description, {:optional, :book_editions_registry_id}]
args: [:title, :isbn, :description, :author_roles, {: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]
args: [:title, :isbn, :description, :author_roles, :related_book_id]
define :list_books, action: :read
define :get_book_by_id, args: [:id], action: :by_id
@@ -52,5 +52,7 @@ defmodule DecentralisedBookIndex.Metadata do
define :update_book_editions_registry, action: :update
define :destroy_book_editions_registry, action: :destroy
end
resource DecentralisedBookIndex.Metadata.AuthorRole
end
end
@@ -5,6 +5,7 @@ defmodule DecentralisedBookIndex.Metadata.Author do
data_layer: AshPostgres.DataLayer
require Ash.Query
alias DecentralisedBookIndex.Metadata
postgres do
table "authors"
@@ -87,7 +88,11 @@ defmodule DecentralisedBookIndex.Metadata.Author do
end
relationships do
belongs_to :author_alias_registry, DecentralisedBookIndex.Metadata.AuthorAliasRegistry do
belongs_to :author_alias_registry, Metadata.AuthorAliasRegistry do
attribute_writable? true
end
belongs_to :author_role, Metadata.AuthorRole do
attribute_writable? true
end
end
@@ -0,0 +1,49 @@
defmodule DecentralisedBookIndex.Metadata.AuthorRole do
use Ash.Resource,
otp_app: :decentralised_book_index,
domain: DecentralisedBookIndex.Metadata,
data_layer: AshPostgres.DataLayer
alias DecentralisedBookIndex.Metadata
postgres do
table "author_role"
repo DecentralisedBookIndex.Repo
end
actions do
defaults [:read, :update, :destroy]
create :create do
primary? true
accept [:order, :role]
argument :author_id, :uuid
end
end
attributes do
uuid_primary_key :id
attribute :role, :string do
allow_nil? true
public? true
end
attribute :order, :integer do
allow_nil? false
public? true
end
timestamps()
end
relationships do
belongs_to :book, Metadata.Book do
allow_nil? true
end
has_one :author, Metadata.Author do
allow_nil? false
end
end
end
+14 -1
View File
@@ -5,6 +5,7 @@ defmodule DecentralisedBookIndex.Metadata.Book do
data_layer: AshPostgres.DataLayer
require Ash.Query
alias DecentralisedBookIndex.Metadata
postgres do
table "books"
@@ -17,6 +18,7 @@ defmodule DecentralisedBookIndex.Metadata.Book do
create :create do
primary? true
accept [:title, :isbn, :description, :book_editions_registry_id]
argument :author_roles, {:array, :map}
change fn changeset, _ ->
registry_id = Ash.Changeset.get_attribute(changeset, :book_editions_registry_id)
@@ -29,6 +31,8 @@ defmodule DecentralisedBookIndex.Metadata.Book do
changeset
end
end
change manage_relationship(:author_roles, type: :direct_control, order_is_key: :order)
end
create :add_book_to_related_editions_registry do
@@ -38,6 +42,8 @@ defmodule DecentralisedBookIndex.Metadata.Book do
allow_nil? false
end
argument :author_roles, {:array, :map}
change fn changeset, context ->
related_book_id = changeset.arguments.related_book_id
@@ -49,6 +55,8 @@ defmodule DecentralisedBookIndex.Metadata.Book do
Ash.Changeset.force_change_attribute(changeset, :book_editions_registry_id, related_book.book_editions_registry_id)
end
end
change manage_relationship(:author_roles, type: :direct_control, order_is_key: :order)
end
read :by_id do
@@ -92,6 +100,11 @@ defmodule DecentralisedBookIndex.Metadata.Book do
end
relationships do
belongs_to :book_editions_registry, DecentralisedBookIndex.Metadata.BookEditionsRegistry
belongs_to :book_editions_registry, Metadata.BookEditionsRegistry
has_many :author_roles, Metadata.AuthorRole do
sort order: :asc
public? true
end
end
end