Add the avatar_url attribute to Author's create action.

dev
KKlochko 3 months ago
parent 5b9646003b
commit 99678c12e5

@ -46,11 +46,11 @@ defmodule DecentralisedBookIndex.Metadata do
resource DecentralisedBookIndex.Metadata.Author do
define :create_author,
action: :create,
args: [:name, :description, {:optional, :author_alias_registry_id}]
args: [:name, :description, {:optional, :avatar_url}, {:optional, :author_alias_registry_id}]
define :add_author_to_related_alias_registry,
action: :add_author_to_related_alias_registry,
args: [:name, :description, :related_author_id]
args: [:name, :description, :related_author_id, {:optional, :avatar_url}]
define :list_authors, action: :read
define :get_author_by_id, args: [:id], action: :by_id

@ -22,7 +22,7 @@ defmodule DecentralisedBookIndex.Metadata.Author do
create :create do
primary? true
accept [:name, :description, :author_alias_registry_id]
accept [:name, :description, :avatar_url, :author_alias_registry_id]
change fn changeset, _ ->
registry_id = Ash.Changeset.get_attribute(changeset, :author_alias_registry_id)
@ -38,7 +38,7 @@ defmodule DecentralisedBookIndex.Metadata.Author do
end
create :add_author_to_related_alias_registry do
accept [:name, :description]
accept [:name, :description, :avatar_url]
argument :related_author_id, :uuid do
allow_nil? false
@ -63,7 +63,7 @@ defmodule DecentralisedBookIndex.Metadata.Author do
end
create :sync do
accept [:id, :name, :description, :dbi_server_id, :author_alias_registry_id]
accept [:id, :name, :description, :avatar_url, :dbi_server_id, :author_alias_registry_id]
change fn changeset, _ ->
server_id = Ash.Changeset.get_attribute(changeset, :dbi_server)

@ -9,12 +9,14 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformer do
id: get_in(json_body, ["data", "id"]),
name: get_in(json_body, ["data", "attributes", "name"]),
description: get_in(json_body, ["data", "attributes", "description"]),
avatar_url: get_in(json_body, ["data", "attributes", "avatar_url"]),
}
else
%{
id: get_in(json_body, ["id"]),
name: get_in(json_body, ["attributes", "name"]),
description: get_in(json_body, ["attributes", "description"]),
avatar_url: get_in(json_body, ["attributes", "avatar_url"]),
}
end

@ -11,7 +11,7 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do
test "a new author belongs to a registry if specified" do
assert {:ok, registry} = Metadata.create_author_alias_registry()
assert {:ok, author} = Metadata.create_author("Author", "An description", registry.id)
assert {:ok, author} = Metadata.create_author("Author", "An description", nil, registry.id)
assert author.author_alias_registry_id == registry.id
end
@ -70,4 +70,18 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do
assert alias2.id in ids
end
end
describe "authors and avatar_url" do
test "a new author has no avatar by default" do
assert {:ok, author} = Metadata.create_author("Author", "An description")
assert author.avatar_url == nil
end
test "a new author has a avatar" do
avatar_url = "/images/avatar.png"
assert {:ok, author} = Metadata.create_author("Author", "An description", avatar_url)
assert author.avatar_url == avatar_url
end
end
end

@ -8,7 +8,7 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformerTest do
test "a json contains correct author information" do
json_body = %{
"data" => %{
"attributes" => %{"description" => "Something", "name" => "Author"},
"attributes" => %{"description" => "Something", "name" => "Author", "avatar_url" => "/images/avatar.png"},
"id" => "889a323e-d104-4b5d-b276-dad5a9b1da9d",
"links" => %{},
"meta" => %{},
@ -26,13 +26,14 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformerTest do
assert %{
id: "889a323e-d104-4b5d-b276-dad5a9b1da9d",
name: "Author",
description: "Something"
description: "Something",
avatar_url: "/images/avatar.png"
} = author
end
test "a json doesn't contains author information \"data\" attribute" do
json_body = %{
"attributes" => %{"description" => "Something", "name" => "Author"},
"attributes" => %{"description" => "Something", "name" => "Author", "avatar_url" => "/images/avatar.png"},
"id" => "889a323e-d104-4b5d-b276-dad5a9b1da9d",
"links" => %{},
"meta" => %{},
@ -44,7 +45,8 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorTransformerTest do
assert %{
id: "889a323e-d104-4b5d-b276-dad5a9b1da9d",
name: "Author",
description: "Something"
description: "Something",
avatar_url: "/images/avatar.png"
} = author
end
end

@ -16,6 +16,7 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorSyncTest do
id: "889a323e-d104-4b5d-b276-dad5a9b1da9d",
name: "Author",
description: "Something",
avatar_url: "/images/avatar.png",
author_alias_registry_id: nil
}
@ -24,6 +25,7 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorSyncTest do
assert author.id == saved_author.id
assert author.name == saved_author.name
assert author.avatar_url == saved_author.avatar_url
assert nil != saved_author.author_alias_registry_id
assert server.id == saved_author.dbi_server_id
end
@ -37,6 +39,7 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorSyncTest do
id: author.id,
name: "Author2",
description: "Something2",
avatar_url: "/images/avatar.png",
author_alias_registry_id: nil
}
@ -46,6 +49,7 @@ defmodule DecentralisedBookIndex.Sync.DataTransformers.AuthorSyncTest do
assert author.id == saved_author.id
assert author_attrs.name == saved_author.name
assert author_attrs.description == saved_author.description
assert author_attrs.avatar_url == saved_author.avatar_url
end
end
end

Loading…
Cancel
Save