diff --git a/lib/decentralised_book_index/metadata/author.ex b/lib/decentralised_book_index/metadata/author.ex index dc40677..0c7fa35 100644 --- a/lib/decentralised_book_index/metadata/author.ex +++ b/lib/decentralised_book_index/metadata/author.ex @@ -9,6 +9,9 @@ defmodule DecentralisedBookIndex.Metadata.Author do require Ash.Query alias DecentralisedBookIndex.Metadata + @brief_description_length 20 + @brief_description_max_length @brief_description_length + 3 + json_api do type "author" end @@ -241,4 +244,16 @@ defmodule DecentralisedBookIndex.Metadata.Author do has_many :author_role, Metadata.AuthorRole end + + calculations do + calculate :brief_description, + :string, + expr( + if(string_length(description) <= @brief_description_max_length) do + description + else + fragment("substr(?, 1, ?)", description, @brief_description_length) <> "..." + end + ) + end end diff --git a/lib/decentralised_book_index/metadata/book.ex b/lib/decentralised_book_index/metadata/book.ex index ad72b07..2ca0014 100644 --- a/lib/decentralised_book_index/metadata/book.ex +++ b/lib/decentralised_book_index/metadata/book.ex @@ -9,6 +9,9 @@ defmodule DecentralisedBookIndex.Metadata.Book do require Ash.Query alias DecentralisedBookIndex.Metadata + @brief_description_length 20 + @brief_description_max_length @brief_description_length + 3 + json_api do type "book" end @@ -366,4 +369,16 @@ defmodule DecentralisedBookIndex.Metadata.Book do public? true end end + + calculations do + calculate :brief_description, + :string, + expr( + if(string_length(description) <= @brief_description_max_length) do + description + else + fragment("substr(?, 1, ?)", description, @brief_description_length) <> "..." + end + ) + end end diff --git a/lib/decentralised_book_index_web/live/author_live/index.ex b/lib/decentralised_book_index_web/live/author_live/index.ex index 1cdddf2..34487d2 100644 --- a/lib/decentralised_book_index_web/live/author_live/index.ex +++ b/lib/decentralised_book_index_web/live/author_live/index.ex @@ -63,6 +63,7 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Index do page = Metadata.search_author!( search_query, + load: [:brief_description], query: [sort_input: sort_by], page: page_params ++ [count: true], actor: socket.assigns.current_user diff --git a/lib/decentralised_book_index_web/live/book_live/index.ex b/lib/decentralised_book_index_web/live/book_live/index.ex index fe8297f..d97dc23 100644 --- a/lib/decentralised_book_index_web/live/book_live/index.ex +++ b/lib/decentralised_book_index_web/live/book_live/index.ex @@ -189,6 +189,7 @@ defmodule DecentralisedBookIndexWeb.BookLive.Index do "title" -> Metadata.search_book!( search_query, + load: [:brief_description], query: [sort_input: sort_by], page: page_params ++ [count: true], actor: actor @@ -198,6 +199,7 @@ defmodule DecentralisedBookIndexWeb.BookLive.Index do Metadata.search_book_by_bid!( type, search_query, + load: [:brief_description], query: [sort_input: sort_by], page: page_params ++ [count: true], actor: actor diff --git a/test/decentralised_book_index/metadata/author_test.exs b/test/decentralised_book_index/metadata/author_test.exs index 55d7410..7ce58ad 100644 --- a/test/decentralised_book_index/metadata/author_test.exs +++ b/test/decentralised_book_index/metadata/author_test.exs @@ -128,4 +128,21 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do assert author.avatar_url == avatar_url end end + + describe "calculate a brief description" do + test "a short description is already enough" do + description = "Amazing!" + assert description == Ash.calculate!(Metadata.Author, :brief_description, + refs: %{description: description}) + end + + test "a long description is too much" do + description = "Etiam laoreet quam sed arcu. Donec at pede. Pellentesque tristique imperdiet tortor." + brief = "Etiam laoreet quam s..." + + assert String.length(brief) == 23 + assert brief == Ash.calculate!(Metadata.Author, :brief_description, + refs: %{description: description}) + end + end end diff --git a/test/decentralised_book_index/metadata/book_test.exs b/test/decentralised_book_index/metadata/book_test.exs index 6a57c1d..26037e0 100644 --- a/test/decentralised_book_index/metadata/book_test.exs +++ b/test/decentralised_book_index/metadata/book_test.exs @@ -273,4 +273,21 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do assert book.cover_image_url == cover_image_url end end + + describe "calculate a brief description" do + test "a short description is already enough" do + description = "Amazing!" + assert description == Ash.calculate!(Metadata.Book, :brief_description, + refs: %{description: description}) + end + + test "a long description is too much" do + description = "Etiam laoreet quam sed arcu. Donec at pede. Pellentesque tristique imperdiet tortor." + brief = "Etiam laoreet quam s..." + + assert String.length(brief) == 23 + assert brief == Ash.calculate!(Metadata.Book, :brief_description, + refs: %{description: description}) + end + end end