From 7d0a098a47f4a35975fcb3afe8f723d6546cccc4 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sun, 16 Mar 2025 08:53:40 +0200 Subject: [PATCH] Add the attribute for a avatar image url for Author. --- lib/decentralised_book_index/metadata.ex | 1 + .../metadata/author.ex | 9 ++ ...064910_update_author_to_add_avatar_url.exs | 21 ++++ .../repo/authors/20250316064910.json | 108 ++++++++++++++++++ .../metadata/author_roles_test.exs | 12 ++ 5 files changed, 151 insertions(+) create mode 100644 priv/repo/migrations/20250316064910_update_author_to_add_avatar_url.exs create mode 100644 priv/resource_snapshots/repo/authors/20250316064910.json diff --git a/lib/decentralised_book_index/metadata.ex b/lib/decentralised_book_index/metadata.ex index 358f249..825f59a 100644 --- a/lib/decentralised_book_index/metadata.ex +++ b/lib/decentralised_book_index/metadata.ex @@ -58,6 +58,7 @@ defmodule DecentralisedBookIndex.Metadata do define :get_author_alternative_names, args: [:author], action: :get_alternative_names define :search_author, action: :search, args: [:name] define :update_author, action: :update + define :assign_author_avatar_image, args: [:avatar_url], action: :assign_avatar_image define :destroy_author, action: :destroy end diff --git a/lib/decentralised_book_index/metadata/author.ex b/lib/decentralised_book_index/metadata/author.ex index 8b3a096..5f520e4 100644 --- a/lib/decentralised_book_index/metadata/author.ex +++ b/lib/decentralised_book_index/metadata/author.ex @@ -108,6 +108,10 @@ defmodule DecentralisedBookIndex.Metadata.Author do pagination offset?: true, default_limit: 10 end + + update :assign_avatar_image do + accept [:avatar_url] + end end attributes do @@ -123,6 +127,11 @@ defmodule DecentralisedBookIndex.Metadata.Author do public? true end + attribute :avatar_url, :string do + allow_nil? true + public? true + end + timestamps() end diff --git a/priv/repo/migrations/20250316064910_update_author_to_add_avatar_url.exs b/priv/repo/migrations/20250316064910_update_author_to_add_avatar_url.exs new file mode 100644 index 0000000..ca64842 --- /dev/null +++ b/priv/repo/migrations/20250316064910_update_author_to_add_avatar_url.exs @@ -0,0 +1,21 @@ +defmodule DecentralisedBookIndex.Repo.Migrations.UpdateAuthorToAddAvatarUrl 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(:authors) do + add :avatar_url, :text + end + end + + def down do + alter table(:authors) do + remove :avatar_url + end + end +end diff --git a/priv/resource_snapshots/repo/authors/20250316064910.json b/priv/resource_snapshots/repo/authors/20250316064910.json new file mode 100644 index 0000000..2f6ec47 --- /dev/null +++ b/priv/resource_snapshots/repo/authors/20250316064910.json @@ -0,0 +1,108 @@ +{ + "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": "name", + "type": "text" + }, + { + "allow_nil?": false, + "default": "nil", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "description", + "type": "text" + }, + { + "allow_nil?": true, + "default": "nil", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "avatar_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": "authors_author_alias_registry_id_fkey", + "on_delete": null, + "on_update": null, + "primary_key?": true, + "schema": "public", + "table": "author_alias_registries" + }, + "size": null, + "source": "author_alias_registry_id", + "type": "uuid" + } + ], + "base_filter": null, + "check_constraints": [], + "custom_indexes": [], + "custom_statements": [], + "has_create_action": true, + "hash": "0B4CCF358A5DFEC0FB98A664FC575AA12A5E851C61722E7AC620FCA5D97FFCF8", + "identities": [], + "multitenancy": { + "attribute": null, + "global": null, + "strategy": null + }, + "repo": "Elixir.DecentralisedBookIndex.Repo", + "schema": null, + "table": "authors" +} \ No newline at end of file diff --git a/test/decentralised_book_index/metadata/author_roles_test.exs b/test/decentralised_book_index/metadata/author_roles_test.exs index ac7d964..17ef673 100644 --- a/test/decentralised_book_index/metadata/author_roles_test.exs +++ b/test/decentralised_book_index/metadata/author_roles_test.exs @@ -24,4 +24,16 @@ defmodule DecentralisedBookIndex.Metadata.AuthorRolesTest do refute author_role.role end end + + describe "author's avatar image" do + test "update avatar image" do + avatar_url = "/images/avatar.avif" + + {:ok, author} = Metadata.create_author("Author", "An description") + + assert {:ok, author} = Metadata.assign_author_avatar_image(author, avatar_url) + + assert author.avatar_url == avatar_url + end + end end