diff --git a/lib/decentralised_book_index_web/live/book_live/show.ex b/lib/decentralised_book_index_web/live/book_live/show.ex index b8b02f4..9f3d60e 100644 --- a/lib/decentralised_book_index_web/live/book_live/show.ex +++ b/lib/decentralised_book_index_web/live/book_live/show.ex @@ -1,12 +1,14 @@ defmodule DecentralisedBookIndexWeb.BookLive.Show do use DecentralisedBookIndexWeb, :live_view + alias DecentralisedBookIndex.Metadata + @impl true def render(assigns) do ~H""" <.header> - Book {@book.id} - <:subtitle>This is a book record from your database. + {@book.title} + <:subtitle>{@authors_string} <:actions> <.link patch={~p"/books/#{@book}/edit"}> @@ -46,13 +48,31 @@ defmodule DecentralisedBookIndexWeb.BookLive.Show do @impl true def handle_params(%{"id" => id}, _, socket) do + book = Ash.get!(Metadata.Book, id, actor: socket.assigns.current_user, load: [:author_roles]) + author_roles = Metadata.get_author_roles_by_book_id!(book.id, load: [:author]) + authors_string = Enum.reduce( + author_roles, "", + fn author_role, acc -> + if acc != "" do + acc <> ", " + else + acc + end + <> + if author_role.role == "" do + "#{author_role.author.name}" + else + "#{author_role.author.name} (#{author_role.role})" + end + end + ) + {:noreply, socket |> assign(:page_title, page_title(socket.assigns.live_action)) - |> assign( - :book, - Ash.get!(DecentralisedBookIndex.Metadata.Book, id, actor: socket.assigns.current_user) - )} + |> assign(:book, book) + |> assign(:authors_string, authors_string) + } end defp page_title(:show), do: "Show Book"