From 54dec4c54829a9f03a49f3ead6b62e8d436d71a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Wo=CC=88ginger?= Date: Wed, 4 Sep 2024 17:48:19 +0200 Subject: [PATCH] parent id can be omitted in case prev id is set --- lib/radiator/outline.ex | 29 ++++++++++++++++++++++++----- test/radiator/outline_test.exs | 22 ++++++++++++++++++++-- test/support/data_case.ex | 1 + 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/lib/radiator/outline.ex b/lib/radiator/outline.ex index a0ef53c0..ee478e38 100644 --- a/lib/radiator/outline.ex +++ b/lib/radiator/outline.ex @@ -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 -> @@ -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. diff --git a/test/radiator/outline_test.exs b/test/radiator/outline_test.exs index 3602d132..548bf6b6 100644 --- a/test/radiator/outline_test.exs +++ b/test/radiator/outline_test.exs @@ -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 @@ -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, @@ -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 } diff --git a/test/support/data_case.ex b/test/support/data_case.ex index a7e9ee3d..a24c1d57 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -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,