Add the attribute for a cover image url for Book.

dev
KKlochko 3 months ago
parent ddc5cc590f
commit 100d8ba39a

@ -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

@ -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

@ -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

@ -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"
}

@ -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

Loading…
Cancel
Save