diff --git a/lib/radiator/outline.ex b/lib/radiator/outline.ex index 413e9554..a0ef53c0 100644 --- a/lib/radiator/outline.ex +++ b/lib/radiator/outline.ex @@ -21,6 +21,7 @@ defmodule Radiator.Outline do alias Radiator.Outline.Node alias Radiator.Outline.NodeRepoResult alias Radiator.Outline.NodeRepository + alias Radiator.Outline.Validations, as: NodeValidator alias Radiator.Repo require Logger @@ -138,7 +139,12 @@ defmodule Radiator.Outline do node -> parent_node = get_parent_node(node) - case validate_consistency_for_move(node, new_prev_id, new_parent_id, parent_node) do + case NodeValidator.validate_consistency_for_move( + node, + new_prev_id, + new_parent_id, + parent_node + ) do {:error, error} -> {:error, error} @@ -149,39 +155,6 @@ defmodule Radiator.Outline do end end - defp validate_consistency_for_move( - %{prev_id: new_prev_id, parent_id: new_parent_id}, - new_prev_id, - new_parent_id, - _parent_node - ) do - {:error, :noop} - end - - # when prev is nil, every parent is allowed - defp validate_consistency_for_move( - node, - nil, - _new_parent_id, - _parent_node - ) do - {:ok, node} - end - - # when prev is not nil, parent and prev must be consistent - defp validate_consistency_for_move( - node, - new_prev_id, - new_parent_id, - _parent_node - ) do - if NodeRepository.get_node(new_prev_id).parent_id == new_parent_id do - {:ok, node} - else - {:error, :parent_and_prev_not_consistent} - end - end - # low level function to move a node defp do_move_node(node, new_prev_id, new_parent_id, prev_node, parent_node) do node_repo_result = %NodeRepoResult{node: node} diff --git a/lib/radiator/outline/validations.ex b/lib/radiator/outline/validations.ex new file mode 100644 index 00000000..c4fc0166 --- /dev/null +++ b/lib/radiator/outline/validations.ex @@ -0,0 +1,49 @@ +defmodule Radiator.Outline.Validations do + @moduledoc """ + Collection of consistency validations for the outline tree. + """ + + alias Radiator.Outline.NodeRepository + + def validate_consistency_for_move( + %{prev_id: new_prev_id, parent_id: new_parent_id}, + new_prev_id, + new_parent_id, + _parent_node + ) do + {:error, :noop} + end + + # when prev is nil, every parent is allowed + def validate_consistency_for_move( + node, + nil, + _new_parent_id, + _parent_node + ) do + {:ok, node} + end + + # when prev is not nil, parent and prev must be consistent + def validate_consistency_for_move( + node, + new_prev_id, + new_parent_id, + _parent_node + ) do + if NodeRepository.get_node(new_prev_id).parent_id == new_parent_id do + {:ok, node} + else + {:error, :parent_and_prev_not_consistent} + end + end + + def validate_tree(episode_id) do + tree_nodes = get_node_tree + all_nodes_by_episode = NodeRepository.list_nodes_by_episode(episode_id) + # tree_nodes + # every level has 1 node with prev_id nil + # all other nodes in level have prev_id set and are connected to the previous node + Enum.size(tree_nodes) == Enum.size(all_nodes_by_episode) + end +end