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
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
get :read
index :search
end
base_route "/publishers", Metadata.Publisher do
get :read
index :search
end
end
end
@ -60,7 +74,12 @@ defmodule DecentralisedBookIndex.Metadata do
resource DecentralisedBookIndex.Metadata.Author do
define :create_author,
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,
action: :add_author_to_related_alias_registry,

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

@ -2,11 +2,16 @@ defmodule DecentralisedBookIndex.Metadata.Publisher do
use Ash.Resource,
otp_app: :decentralised_book_index,
domain: DecentralisedBookIndex.Metadata,
data_layer: AshPostgres.DataLayer
data_layer: AshPostgres.DataLayer,
extensions: [AshJsonApi.Resource]
require Ash.Query
alias DecentralisedBookIndex.Metadata
json_api do
type "publisher"
end
postgres do
table "publishers"
repo DecentralisedBookIndex.Repo
@ -25,6 +30,17 @@ defmodule DecentralisedBookIndex.Metadata.Publisher do
get? true
filter expr(id == ^arg(:id))
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
attributes do

Loading…
Cancel
Save