Update the code format.

dev
KKlochko 2 months ago
parent cffbeb04e7
commit ebbee7f9a5

@ -48,6 +48,18 @@ defmodule DecentralisedBookIndex.Accounts.User do
repo DecentralisedBookIndex.Repo repo DecentralisedBookIndex.Repo
end end
code_interface do
define :list_moderators
define :search, action: :search, args: [:email]
define :get_by_id, action: :read, get_by: [:id]
define :get_by_email, args: [:email]
define :set_role, args: [:role]
define :make_moderator
define :make_user
end
actions do actions do
defaults [:read] defaults [:read]
@ -289,16 +301,4 @@ defmodule DecentralisedBookIndex.Accounts.User do
identities do identities do
identity :unique_email, [:email] identity :unique_email, [:email]
end end
code_interface do
define :list_moderators
define :search, action: :search, args: [:email]
define :get_by_id, action: :read, get_by: [:id]
define :get_by_email, args: [:email]
define :set_role, args: [:role]
define :make_moderator
define :make_user
end
end end

@ -81,7 +81,8 @@ defmodule DecentralisedBookIndex.Metadata.Book do
registry_id = Ash.Changeset.get_attribute(changeset, :book_editions_registry_id) registry_id = Ash.Changeset.get_attribute(changeset, :book_editions_registry_id)
if registry_id == nil do if registry_id == nil do
registry = DecentralisedBookIndex.Metadata.create_book_editions_registry!(authorize?: false) registry =
DecentralisedBookIndex.Metadata.create_book_editions_registry!(authorize?: false)
Ash.Changeset.force_change_attribute(changeset, :book_editions_registry_id, registry.id) Ash.Changeset.force_change_attribute(changeset, :book_editions_registry_id, registry.id)
else else

@ -7,7 +7,9 @@ defmodule DecentralisedBookIndex.Sync.ApiClients.FetchJson do
else else
{:error, data} {:error, data}
end end
{:error, message} -> {:error, message}
{:error, message} ->
{:error, message}
end end
end end
end end

@ -18,6 +18,7 @@ defmodule DecentralisedBookIndex.Sync.ApiClients.FetchJsons do
case FetchJson.get(next) do case FetchJson.get(next) do
{:ok, page} -> {:ok, page} ->
{mapper.(page["data"]), get_in(page, ["links", "next"])} {mapper.(page["data"]), get_in(page, ["links", "next"])}
{:error, message} -> {:error, message} ->
Logger.error("FetchJsons error for #{url}: #{inspect(message)}") Logger.error("FetchJsons error for #{url}: #{inspect(message)}")
{[], nil} {[], nil}

@ -13,7 +13,7 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformer do
name: get_in(json_body, ["attributes", "name"]), name: get_in(json_body, ["attributes", "name"]),
description: get_in(json_body, ["attributes", "description"]), description: get_in(json_body, ["attributes", "description"]),
avatar_url: get_in(json_body, ["attributes", "avatar_url"]), avatar_url: get_in(json_body, ["attributes", "avatar_url"]),
author_alias_registry_id: get_in(json_body, ["attributes", "author_alias_registry_id"]), author_alias_registry_id: get_in(json_body, ["attributes", "author_alias_registry_id"])
} }
{:ok, attrs} {:ok, attrs}

@ -19,6 +19,7 @@ defmodule DecentralisedBookIndex.Sync.BookSync do
|> Ash.update!(authorize?: false) |> Ash.update!(authorize?: false)
:ok :ok
{:error, %Ash.Error.Query.NotFound{}} -> {:error, %Ash.Error.Query.NotFound{}} ->
attrs = attrs =
attrs attrs

@ -15,6 +15,7 @@ defmodule DecentralisedBookIndex.Sync.PublisherSync do
|> Ash.update!(authorize?: false) |> Ash.update!(authorize?: false)
:ok :ok
{:error, %Ash.Error.Query.NotFound{}} -> {:error, %Ash.Error.Query.NotFound{}} ->
attrs = attrs =
attrs attrs

@ -28,6 +28,7 @@ defmodule DecentralisedBookIndex.SyncTasks.SyncServerTask do
end end
defp params(datetime) when is_nil(datetime), do: "" defp params(datetime) when is_nil(datetime), do: ""
defp params(datetime) when not is_nil(datetime) do defp params(datetime) when not is_nil(datetime) do
"?filter[updated_at][gte]=#{DateTime.to_iso8601(datetime)}" "?filter[updated_at][gte]=#{DateTime.to_iso8601(datetime)}"
end end

@ -12,13 +12,15 @@ defmodule DecentralisedBookIndexWeb.Components.MyComponents do
import MyComponents.JustSearchResources, only: [just_search_resources: 1] import MyComponents.JustSearchResources, only: [just_search_resources: 1]
import MyComponents.Pagination, only: [pagination: 1] import MyComponents.Pagination, only: [pagination: 1]
import MyComponents.Buttons, only: [
import MyComponents.Buttons,
only: [
success_button: 1, success_button: 1,
primary_button: 1, primary_button: 1,
add_button: 1, add_button: 1,
save_button: 1, save_button: 1,
edit_button: 1, edit_button: 1,
cancel_button: 1, cancel_button: 1
] ]
alias MyComponents.SelectPublisher alias MyComponents.SelectPublisher

@ -84,7 +84,7 @@ defmodule DecentralisedBookIndexWeb.Components.MyComponents.Pagination do
%{count: count, limit: limit} = pages %{count: count, limit: limit} = pages
max_number = max_number =
count/limit (count / limit)
|> :math.ceil() |> :math.ceil()
|> Kernel.trunc() |> Kernel.trunc()
@ -94,7 +94,7 @@ defmodule DecentralisedBookIndexWeb.Components.MyComponents.Pagination do
defp get_current_page(pages) do defp get_current_page(pages) do
%{offset: offset, limit: limit} = pages %{offset: offset, limit: limit} = pages
Kernel.trunc(offset/limit) +1 Kernel.trunc(offset / limit) + 1
end end
def page_params_which(page, params, which) do def page_params_which(page, params, which) do
@ -111,7 +111,7 @@ defmodule DecentralisedBookIndexWeb.Components.MyComponents.Pagination do
def page_params_number(page_params, params, number) do def page_params_number(page_params, params, number) do
limit = Keyword.get(page_params, :limit) limit = Keyword.get(page_params, :limit)
offset = limit*(number-1) offset = limit * (number - 1)
params params
|> Map.put("limit", limit) |> Map.put("limit", limit)

@ -74,7 +74,10 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.FormComponent do
end end
def handle_event("save", %{"author" => author_params}, socket) do def handle_event("save", %{"author" => author_params}, socket) do
case AshPhoenix.Form.submit(socket.assigns.form, params: author_params, actor: socket.assigns.current_user) do case AshPhoenix.Form.submit(socket.assigns.form,
params: author_params,
actor: socket.assigns.current_user
) do
{:ok, author} -> {:ok, author} ->
notify_parent({:saved, author}) notify_parent({:saved, author})
@ -103,7 +106,9 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.FormComponent do
defp notify_parent(msg), do: send(self(), {__MODULE__, msg}) defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
defp assign_form(%{assigns: %{author: author, alternative_author_id: alternative_author_id}} = socket) do defp assign_form(
%{assigns: %{author: author, alternative_author_id: alternative_author_id}} = socket
) do
alias_id = alias_id =
if alternative_author_id == nil do if alternative_author_id == nil do
nil nil

@ -60,7 +60,8 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Index do
sort_by = Map.get(params, "sort_by", "name") |> validate_sort_by() sort_by = Map.get(params, "sort_by", "name") |> validate_sort_by()
page_params = AshPhoenix.LiveView.page_from_params(params, 10) page_params = AshPhoenix.LiveView.page_from_params(params, 10)
page = Metadata.search_author!( page =
Metadata.search_author!(
search_query, search_query,
query: [sort_input: sort_by], query: [sort_input: sort_by],
page: page_params ++ [count: true], page: page_params ++ [count: true],
@ -110,7 +111,7 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Index do
[ [
{"Sort by name", "name"}, {"Sort by name", "name"},
{"Sort by recently updated", "-updated_at"}, {"Sort by recently updated", "-updated_at"},
{"Sort by recently added", "-inserted_at"}, {"Sort by recently added", "-inserted_at"}
] ]
end end

@ -71,15 +71,16 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Show do
@impl true @impl true
def handle_params(%{"id" => id}, _, socket) do def handle_params(%{"id" => id}, _, socket) do
author = Ash.get!(DecentralisedBookIndex.Metadata.Author, id, actor: socket.assigns.current_user) author =
Ash.get!(DecentralisedBookIndex.Metadata.Author, id, actor: socket.assigns.current_user)
alternative_names = DecentralisedBookIndex.Metadata.get_author_alternative_names!(author) alternative_names = DecentralisedBookIndex.Metadata.get_author_alternative_names!(author)
{:noreply, {:noreply,
socket socket
|> assign(:page_title, page_title(socket.assigns.live_action)) |> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:author, author) |> assign(:author, author)
|> assign(:alternative_names, alternative_names) |> assign(:alternative_names, alternative_names)}
}
end end
defp page_title(:show), do: "Show Author" defp page_title(:show), do: "Show Author"

@ -373,7 +373,9 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
defp set_book_edition_registry_id(form, alternative_edition_id) do defp set_book_edition_registry_id(form, alternative_edition_id) do
if alternative_edition_id != nil do if alternative_edition_id != nil do
form form
|> AshPhoenix.Form.update_params(&Map.put(&1, "book_editions_registry_id", alternative_edition_id)) |> AshPhoenix.Form.update_params(
&Map.put(&1, "book_editions_registry_id", alternative_edition_id)
)
else else
form form
end end

@ -71,7 +71,8 @@ defmodule DecentralisedBookIndexWeb.BookLive.Index do
search_mode = Map.get(params, "search_mode", "title") |> validate_search_mode() search_mode = Map.get(params, "search_mode", "title") |> validate_search_mode()
page_params = AshPhoenix.LiveView.page_from_params(params, 10) page_params = AshPhoenix.LiveView.page_from_params(params, 10)
page = search( page =
search(
search_mode, search_mode,
search_query, search_query,
page_params, page_params,
@ -129,7 +130,7 @@ defmodule DecentralisedBookIndexWeb.BookLive.Index do
[ [
{"Sort by title", "title"}, {"Sort by title", "title"},
{"Sort by recently updated", "-updated_at"}, {"Sort by recently updated", "-updated_at"},
{"Sort by recently added", "-inserted_at"}, {"Sort by recently added", "-inserted_at"}
] ]
end end
@ -157,7 +158,7 @@ defmodule DecentralisedBookIndexWeb.BookLive.Index do
{"Search by title", "title"}, {"Search by title", "title"},
{"Search by isbn13", "isbn13"}, {"Search by isbn13", "isbn13"},
{"Search by isbn", "isbn"}, {"Search by isbn", "isbn"},
{"Search by asin", "asin"}, {"Search by asin", "asin"}
] ]
end end
@ -212,5 +213,4 @@ defmodule DecentralisedBookIndexWeb.BookLive.Index do
{:noreply, push_patch(socket, to: ~p"/books?#{params}")} {:noreply, push_patch(socket, to: ~p"/books?#{params}")}
end end
end end

@ -99,15 +99,19 @@ defmodule DecentralisedBookIndexWeb.BookLive.Show do
@impl true @impl true
def handle_params(%{"id" => id}, _, socket) do def handle_params(%{"id" => id}, _, socket) do
book = Ash.get!(Metadata.Book, id, actor: socket.assigns.current_user, load: [:bids, :author_roles, :publisher]) book =
Ash.get!(Metadata.Book, id,
actor: socket.assigns.current_user,
load: [:bids, :author_roles, :publisher]
)
alternative_editions = Metadata.get_book_alternative_editions!(book) alternative_editions = Metadata.get_book_alternative_editions!(book)
{:noreply, {:noreply,
socket socket
|> assign(:page_title, page_title(socket.assigns.live_action)) |> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:book, book) |> assign(:book, book)
|> assign(:alternative_editions, alternative_editions) |> assign(:alternative_editions, alternative_editions)}
}
end end
defp page_title(:show), do: "Show Book" defp page_title(:show), do: "Show Book"
@ -118,6 +122,7 @@ defmodule DecentralisedBookIndexWeb.BookLive.Show do
def authors(assigns) do def authors(assigns) do
author_roles = Metadata.get_author_roles_by_book_id!(assigns.book.id, load: [:author]) author_roles = Metadata.get_author_roles_by_book_id!(assigns.book.id, load: [:author])
author_count = length(author_roles) author_count = length(author_roles)
authors_string = authors_string =
author_roles author_roles
|> Enum.map(fn author_role -> |> Enum.map(fn author_role ->

@ -50,7 +50,10 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.Edit do
end end
@impl true @impl true
def handle_info({DecentralisedBookIndexWeb.DbiServerLive.FormComponent, {:saved, _dbi_server}}, socket) do def handle_info(
{DecentralisedBookIndexWeb.DbiServerLive.FormComponent, {:saved, _dbi_server}},
socket
) do
{:noreply, socket} {:noreply, socket}
end end
end end

@ -100,7 +100,8 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.Index do
sort_by = Map.get(params, "sort_by", "name") |> validate_sort_by() sort_by = Map.get(params, "sort_by", "name") |> validate_sort_by()
page_params = AshPhoenix.LiveView.page_from_params(params, 10) page_params = AshPhoenix.LiveView.page_from_params(params, 10)
page = Metadata.search_dbi_server!( page =
Metadata.search_dbi_server!(
search_query, search_query,
query: [sort_input: sort_by], query: [sort_input: sort_by],
page: page_params ++ [count: true], page: page_params ++ [count: true],
@ -163,7 +164,7 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.Index do
[ [
{"Sort by name", "name"}, {"Sort by name", "name"},
{"Sort by recently updated", "-updated_at"}, {"Sort by recently updated", "-updated_at"},
{"Sort by recently added", "-inserted_at"}, {"Sort by recently added", "-inserted_at"}
] ]
end end

@ -50,7 +50,10 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Edit do
end end
@impl true @impl true
def handle_info({DecentralisedBookIndexWeb.PublisherLive.FormComponent, {:saved, _publisher}}, socket) do def handle_info(
{DecentralisedBookIndexWeb.PublisherLive.FormComponent, {:saved, _publisher}},
socket
) do
{:noreply, socket} {:noreply, socket}
end end
end end

@ -71,7 +71,8 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Index do
sort_by = Map.get(params, "sort_by", "name") |> validate_sort_by() sort_by = Map.get(params, "sort_by", "name") |> validate_sort_by()
page_params = AshPhoenix.LiveView.page_from_params(params, 10) page_params = AshPhoenix.LiveView.page_from_params(params, 10)
page = Metadata.search_publisher!( page =
Metadata.search_publisher!(
search_query, search_query,
query: [sort_input: sort_by], query: [sort_input: sort_by],
page: page_params ++ [count: true], page: page_params ++ [count: true],
@ -124,7 +125,7 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Index do
[ [
{"Sort by name", "name"}, {"Sort by name", "name"},
{"Sort by recently updated", "-updated_at"}, {"Sort by recently updated", "-updated_at"},
{"Sort by recently added", "-inserted_at"}, {"Sort by recently added", "-inserted_at"}
] ]
end end

@ -56,7 +56,6 @@ defmodule DecentralisedBookIndexWeb.UserLive.Index do
/> />
</div> </div>
<% end %> <% end %>
""" """
end end
@ -71,7 +70,8 @@ defmodule DecentralisedBookIndexWeb.UserLive.Index do
sort_by = Map.get(params, "sort_by", "name") |> validate_sort_by() sort_by = Map.get(params, "sort_by", "name") |> validate_sort_by()
page_params = AshPhoenix.LiveView.page_from_params(params, 10) page_params = AshPhoenix.LiveView.page_from_params(params, 10)
page = User.search!( page =
User.search!(
search_query, search_query,
query: [sort_input: sort_by], query: [sort_input: sort_by],
page: page_params ++ [count: true], page: page_params ++ [count: true],
@ -101,7 +101,7 @@ defmodule DecentralisedBookIndexWeb.UserLive.Index do
[ [
{"Sort by email", "email"}, {"Sort by email", "email"},
{"Sort by role", "role"}, {"Sort by role", "role"},
{"Sort by role desc", "-role"}, {"Sort by role desc", "-role"}
] ]
end end

@ -10,28 +10,42 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do
describe "authors and registries relationship" do describe "authors and registries relationship" do
test "a new author get new registry by default", %{user: user} do test "a new author get new registry by default", %{user: user} do
assert {:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user) assert {:ok, author} =
Metadata.create_author("Author", "An description", nil, nil, actor: user)
assert author.author_alias_registry_id != nil assert author.author_alias_registry_id != nil
end end
test "a new author belongs to a registry if specified", %{user: user} do test "a new author belongs to a registry if specified", %{user: user} do
assert {:ok, registry} = Metadata.create_author_alias_registry(actor: user) assert {:ok, registry} = Metadata.create_author_alias_registry(actor: user)
assert {:ok, author} = Metadata.create_author("Author", "An description", nil, registry.id, actor: user)
assert {:ok, author} =
Metadata.create_author("Author", "An description", nil, registry.id, actor: user)
assert author.author_alias_registry_id == registry.id assert author.author_alias_registry_id == registry.id
end end
test "a new author to a registry via a related author record", %{user: user} do test "a new author to a registry via a related author record", %{user: user} do
{:ok, related_author} = Metadata.create_author("Author", "An description", nil, nil, actor: user) {:ok, related_author} =
Metadata.create_author("Author", "An description", nil, nil, actor: user)
assert {:ok, author} = assert {:ok, author} =
Metadata.add_author_to_related_alias_registry("Author2", "An description2", related_author.id, nil, actor: user) Metadata.add_author_to_related_alias_registry(
"Author2",
"An description2",
related_author.id,
nil,
actor: user
)
assert related_author.author_alias_registry_id == author.author_alias_registry_id assert related_author.author_alias_registry_id == author.author_alias_registry_id
end end
test "a new author to a registry via a empty related author record", %{user: user} do test "a new author to a registry via a empty related author record", %{user: user} do
assert {:error, _} = assert {:error, _} =
Metadata.add_author_to_related_alias_registry("Author2", "An description2", nil, actor: user) Metadata.add_author_to_related_alias_registry("Author2", "An description2", nil,
actor: user
)
end end
end end
@ -43,10 +57,17 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do
end end
test "author has related author so they has one alternative name", %{user: user} do test "author has related author so they has one alternative name", %{user: user} do
{:ok, related_author} = Metadata.create_author("Author", "An description", nil, nil, actor: user) {:ok, related_author} =
Metadata.create_author("Author", "An description", nil, nil, actor: user)
{:ok, author} = {:ok, author} =
Metadata.add_author_to_related_alias_registry("Author2", "An description2", related_author.id, nil, actor: user) Metadata.add_author_to_related_alias_registry(
"Author2",
"An description2",
related_author.id,
nil,
actor: user
)
assert {:ok, alternatives_names} = Metadata.get_author_alternative_names(author) assert {:ok, alternatives_names} = Metadata.get_author_alternative_names(author)
assert alternatives_names = [related_author] assert alternatives_names = [related_author]
@ -63,10 +84,24 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do
test "the list has the aliases' ids", %{user: user} do test "the list has the aliases' ids", %{user: user} do
{:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user) {:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
{:ok, alias1} = {:ok, alias1} =
Metadata.add_author_to_related_alias_registry("Author2", "An description2", author.id, nil, actor: user) Metadata.add_author_to_related_alias_registry(
"Author2",
"An description2",
author.id,
nil,
actor: user
)
{:ok, alias2} = {:ok, alias2} =
Metadata.add_author_to_related_alias_registry("Author3", "An description3", author.id, nil, actor: user) Metadata.add_author_to_related_alias_registry(
"Author3",
"An description3",
author.id,
nil,
actor: user
)
assert {:ok, ids} = Metadata.get_author_ids(author) assert {:ok, ids} = Metadata.get_author_ids(author)
@ -78,14 +113,18 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do
describe "authors and avatar_url" do describe "authors and avatar_url" do
test "a new author has no avatar by default", %{user: user} do test "a new author has no avatar by default", %{user: user} do
assert {:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user) assert {:ok, author} =
Metadata.create_author("Author", "An description", nil, nil, actor: user)
assert author.avatar_url == nil assert author.avatar_url == nil
end end
test "a new author has a avatar", %{user: user} do test "a new author has a avatar", %{user: user} do
avatar_url = "/images/avatar.png" avatar_url = "/images/avatar.png"
assert {:ok, author} = Metadata.create_author("Author", "An description", avatar_url, nil, actor: user) assert {:ok, author} =
Metadata.create_author("Author", "An description", avatar_url, nil, actor: user)
assert author.avatar_url == avatar_url assert author.avatar_url == avatar_url
end end
end end

@ -14,7 +14,22 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
bids = bids() bids = bids()
publisher = generate(publisher()) publisher = generate(publisher())
assert {:ok, book} = Metadata.create_book("Book", "An description", "English", "Paperback", 256, ~D[2025-03-04], bids, author_roles, publisher.id, nil, nil, actor: user) assert {:ok, book} =
Metadata.create_book(
"Book",
"An description",
"English",
"Paperback",
256,
~D[2025-03-04],
bids,
author_roles,
publisher.id,
nil,
nil,
actor: user
)
assert book.book_editions_registry_id != nil assert book.book_editions_registry_id != nil
end end
@ -24,7 +39,23 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
publisher = generate(publisher()) publisher = generate(publisher())
assert {:ok, registry} = Metadata.create_book_editions_registry(actor: user) assert {:ok, registry} = Metadata.create_book_editions_registry(actor: user)
assert {:ok, book} = Metadata.create_book("Book", "An description","English", "Paperback", 256, ~D[2025-03-04], bids, author_roles, publisher.id, nil, registry.id, actor: user)
assert {:ok, book} =
Metadata.create_book(
"Book",
"An description",
"English",
"Paperback",
256,
~D[2025-03-04],
bids,
author_roles,
publisher.id,
nil,
registry.id,
actor: user
)
assert book.book_editions_registry_id == registry.id assert book.book_editions_registry_id == registry.id
end end
@ -33,10 +64,37 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
bids = bids() bids = bids()
publisher = generate(publisher()) publisher = generate(publisher())
{:ok, related_book} = Metadata.create_book("Book", "An description", "English", "Paperback", 256, ~D[2025-03-04], bids, author_roles, publisher.id, nil, nil, actor: user) {:ok, related_book} =
Metadata.create_book(
"Book",
"An description",
"English",
"Paperback",
256,
~D[2025-03-04],
bids,
author_roles,
publisher.id,
nil,
nil,
actor: user
)
assert {:ok, book} = assert {:ok, book} =
Metadata.add_book_to_related_editions_registry("Book2", "An description2", "English", "Paperback", 256, ~D[2025-03-04], bids, author_roles, publisher.id, related_book.id, nil, actor: user) Metadata.add_book_to_related_editions_registry(
"Book2",
"An description2",
"English",
"Paperback",
256,
~D[2025-03-04],
bids,
author_roles,
publisher.id,
related_book.id,
nil,
actor: user
)
assert related_book.book_editions_registry_id == book.book_editions_registry_id assert related_book.book_editions_registry_id == book.book_editions_registry_id
end end
@ -47,7 +105,18 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
publisher = generate(publisher()) publisher = generate(publisher())
assert {:error, _} = assert {:error, _} =
Metadata.add_book_to_related_editions_registry("Book2", "An description2", "English", "Paperback", 256, ~D[2025-03-04], bids, author_roles, publisher.id, nil) Metadata.add_book_to_related_editions_registry(
"Book2",
"An description2",
"English",
"Paperback",
256,
~D[2025-03-04],
bids,
author_roles,
publisher.id,
nil
)
end end
end end
@ -57,7 +126,22 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
bids = bids() bids = bids()
publisher = generate(publisher()) publisher = generate(publisher())
{:ok, book} = Metadata.create_book("Book", "An description", "English", "Paperback", 256, ~D[2025-03-04], bids, author_roles, publisher.id, nil, nil, actor: user) {:ok, book} =
Metadata.create_book(
"Book",
"An description",
"English",
"Paperback",
256,
~D[2025-03-04],
bids,
author_roles,
publisher.id,
nil,
nil,
actor: user
)
assert {:ok, alternatives_names} = Metadata.get_book_alternative_editions(book) assert {:ok, alternatives_names} = Metadata.get_book_alternative_editions(book)
assert alternatives_names = [] assert alternatives_names = []
end end
@ -67,10 +151,37 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
bids = bids() bids = bids()
publisher = generate(publisher()) publisher = generate(publisher())
{:ok, related_book} = Metadata.create_book("Book", "An description", "English", "Paperback", 256, ~D[2025-03-04], bids, author_roles, publisher.id, nil, nil, actor: user) {:ok, related_book} =
Metadata.create_book(
"Book",
"An description",
"English",
"Paperback",
256,
~D[2025-03-04],
bids,
author_roles,
publisher.id,
nil,
nil,
actor: user
)
{:ok, book} = {:ok, book} =
Metadata.add_book_to_related_editions_registry("Book2", "An description2", "English", "Paperback", 256, ~D[2025-03-04], bids, author_roles, publisher.id, related_book.id, nil, actor: user) Metadata.add_book_to_related_editions_registry(
"Book2",
"An description2",
"English",
"Paperback",
256,
~D[2025-03-04],
bids,
author_roles,
publisher.id,
related_book.id,
nil,
actor: user
)
assert {:ok, alternatives_names} = Metadata.get_book_alternative_editions(book) assert {:ok, alternatives_names} = Metadata.get_book_alternative_editions(book)
assert alternatives_names = [related_book] assert alternatives_names = [related_book]
@ -87,17 +198,45 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
%{order: 1, author_id: author.id, role: ""} %{order: 1, author_id: author.id, role: ""}
] ]
{:ok, book} = Metadata.create_book("Book", "An description", "English", "Paperback", 256, ~D[2025-03-04], bids, author_roles, publisher.id, nil, nil, actor: user) {:ok, book} =
Metadata.create_book(
"Book",
"An description",
"English",
"Paperback",
256,
~D[2025-03-04],
bids,
author_roles,
publisher.id,
nil,
nil,
actor: user
)
assert {:ok, books} = Metadata.get_author_books(author) assert {:ok, books} = Metadata.get_author_books(author)
end end
test "get the list contains aliases' books", %{user: user} do test "get the list contains aliases' books", %{user: user} do
{:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user) {:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
{:ok, alias1} = {:ok, alias1} =
Metadata.add_author_to_related_alias_registry("Author2", "An description2", author.id, nil, actor: user) Metadata.add_author_to_related_alias_registry(
"Author2",
"An description2",
author.id,
nil,
actor: user
)
{:ok, alias2} = {:ok, alias2} =
Metadata.add_author_to_related_alias_registry("Author3", "An description3", author.id, nil, actor: user) Metadata.add_author_to_related_alias_registry(
"Author3",
"An description3",
author.id,
nil,
actor: user
)
book = generate(book(authors: [author])) book = generate(book(authors: [author]))
book2 = generate(book(authors: [alias1])) book2 = generate(book(authors: [alias1]))

@ -14,12 +14,11 @@ defmodule DecentralisedBookIndex.Metadata.Forms.AuthorFormTest do
valid_params = %{ valid_params = %{
"name" => "Oleh", "name" => "Oleh",
"description" => "A cool author", "description" => "A cool author"
} }
assert_raise Ash.Error.Forbidden, fn -> assert_raise Ash.Error.Forbidden, fn ->
form = \ form =
AshPhoenix.Form.for_create(Metadata.Author, :create, AshPhoenix.Form.for_create(Metadata.Author, :create,
as: "author", as: "author",
actor: user actor: user
@ -31,7 +30,7 @@ defmodule DecentralisedBookIndex.Metadata.Forms.AuthorFormTest do
test "moderator can submit", %{user: user} do test "moderator can submit", %{user: user} do
valid_params = %{ valid_params = %{
"name" => "Oleh", "name" => "Oleh",
"description" => "A cool author", "description" => "A cool author"
} }
assert form = assert form =
@ -47,7 +46,7 @@ defmodule DecentralisedBookIndex.Metadata.Forms.AuthorFormTest do
test "just name and description", %{user: user} do test "just name and description", %{user: user} do
valid_params = %{ valid_params = %{
"name" => "Oleh", "name" => "Oleh",
"description" => "A cool author", "description" => "A cool author"
} }
form = form =
@ -66,7 +65,7 @@ defmodule DecentralisedBookIndex.Metadata.Forms.AuthorFormTest do
valid_params = %{ valid_params = %{
"name" => "Another Oleh", "name" => "Another Oleh",
"description" => "A cool author", "description" => "A cool author"
} }
form = form =

@ -16,7 +16,9 @@ defmodule DecentralisedBookIndex.Sync.ApiClients.FetchJsonTest do
test "get an author", %{user: user} do test "get an author", %{user: user} do
{:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user) {:ok, author} = Metadata.create_author("Author", "An description", nil, nil, actor: user)
assert {:ok, data} = FetchJson.get("#{@test_server_endpoint}/api/v1/json/authors/#{author.id}") assert {:ok, data} =
FetchJson.get("#{@test_server_endpoint}/api/v1/json/authors/#{author.id}")
assert data["data"]["id"] == author.id assert data["data"]["id"] == author.id
end end
end end

@ -16,7 +16,9 @@ defmodule DecentralisedBookIndex.Sync.ApiClients.FetchJsonsTest do
describe "authors api" do describe "authors api" do
test "get authors", %{user: user} do test "get authors", %{user: user} do
for number <- 1..11 do for number <- 1..11 do
Metadata.create_author!("Author#{number}", "An description#{number}", nil, nil, actor: user) Metadata.create_author!("Author#{number}", "An description#{number}", nil, nil,
actor: user
)
end end
assert {:ok, records} = FetchJsons.get("#{@test_server_endpoint}/api/v1/json/authors") assert {:ok, records} = FetchJsons.get("#{@test_server_endpoint}/api/v1/json/authors")
@ -25,7 +27,9 @@ defmodule DecentralisedBookIndex.Sync.ApiClients.FetchJsonsTest do
test "get authors names from API", %{user: user} do test "get authors names from API", %{user: user} do
for number <- 1..11 do for number <- 1..11 do
Metadata.create_author!("Author#{number}", "An description#{number}", nil, nil, actor: user) Metadata.create_author!("Author#{number}", "An description#{number}", nil, nil,
actor: user
)
end end
get_author_names = fn data -> get_author_names = fn data ->

@ -21,12 +21,14 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformerTest do
}, },
"jsonapi" => %{"version" => "1.0"}, "jsonapi" => %{"version" => "1.0"},
"links" => %{ "links" => %{
"self" => "http://localhost:4000/api/v1/json/author/889a323e-d104-4b5d-b276-dad5a9b1da9d" "self" =>
"http://localhost:4000/api/v1/json/author/889a323e-d104-4b5d-b276-dad5a9b1da9d"
}, },
"meta" => %{} "meta" => %{}
} }
assert {:ok, author} = AuthorTransformer.from_json(json_body) assert {:ok, author} = AuthorTransformer.from_json(json_body)
assert %{ assert %{
id: "889a323e-d104-4b5d-b276-dad5a9b1da9d", id: "889a323e-d104-4b5d-b276-dad5a9b1da9d",
name: "Author", name: "Author",
@ -52,6 +54,7 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformerTest do
} }
assert {:ok, author} = AuthorTransformer.from_json(json_body) assert {:ok, author} = AuthorTransformer.from_json(json_body)
assert %{ assert %{
id: "889a323e-d104-4b5d-b276-dad5a9b1da9d", id: "889a323e-d104-4b5d-b276-dad5a9b1da9d",
name: "Author", name: "Author",

@ -17,7 +17,7 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorAliasRegistrySyncTe
server = generate(dbi_server(url: @test_server_endpoint)) server = generate(dbi_server(url: @test_server_endpoint))
attrs = %{ attrs = %{
id: "889a323e-d104-4b5d-b276-dad5a9b1da9d", id: "889a323e-d104-4b5d-b276-dad5a9b1da9d"
} }
assert :ok = AuthorAliasRegistrySync.create_update(attrs, server.id) assert :ok = AuthorAliasRegistrySync.create_update(attrs, server.id)
@ -31,7 +31,7 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorAliasRegistrySyncTe
{:ok, same_alias} = Metadata.create_author_alias_registry(actor: user) {:ok, same_alias} = Metadata.create_author_alias_registry(actor: user)
attrs = %{ attrs = %{
id: same_alias.id, id: same_alias.id
} }
assert :ok = AuthorAliasRegistrySync.create_update(attrs, server.id) assert :ok = AuthorAliasRegistrySync.create_update(attrs, server.id)

@ -17,7 +17,7 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.BookEditionsRegistrySyncT
server = generate(dbi_server(url: @test_server_endpoint)) server = generate(dbi_server(url: @test_server_endpoint))
attrs = %{ attrs = %{
id: "889a323e-d104-4b5d-b276-dad5a9b1da9d", id: "889a323e-d104-4b5d-b276-dad5a9b1da9d"
} }
assert :ok = BookEditionsRegistrySync.create_update(attrs, server.id) assert :ok = BookEditionsRegistrySync.create_update(attrs, server.id)
@ -31,7 +31,7 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.BookEditionsRegistrySyncT
{:ok, same_alias} = Metadata.create_book_editions_registry(actor: user) {:ok, same_alias} = Metadata.create_book_editions_registry(actor: user)
attrs = %{ attrs = %{
id: same_alias.id, id: same_alias.id
} }
assert :ok = BookEditionsRegistrySync.create_update(attrs, server.id) assert :ok = BookEditionsRegistrySync.create_update(attrs, server.id)

@ -64,7 +64,7 @@ defmodule DecentralisedBookIndex.Generators do
authors = authors =
opts[:authors] || opts[:authors] ||
for id <- 1..2 do for id <- 1..2 do
generate(author(actor: actor)) #.id generate(author(actor: actor))
end end
count = min(opts[:count], length(authors)) count = min(opts[:count], length(authors))
@ -140,7 +140,7 @@ defmodule DecentralisedBookIndex.Generators do
Metadata.Publisher, Metadata.Publisher,
:create, :create,
defaults: [ defaults: [
name: sequence(:name, &"Publisher #{&1}"), name: sequence(:name, &"Publisher #{&1}")
], ],
overrides: opts, overrides: opts,
actor: actor actor: actor
@ -194,12 +194,12 @@ defmodule DecentralisedBookIndex.Generators do
description: sequence(:description, &"Description #{&1}"), description: sequence(:description, &"Description #{&1}"),
language: sequence(:language, &"Language #{&1}"), language: sequence(:language, &"Language #{&1}"),
format: sequence(:format, &"Format #{&1}"), format: sequence(:format, &"Format #{&1}"),
page_count: sequence(:page_count, &(abs(&1)+1)), page_count: sequence(:page_count, &(abs(&1) + 1)),
published: sequence(:published, &Date.add(~D[2025-03-04], &1)), published: sequence(:published, &Date.add(~D[2025-03-04], &1)),
bids: bids, bids: bids,
author_roles: author_roles, author_roles: author_roles,
publisher_id: publisher_id, publisher_id: publisher_id,
book_editions_registry_id: book_editions_registry_id, book_editions_registry_id: book_editions_registry_id
], ],
overrides: opts, overrides: opts,
actor: actor actor: actor

Loading…
Cancel
Save