Skip to content

Commit

Permalink
Merge pull request #560 from podlove/optional-parent-node-for-insert-…
Browse files Browse the repository at this point in the history
…node

parent id can be omitted in case prev id is set
  • Loading branch information
electronicbites authored Sep 5, 2024
2 parents d483155 + 54dec4c commit 1c365ad
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
29 changes: 24 additions & 5 deletions lib/radiator/outline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ defmodule Radiator.Outline do
|> where_parent_node_equals(parent_node_id)
|> Repo.one()

with parent_node <- NodeRepository.get_node_if(parent_node_id),
prev_node <- NodeRepository.get_node_if(prev_node_id),
with prev_node <- NodeRepository.get_node_if(prev_node_id),
parent_node <- find_parent_node(prev_node, parent_node_id),
true <- parent_and_prev_consistent?(parent_node, prev_node),
{:ok, node} <- NodeRepository.create_node(attrs),
{:ok, _node_to_move} <- move_node_if(next_node, parent_node_id, node.uuid),
{:ok, node} <- move_node_if(node, parent_node_id, prev_node_id) do
true <- episode_valid?(episode_id, parent_node, prev_node),
{:ok, node} <- NodeRepository.create_node(set_parent_id_if(attrs, parent_node)),
{:ok, _node_to_move} <- move_node_if(next_node, parent_node_id, node.uuid) do
%NodeRepoResult{node: node, next_id: get_node_id(next_node)}
else
false ->
Expand All @@ -111,6 +111,25 @@ defmodule Radiator.Outline do
end)
end

defp episode_valid?(episode_id, %Node{episode_id: episode_id}, %Node{episode_id: episode_id}),
do: true

defp episode_valid?(episode_id, %Node{episode_id: episode_id}, nil), do: true
defp episode_valid?(episode_id, nil, %Node{episode_id: episode_id}), do: true
defp episode_valid?(_episode_id, nil, nil), do: true
defp episode_valid?(_episode_id, _parent_node, _prev_node), do: false

defp set_parent_id_if(attrs, nil), do: attrs
defp set_parent_id_if(attrs, %Node{uuid: uuid}), do: Map.put_new(attrs, "parent_id", uuid)

defp find_parent_node(%Node{parent_id: parent_id}, nil) do
NodeRepository.get_node_if(parent_id)
end

defp find_parent_node(_, parent_id) do
NodeRepository.get_node_if(parent_id)
end

@doc """
Moves a node to another parent.
Expand Down
22 changes: 20 additions & 2 deletions test/radiator/outline_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Radiator.OutlineTest do
alias Radiator.Outline
alias Radiator.Outline.Node
alias Radiator.Outline.NodeRepository
alias Radiator.Podcast
alias Radiator.PodcastFixtures

import Radiator.OutlineFixtures
Expand Down Expand Up @@ -186,6 +187,20 @@ defmodule Radiator.OutlineTest do
assert new_node.prev_id == nested_node_1.uuid
end

test "if prev_id has been given the parent_id can be omitted", %{
node_3: node_3,
nested_node_1: nested_node_1
} do
node_attrs = %{
"content" => "new node",
"episode_id" => node_3.episode_id,
"prev_id" => nested_node_1.uuid
}

{:ok, %{node: new_node}} = Outline.insert_node(node_attrs)
assert new_node.parent_id == node_3.uuid
end

test "all nodes in same level are correctly connected", %{
node_3: node_3,
nested_node_1: nested_node_1,
Expand Down Expand Up @@ -289,15 +304,18 @@ defmodule Radiator.OutlineTest do
end

test "in case of error no node gets inserted", %{
episode: episode,
parent_node: parent_node,
nested_node_1: nested_node_1
} do
count_nodes = NodeRepository.count_nodes_by_episode(parent_node.episode_id)

{:ok, another_episode} =
Podcast.create_episode(%{title: "current episode", show_id: episode.show_id, number: 23})

node_attrs = %{
"content" => "new node",
"episode_id" => parent_node.episode_id,
"parent_id" => parent_node.uuid,
"episode_id" => another_episode.id,
"prev_id" => nested_node_1.uuid
}

Expand Down
1 change: 1 addition & 0 deletions test/support/data_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ defmodule Radiator.DataCase do
assert nested_node_2.prev_id == nested_node_1.uuid

%{
episode: episode,
node_1: node_1,
node_2: node_2,
node_3: node_3,
Expand Down

0 comments on commit 1c365ad

Please sign in to comment.