From 100d8ba39a7d872e0dfdca02290c0cbeda0359cd Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sun, 16 Mar 2025 08:42:25 +0200 Subject: [PATCH] Add the attribute for a cover image url for Book. --- lib/decentralised_book_index/metadata.ex | 1 + lib/decentralised_book_index/metadata/book.ex | 9 + ...123_update_book_to_add_cover_image_url.exs | 21 +++ .../repo/books/20250316063123.json | 177 ++++++++++++++++++ .../metadata/book_test.exs | 19 +- 5 files changed, 223 insertions(+), 4 deletions(-) create mode 100644 priv/repo/migrations/20250316063123_update_book_to_add_cover_image_url.exs create mode 100644 priv/resource_snapshots/repo/books/20250316063123.json diff --git a/lib/decentralised_book_index/metadata.ex b/lib/decentralised_book_index/metadata.ex index 20931af..358f249 100644 --- a/lib/decentralised_book_index/metadata.ex +++ b/lib/decentralised_book_index/metadata.ex @@ -39,6 +39,7 @@ defmodule DecentralisedBookIndex.Metadata do define :get_book_alternative_editions, args: [:book], action: :get_alternative_editions define :get_author_books, args: [:author], action: :get_author_books define :update_book, action: :update + define :assign_book_cover_image, args: [:cover_image_url], action: :assign_cover_image define :destroy_book, action: :destroy end diff --git a/lib/decentralised_book_index/metadata/book.ex b/lib/decentralised_book_index/metadata/book.ex index e7b6f03..1528450 100644 --- a/lib/decentralised_book_index/metadata/book.ex +++ b/lib/decentralised_book_index/metadata/book.ex @@ -100,6 +100,10 @@ defmodule DecentralisedBookIndex.Metadata.Book do |> Ash.Query.filter(expr(exists(author_roles, author_id in ^author_ids))) end end + + update :assign_cover_image do + accept [:cover_image_url] + end end attributes do @@ -135,6 +139,11 @@ defmodule DecentralisedBookIndex.Metadata.Book do public? true end + attribute :cover_image_url, :string do + allow_nil? true + public? true + end + timestamps() end diff --git a/priv/repo/migrations/20250316063123_update_book_to_add_cover_image_url.exs b/priv/repo/migrations/20250316063123_update_book_to_add_cover_image_url.exs new file mode 100644 index 0000000..17e2425 --- /dev/null +++ b/priv/repo/migrations/20250316063123_update_book_to_add_cover_image_url.exs @@ -0,0 +1,21 @@ +defmodule DecentralisedBookIndex.Repo.Migrations.UpdateBookToAddCoverImageUrl do + @moduledoc """ + Updates resources based on their most recent snapshots. + + This file was autogenerated with `mix ash_postgres.generate_migrations` + """ + + use Ecto.Migration + + def up do + alter table(:books) do + add :cover_image_url, :text + end + end + + def down do + alter table(:books) do + remove :cover_image_url + end + end +end diff --git a/priv/resource_snapshots/repo/books/20250316063123.json b/priv/resource_snapshots/repo/books/20250316063123.json new file mode 100644 index 0000000..e6ce7ce --- /dev/null +++ b/priv/resource_snapshots/repo/books/20250316063123.json @@ -0,0 +1,177 @@ +{ + "attributes": [ + { + "allow_nil?": false, + "default": "fragment(\"gen_random_uuid()\")", + "generated?": false, + "primary_key?": true, + "references": null, + "size": null, + "source": "id", + "type": "uuid" + }, + { + "allow_nil?": false, + "default": "nil", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "title", + "type": "text" + }, + { + "allow_nil?": false, + "default": "nil", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "description", + "type": "text" + }, + { + "allow_nil?": false, + "default": "nil", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "published", + "type": "date" + }, + { + "allow_nil?": false, + "default": "nil", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "language", + "type": "text" + }, + { + "allow_nil?": false, + "default": "nil", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "format", + "type": "text" + }, + { + "allow_nil?": false, + "default": "nil", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "page_count", + "type": "bigint" + }, + { + "allow_nil?": true, + "default": "nil", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "cover_image_url", + "type": "text" + }, + { + "allow_nil?": false, + "default": "fragment(\"(now() AT TIME ZONE 'utc')\")", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "inserted_at", + "type": "utc_datetime_usec" + }, + { + "allow_nil?": false, + "default": "fragment(\"(now() AT TIME ZONE 'utc')\")", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "updated_at", + "type": "utc_datetime_usec" + }, + { + "allow_nil?": true, + "default": "nil", + "generated?": false, + "primary_key?": false, + "references": { + "deferrable": false, + "destination_attribute": "id", + "destination_attribute_default": null, + "destination_attribute_generated": null, + "index?": false, + "match_type": null, + "match_with": null, + "multitenancy": { + "attribute": null, + "global": null, + "strategy": null + }, + "name": "books_book_editions_registry_id_fkey", + "on_delete": null, + "on_update": null, + "primary_key?": true, + "schema": "public", + "table": "book_editions_registries" + }, + "size": null, + "source": "book_editions_registry_id", + "type": "uuid" + }, + { + "allow_nil?": true, + "default": "nil", + "generated?": false, + "primary_key?": false, + "references": { + "deferrable": false, + "destination_attribute": "id", + "destination_attribute_default": null, + "destination_attribute_generated": null, + "index?": false, + "match_type": null, + "match_with": null, + "multitenancy": { + "attribute": null, + "global": null, + "strategy": null + }, + "name": "books_publisher_id_fkey", + "on_delete": null, + "on_update": null, + "primary_key?": true, + "schema": "public", + "table": "publishers" + }, + "size": null, + "source": "publisher_id", + "type": "uuid" + } + ], + "base_filter": null, + "check_constraints": [], + "custom_indexes": [], + "custom_statements": [], + "has_create_action": true, + "hash": "6CC39E25B6B35E3FB69403004A0FB96AD52450E21FC6D6725F349F7B3705B3B7", + "identities": [], + "multitenancy": { + "attribute": null, + "global": null, + "strategy": null + }, + "repo": "Elixir.DecentralisedBookIndex.Repo", + "schema": null, + "table": "books" +} \ No newline at end of file diff --git a/test/decentralised_book_index/metadata/book_test.exs b/test/decentralised_book_index/metadata/book_test.exs index a823260..a3cab21 100644 --- a/test/decentralised_book_index/metadata/book_test.exs +++ b/test/decentralised_book_index/metadata/book_test.exs @@ -102,21 +102,21 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do {:ok, book} = Metadata.create_book("Book", "An description", "English", "Paperback", 256, ~D[2025-03-04], bids, author_roles, publisher.id) - author_roles = [ + author_roles2 = [ %{order: 1, author_id: alias1.id, role: ""} ] bids2 = bids() publisher2 = generate(publisher()) - {:ok, book2} = Metadata.create_book("Book2", "An description", "English", "Paperback", 256, ~D[2025-03-04], bids, author_roles, publisher2.id) + {:ok, book2} = Metadata.create_book("Book2", "An description", "English", "Paperback", 256, ~D[2025-03-04], bids2, author_roles2, publisher2.id) - author_roles = [ + author_roles3 = [ %{order: 1, author_id: alias2.id, role: ""} ] bids3 = bids() publisher3 = generate(publisher()) - {:ok, book3} = Metadata.create_book("Book3", "An description", "English", "Paperback", 256, ~D[2025-03-04], bids, author_roles, publisher3.id) + {:ok, book3} = Metadata.create_book("Book3", "An description", "English", "Paperback", 256, ~D[2025-03-04], bids3, author_roles3, publisher3.id) assert {:ok, books} = Metadata.get_author_books(author) @@ -138,4 +138,15 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do assert expected_book.id == book.id end end + + describe "book's cover image" do + test "update cover image" do + cover_image_url = "/images/book.avif" + + book = generate(book()) + + assert {:ok, book} = Metadata.assign_book_cover_image(book, cover_image_url) + assert book.cover_image_url == cover_image_url + end + end end