Skip to content

Commit

Permalink
extract module: move consistency validations to a new module
Browse files Browse the repository at this point in the history
  • Loading branch information
electronicbites committed Aug 14, 2024
1 parent 66386f9 commit 982df7a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 34 deletions.
41 changes: 7 additions & 34 deletions lib/radiator/outline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}

Expand All @@ -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}
Expand Down
49 changes: 49 additions & 0 deletions lib/radiator/outline/validations.ex
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 982df7a

Please sign in to comment.