From 02a34694e4b7e5afede05e836689bdf8395478e2 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Tue, 3 Jun 2025 19:32:17 +0300 Subject: [PATCH] Update the Author Show LiveView to show their books. --- lib/decentralised_book_index/metadata/book.ex | 10 ++++-- .../live/author_live/show.ex | 31 +++++++++++++++++-- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/lib/decentralised_book_index/metadata/book.ex b/lib/decentralised_book_index/metadata/book.ex index ae58a53..5a3bec0 100644 --- a/lib/decentralised_book_index/metadata/book.ex +++ b/lib/decentralised_book_index/metadata/book.ex @@ -179,11 +179,17 @@ defmodule DecentralisedBookIndex.Metadata.Book do prepare fn query, context -> author = query.arguments.author - {:ok, author_ids} = Metadata.get_author_ids(author) + author_ids = + case Metadata.get_author_ids(author) do + {:ok, ids} -> ids + {:error, _error} -> [] + end - Metadata.Book + query |> Ash.Query.filter(expr(exists(author_roles, author_id in ^author_ids))) end + + pagination offset?: true, default_limit: 10 end read :search do diff --git a/lib/decentralised_book_index_web/live/author_live/show.ex b/lib/decentralised_book_index_web/live/author_live/show.ex index 3ea2c24..ae77e7b 100644 --- a/lib/decentralised_book_index_web/live/author_live/show.ex +++ b/lib/decentralised_book_index_web/live/author_live/show.ex @@ -2,6 +2,7 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Show do use DecentralisedBookIndexWeb, :live_view alias DecentralisedBookIndex.Accounts.Role + alias DecentralisedBookIndex.Metadata @impl true def render(assigns) do @@ -96,6 +97,19 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Show do <% end %> + <%= if not Enum.empty?(@page.results) do %> +

+ Books +

+
+ <%= for book <- @page.results do %> + <.book_card book={book} current_user={@current_user} /> + <% end %> +
+ + <.pagination endpoint={~p"/authors/#{@author}"} page={@page} page_params={@page_params} params={@params} /> + <% end %> + <.back navigate={~p"/authors"}>Back to authors """ end @@ -106,19 +120,30 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Show do end @impl true - def handle_params(%{"id" => id}, _, socket) do + def handle_params(%{"id" => id} = params, _, socket) do author = - Ash.get!(DecentralisedBookIndex.Metadata.Author, id, + Ash.get!(Metadata.Author, id, load: [:dbi_server], actor: socket.assigns.current_user ) - alternative_names = DecentralisedBookIndex.Metadata.get_author_alternative_names!(author) + alternative_names = Metadata.get_author_alternative_names!(author) + + page_params = AshPhoenix.LiveView.page_from_params(params, 10) + page = + Metadata.get_author_books!(author, + load: [:brief_description], + page: page_params ++ [count: true], + actor: socket.assigns.current_user + ) {:noreply, socket |> assign(:page_title, page_title(socket.assigns.live_action)) |> assign(:author, author) + |> assign(:page_params, page_params) + |> assign(:page, page) + |> assign(:params, params) |> assign(:alternative_names, alternative_names)} end