diff --git a/lib/decentralised_book_index/metadata/author_role.ex b/lib/decentralised_book_index/metadata/author_role.ex index fb3c0a7..f0ffb5e 100644 --- a/lib/decentralised_book_index/metadata/author_role.ex +++ b/lib/decentralised_book_index/metadata/author_role.ex @@ -47,12 +47,12 @@ defmodule DecentralisedBookIndex.Metadata.AuthorRole do update :update do primary? true accept [:order, :role] - argument :author_id, :uuid - + argument :author_id, :uuid, allow_nil?: true require_atomic? false change fn changeset, _ -> - author_id = changeset.arguments.author_id + author_id = + Map.get(changeset.arguments, :author_id, nil) if author_id != nil do {:ok, author} = DecentralisedBookIndex.Metadata.get_author_by_id(author_id) diff --git a/lib/decentralised_book_index/metadata/book.ex b/lib/decentralised_book_index/metadata/book.ex index ad506eb..8783278 100644 --- a/lib/decentralised_book_index/metadata/book.ex +++ b/lib/decentralised_book_index/metadata/book.ex @@ -13,7 +13,7 @@ defmodule DecentralisedBookIndex.Metadata.Book do end actions do - defaults [:read, :update, :destroy] + defaults [:read, :destroy] create :create do primary? true @@ -112,6 +112,29 @@ defmodule DecentralisedBookIndex.Metadata.Book do pagination offset?: true, default_limit: 10 end + 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] + argument :bids, {:array, :map} + argument :author_roles, {:array, :map} + + change fn changeset, _ -> + registry_id = Ash.Changeset.get_attribute(changeset, :book_editions_registry_id) + + if registry_id == nil do + {:ok, registry} = DecentralisedBookIndex.Metadata.create_book_editions_registry() + + Ash.Changeset.force_change_attribute(changeset, :book_editions_registry_id, registry.id) + else + changeset + end + end + + change manage_relationship(:bids, type: :direct_control, order_is_key: :order) + change manage_relationship(:author_roles, type: :direct_control, order_is_key: :order) + end + update :assign_cover_image do accept [:cover_image_url] end diff --git a/lib/decentralised_book_index_web/live/book_live/form_component.ex b/lib/decentralised_book_index_web/live/book_live/form_component.ex index e6e1245..fd6d2b7 100644 --- a/lib/decentralised_book_index_web/live/book_live/form_component.ex +++ b/lib/decentralised_book_index_web/live/book_live/form_component.ex @@ -42,6 +42,28 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do /> <% end %> <%= if @form.source.type == :update do %> + <.input field={@form[:title]} type="text" label="Title" /> + <.input field={@form[:cover_image_url]} type="text" label="Cover image url" /> + <.input + field={@form[:description]} + type="textarea" + label="Description" + /> + <.bids_inputs form={@form} myself={@myself} /> + <.author_roles_inputs form={@form} myself={@myself} /> + <.input field={@form[:format]} type="text" label="Format" /> + <.input + field={@form[:language]} + type="text" + label="Language" + /> + <.input field={@form[:page_count]} type="number" label="Page count" /> + <.input field={@form[:publisher_id]} type="text" label="Publisher" /> + <.input + field={@form[:published]} + type="date" + label="Published" + /> <% end %> <:actions> @@ -253,6 +275,10 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do defp assign_form(%{assigns: %{book: book}} = socket) do form = if book do + book = + book + |> Ash.load!([:bids, :author_roles, :publisher]) + AshPhoenix.Form.for_update(book, :update, as: "book", actor: socket.assigns.current_user) else AshPhoenix.Form.for_create(DecentralisedBookIndex.Metadata.Book, :create,