From 71c7ce3501e1162e704f7a297fc6cb5ca0090be6 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Wed, 2 Apr 2025 09:28:31 +0300 Subject: [PATCH] Add JSON API for Book and Publisher. --- lib/decentralised_book_index/metadata.ex | 21 ++++++- lib/decentralised_book_index/metadata/book.ex | 58 ++++++++++++++++--- .../metadata/publisher.ex | 18 +++++- 3 files changed, 88 insertions(+), 9 deletions(-) diff --git a/lib/decentralised_book_index/metadata.ex b/lib/decentralised_book_index/metadata.ex index 3ab9d24..52e1100 100644 --- a/lib/decentralised_book_index/metadata.ex +++ b/lib/decentralised_book_index/metadata.ex @@ -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, diff --git a/lib/decentralised_book_index/metadata/book.ex b/lib/decentralised_book_index/metadata/book.ex index 8783278..57276d8 100644 --- a/lib/decentralised_book_index/metadata/book.ex +++ b/lib/decentralised_book_index/metadata/book.ex @@ -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} diff --git a/lib/decentralised_book_index/metadata/publisher.ex b/lib/decentralised_book_index/metadata/publisher.ex index 466cb41..38ef791 100644 --- a/lib/decentralised_book_index/metadata/publisher.ex +++ b/lib/decentralised_book_index/metadata/publisher.ex @@ -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