Add the search by bids for Book Index LiveView.

dev
KKlochko 3 months ago
parent 73f69a72a5
commit 0150095233

@ -180,6 +180,15 @@ defmodule DecentralisedBookIndex.Metadata.Book do
pagination offset?: true, default_limit: 10
end
read :search_by_bid do
argument :type, :string, allow_nil?: false
argument :bid, :string, allow_nil?: false
filter expr(exists(bids, type == ^arg(:type) and contains(bid, ^arg(:bid))))
pagination offset?: true, default_limit: 10
end
update :update do
primary? true
require_atomic? false

@ -106,9 +106,11 @@ defmodule DecentralisedBookIndexWeb.BookLive.Index do
search_mode = Map.get(params, "search_mode", "title") |> validate_search_mode()
page_params = AshPhoenix.LiveView.page_from_params(params, 10)
page = Metadata.search_book!(
page = search(
search_mode,
search_query,
page: page_params ++ [count: true],
page_params,
sort_by: sort_by,
actor: socket.assigns[:current_user]
)
@ -207,7 +209,7 @@ defmodule DecentralisedBookIndexWeb.BookLive.Index do
end
def validate_search_mode(key) do
valid_keys = Enum.map(sort_options(), &elem(&1, 1))
valid_keys = Enum.map(search_mode_options(), &elem(&1, 1))
if key in valid_keys do
key
@ -224,4 +226,28 @@ defmodule DecentralisedBookIndexWeb.BookLive.Index do
{:noreply, push_patch(socket, to: ~p"/books?#{params}")}
end
def search(search_mode, search_query, page_params, opts \\ []) do
sort_by = Keyword.get(opts, :sort_by, nil)
actor = Keyword.get(opts, :actor, nil)
case search_mode do
"title" ->
Metadata.search_book!(
search_query,
query: [sort_input: sort_by],
page: page_params ++ [count: true],
actor: actor
)
type ->
Metadata.search_book_by_bid!(
type,
search_query,
query: [sort_input: sort_by],
page: page_params ++ [count: true],
actor: actor
)
end
end
end

Loading…
Cancel
Save