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 resource DecentralisedBookIndex.Metadata.Author do
define :create_author, define :create_author,
action: :create, 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, define :add_author_to_related_alias_registry,
action: :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 :list_authors, action: :read
define :get_author_by_id, args: [:id], action: :by_id define :get_author_by_id, args: [:id], action: :by_id

@ -22,7 +22,7 @@ defmodule DecentralisedBookIndex.Metadata.Author do
create :create do create :create do
primary? true primary? true
accept [:name, :description, :author_alias_registry_id] accept [:name, :description, :avatar_url, :author_alias_registry_id]
change fn changeset, _ -> change fn changeset, _ ->
registry_id = Ash.Changeset.get_attribute(changeset, :author_alias_registry_id) registry_id = Ash.Changeset.get_attribute(changeset, :author_alias_registry_id)
@ -38,7 +38,7 @@ defmodule DecentralisedBookIndex.Metadata.Author do
end end
create :add_author_to_related_alias_registry do create :add_author_to_related_alias_registry do
accept [:name, :description] accept [:name, :description, :avatar_url]
argument :related_author_id, :uuid do argument :related_author_id, :uuid do
allow_nil? false allow_nil? false
@ -63,7 +63,7 @@ defmodule DecentralisedBookIndex.Metadata.Author do
end end
create :sync do 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, _ -> change fn changeset, _ ->
server_id = Ash.Changeset.get_attribute(changeset, :dbi_server) 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"]), id: get_in(json_body, ["data", "id"]),
name: get_in(json_body, ["data", "attributes", "name"]), name: get_in(json_body, ["data", "attributes", "name"]),
description: get_in(json_body, ["data", "attributes", "description"]), description: get_in(json_body, ["data", "attributes", "description"]),
avatar_url: get_in(json_body, ["data", "attributes", "avatar_url"]),
} }
else else
%{ %{
id: get_in(json_body, ["id"]), id: get_in(json_body, ["id"]),
name: get_in(json_body, ["attributes", "name"]), name: get_in(json_body, ["attributes", "name"]),
description: get_in(json_body, ["attributes", "description"]), description: get_in(json_body, ["attributes", "description"]),
avatar_url: get_in(json_body, ["attributes", "avatar_url"]),
} }
end end

@ -11,7 +11,7 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do
test "a new author belongs to a registry if specified" do test "a new author belongs to a registry if specified" do
assert {:ok, registry} = Metadata.create_author_alias_registry() 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 assert author.author_alias_registry_id == registry.id
end end
@ -70,4 +70,18 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do
assert alias2.id in ids assert alias2.id in ids
end end
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 end

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

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

Loading…
Cancel
Save