diff --git a/lib/radiator/outline.ex b/lib/radiator/outline.ex index ca25a48c..794d9a45 100644 --- a/lib/radiator/outline.ex +++ b/lib/radiator/outline.ex @@ -78,9 +78,9 @@ defmodule Radiator.Outline do # if no previous node is given, the new node will be inserted as the first child of the parent node def insert_node(%{"show_id" => _show_id} = attrs) do Repo.transaction(fn -> - prev_id = attrs["prev_id"] - parent_id = attrs["parent_id"] episode_id = attrs["episode_id"] + prev_id = attrs["prev_id"] + parent_id = convert_parent_id_to_intern(attrs["parent_id"], episode_id) prev_node = NodeRepository.get_node_if(prev_id) parent_node = find_parent_node(prev_node, parent_id) @@ -108,6 +108,13 @@ defmodule Radiator.Outline do end) end + defp convert_parent_id_to_intern(nil, episode_id) do + {episode_root, _} = NodeRepository.get_virtual_nodes_for_episode(episode_id) + episode_root.uuid + end + + defp convert_parent_id_to_intern(parent_id, _episode_id), do: parent_id + def insert_node(%{"episode_id" => episode_id} = attrs) do %Episode{show_id: show_id} = Podcast.get_episode!(episode_id) diff --git a/lib/radiator/outline/node_repository.ex b/lib/radiator/outline/node_repository.ex index 372dc50a..306993bf 100644 --- a/lib/radiator/outline/node_repository.ex +++ b/lib/radiator/outline/node_repository.ex @@ -80,6 +80,9 @@ defmodule Radiator.Outline.NodeRepository do {episode_root, episode_inbox} end + @doc """ + TODO add documentation + """ def get_virtual_nodes_for_show(show_id) do [node_1, node_2] = Node @@ -94,6 +97,23 @@ defmodule Radiator.Outline.NodeRepository do end end + @doc """ + TODO add documentation + """ + def get_virtual_nodes_for_episode(episode_id) do + [node_1, node_2] = + Node + |> where([p], p.episode_id == ^episode_id) + |> where([p], p._type in [:episode_root, :episode_inbox]) + |> Repo.all() + + if node_1._type == :episode_root do + {node_1, node_2} + else + {node_2, node_1} + end + end + @doc """ Deletes a node from the repository. diff --git a/test/radiator/outline_test.exs b/test/radiator/outline_test.exs index f779068f..46873173 100644 --- a/test/radiator/outline_test.exs +++ b/test/radiator/outline_test.exs @@ -264,6 +264,23 @@ defmodule Radiator.OutlineTest do assert NodeRepository.get_node!(nested_node_2.uuid).prev_id == nested_node_1.uuid end + test "when parent node is nil it will be set to the episode root", %{ + parent_node: parent_node + } do + node_attrs = %{"parent_id" => nil, "episode_id" => parent_node.episode_id} + {:ok, %{node: new_node}} = Outline.insert_node(node_attrs) + # do not test result since it will be altered + # but fetching if from the repo with low level functions will return the true value + {episode_root, _} = NodeRepository.get_virtual_nodes_for_episode(parent_node.episode_id) + assert NodeRepository.get_node!(new_node.uuid).parent_id == episode_root.uuid + end + + test "when parent node is not given it will be set to the episode root", %{ + parent_node: parent_node + } do + + end + test "without a parent node the inserted node will be put at the top", %{ parent_node: parent_node } do diff --git a/test/support/data_case.ex b/test/support/data_case.ex index 136ba872..b3b1b409 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -128,13 +128,13 @@ defmodule Radiator.DataCase do {_show_root, _global_inbox} = NodeRepository.create_virtual_nodes_for_show(show.id) episode = PodcastFixtures.episode_fixture() - {_show_root, _global_inbox} = NodeRepository.create_virtual_nodes_for_episode(episode) + {episode_root, _eposide_inbox} = NodeRepository.create_virtual_nodes_for_episode(episode) parent_node = node_fixture( episode_id: episode.id, show_id: episode.show_id, - parent_id: nil, + parent_id: episode_root.uuid, prev_id: nil, content: "root of all evil" )