Add BookId as ids for Book.

This commit is contained in:
2025-03-14 10:49:36 +02:00
parent 9f15260275
commit 91065e0acd
6 changed files with 337 additions and 16 deletions
+17 -9
View File
@@ -3,21 +3,30 @@ defmodule DecentralisedBookIndex.Metadata do
alias DecentralisedBookIndex.Metadata
json_api do
routes do
base_route "/author", Metadata.Author do
get :read
index :search
end
end
end
resources do
resource DecentralisedBookIndex.Metadata.Book do
define :create_book,
action: :create,
args: [
:title,
:isbn,
:description,
:bids,
: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, :author_roles, :related_book_id]
args: [:title, :description, :bids, :author_roles, :related_book_id]
define :list_books, action: :read
define :get_book_by_id, args: [:id], action: :by_id
@@ -70,14 +79,13 @@ defmodule DecentralisedBookIndex.Metadata do
define :update_author_role, action: :update
define :destroy_author_role, action: :destroy
end
end
json_api do
routes do
base_route "/author", Metadata.Author do
get :read
index :search
end
resource DecentralisedBookIndex.Metadata.BookId do
define :create_book_id, action: :create, args: [:type, :bid, :order]
define :list_book_ids, action: :read
define :get_book_id_by_id, args: [:id], action: :by_id
define :update_book_id, action: :update
define :destroy_book_id, action: :destroy
end
end
end
+11 -7
View File
@@ -17,7 +17,8 @@ defmodule DecentralisedBookIndex.Metadata.Book do
create :create do
primary? true
accept [:title, :isbn, :description, :book_editions_registry_id]
accept [:title, :description, :book_editions_registry_id]
argument :bids, {:array, :map}
argument :author_roles, {:array, :map}
change fn changeset, _ ->
@@ -32,16 +33,18 @@ defmodule DecentralisedBookIndex.Metadata.Book do
end
end
change manage_relationship(:bids, type: :direct_control, order_is_key: :order)
change manage_relationship(:author_roles, type: :direct_control, order_is_key: :order)
end
create :add_book_to_related_editions_registry do
accept [:title, :isbn, :description]
accept [:title, :description]
argument :related_book_id, :uuid do
allow_nil? false
end
argument :bids, {:array, :map}
argument :author_roles, {:array, :map}
change fn changeset, context ->
@@ -56,6 +59,7 @@ defmodule DecentralisedBookIndex.Metadata.Book do
end
end
change manage_relationship(:bids, type: :direct_control, order_is_key: :order)
change manage_relationship(:author_roles, type: :direct_control, order_is_key: :order)
end
@@ -99,11 +103,6 @@ defmodule DecentralisedBookIndex.Metadata.Book do
public? true
end
attribute :isbn, :string do
allow_nil? false
public? true
end
attribute :description, :string do
allow_nil? false
public? true
@@ -119,5 +118,10 @@ defmodule DecentralisedBookIndex.Metadata.Book do
sort order: :asc
public? true
end
has_many :bids, Metadata.BookId do
sort order: :asc
public? true
end
end
end
@@ -0,0 +1,55 @@
defmodule DecentralisedBookIndex.Metadata.BookId do
use Ash.Resource,
otp_app: :decentralised_book_index,
domain: DecentralisedBookIndex.Metadata,
data_layer: AshPostgres.DataLayer
alias DecentralisedBookIndex.Metadata
postgres do
table "book_ids"
repo DecentralisedBookIndex.Repo
end
actions do
defaults [:read, :update, :destroy]
create :create do
primary? true
accept [:type, :bid, :order]
end
read :by_id do
argument :id, :uuid, allow_nil?: false
get? true
filter expr(id == ^arg(:id))
end
end
attributes do
uuid_primary_key :id
attribute :type, :string do
allow_nil? false
public? true
end
attribute :bid, :string do
allow_nil? false
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
end
end