Add JSON API for Book and Publisher.

dev
KKlochko 3 months ago
parent 488b610aed
commit 71c7ce3501

@ -5,10 +5,24 @@ defmodule DecentralisedBookIndex.Metadata do
json_api do json_api do
routes do routes do
base_route "/books", Metadata.Book do
get :read
index :search
related :bids, :read, primary?: true
related :author_roles, :read, primary?: true
related :publisher, :read, primary?: true
end
base_route "/author", Metadata.Author do base_route "/author", Metadata.Author do
get :read get :read
index :search index :search
end end
base_route "/publishers", Metadata.Publisher do
get :read
index :search
end
end end
end end
@ -60,7 +74,12 @@ defmodule DecentralisedBookIndex.Metadata do
resource DecentralisedBookIndex.Metadata.Author do resource DecentralisedBookIndex.Metadata.Author do
define :create_author, define :create_author,
action: :create, action: :create,
args: [:name, :description, {:optional, :avatar_url}, {:optional, :author_alias_registry_id}] args: [
:name,
:description,
{:optional, :avatar_url},
{:optional, :author_alias_registry_id}
]
define :add_author_to_related_alias_registry, define :add_author_to_related_alias_registry,
action: :add_author_to_related_alias_registry, action: :add_author_to_related_alias_registry,

@ -2,11 +2,16 @@ defmodule DecentralisedBookIndex.Metadata.Book do
use Ash.Resource, use Ash.Resource,
otp_app: :decentralised_book_index, otp_app: :decentralised_book_index,
domain: DecentralisedBookIndex.Metadata, domain: DecentralisedBookIndex.Metadata,
data_layer: AshPostgres.DataLayer data_layer: AshPostgres.DataLayer,
extensions: [AshJsonApi.Resource]
require Ash.Query require Ash.Query
alias DecentralisedBookIndex.Metadata alias DecentralisedBookIndex.Metadata
json_api do
type "book"
end
postgres do postgres do
table "books" table "books"
repo DecentralisedBookIndex.Repo repo DecentralisedBookIndex.Repo
@ -17,7 +22,19 @@ defmodule DecentralisedBookIndex.Metadata.Book do
create :create do create :create do
primary? true primary? true
accept [:title, :description, :format, :language, :page_count, :published, :publisher_id, :cover_image_url, :book_editions_registry_id]
accept [
:title,
:description,
:format,
:language,
:page_count,
:published,
:publisher_id,
:cover_image_url,
:book_editions_registry_id
]
argument :bids, {:array, :map} argument :bids, {:array, :map}
argument :author_roles, {:array, :map} argument :author_roles, {:array, :map}
@ -38,7 +55,16 @@ defmodule DecentralisedBookIndex.Metadata.Book do
end end
create :add_book_to_related_editions_registry do create :add_book_to_related_editions_registry do
accept [:title, :description, :format, :language, :page_count, :published, :publisher_id, :cover_image_url] accept [
:title,
:description,
:format,
:language,
:page_count,
:published,
:publisher_id,
:cover_image_url
]
argument :related_book_id, :uuid do argument :related_book_id, :uuid do
allow_nil? false allow_nil? false
@ -55,7 +81,11 @@ defmodule DecentralisedBookIndex.Metadata.Book do
else else
{:ok, related_book} = DecentralisedBookIndex.Metadata.get_book_by_id(related_book_id) {: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) Ash.Changeset.force_change_attribute(
changeset,
:book_editions_registry_id,
related_book.book_editions_registry_id
)
end end
end end
@ -83,8 +113,10 @@ defmodule DecentralisedBookIndex.Metadata.Book do
book = query.arguments.book book = query.arguments.book
DecentralisedBookIndex.Metadata.Book DecentralisedBookIndex.Metadata.Book
|> Ash.Query.filter(book_editions_registry_id == ^book.book_editions_registry_id |> Ash.Query.filter(
and id != ^book.id) book_editions_registry_id == ^book.book_editions_registry_id and
id != ^book.id
)
end end
end end
@ -115,7 +147,19 @@ defmodule DecentralisedBookIndex.Metadata.Book do
update :update do update :update do
primary? true primary? true
require_atomic? false require_atomic? false
accept [:title, :description, :format, :language, :page_count, :published, :publisher_id, :cover_image_url, :book_editions_registry_id]
accept [
:title,
:description,
:format,
:language,
:page_count,
:published,
:publisher_id,
:cover_image_url,
:book_editions_registry_id
]
argument :bids, {:array, :map} argument :bids, {:array, :map}
argument :author_roles, {:array, :map} argument :author_roles, {:array, :map}

@ -2,11 +2,16 @@ defmodule DecentralisedBookIndex.Metadata.Publisher do
use Ash.Resource, use Ash.Resource,
otp_app: :decentralised_book_index, otp_app: :decentralised_book_index,
domain: DecentralisedBookIndex.Metadata, domain: DecentralisedBookIndex.Metadata,
data_layer: AshPostgres.DataLayer data_layer: AshPostgres.DataLayer,
extensions: [AshJsonApi.Resource]
require Ash.Query require Ash.Query
alias DecentralisedBookIndex.Metadata alias DecentralisedBookIndex.Metadata
json_api do
type "publisher"
end
postgres do postgres do
table "publishers" table "publishers"
repo DecentralisedBookIndex.Repo repo DecentralisedBookIndex.Repo
@ -25,6 +30,17 @@ defmodule DecentralisedBookIndex.Metadata.Publisher do
get? true get? true
filter expr(id == ^arg(:id)) filter expr(id == ^arg(:id))
end end
read :search do
argument :name, :ci_string do
constraints allow_empty?: true
default ""
end
filter expr(contains(name, ^arg(:name)))
pagination offset?: true, default_limit: 10
end
end end
attributes do attributes do

Loading…
Cancel
Save