Skip to content

Commit

Permalink
more datamodel fixups, parent_id nil
Browse files Browse the repository at this point in the history
  • Loading branch information
electronicbites committed Dec 3, 2024
1 parent 7e431b2 commit d254b03
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
11 changes: 9 additions & 2 deletions lib/radiator/outline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
20 changes: 20 additions & 0 deletions lib/radiator/outline/node_repository.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions test/radiator/outline_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/support/data_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down

0 comments on commit d254b03

Please sign in to comment.