Skip to content

Commit

Permalink
proceed with validations
Browse files Browse the repository at this point in the history
  • Loading branch information
electronicbites committed Aug 24, 2024
1 parent a2a2c51 commit 0923941
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/radiator/outline/validations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Radiator.Outline.Validations do
Collection of consistency validations for the outline tree.
"""
alias Radiator.Outline
alias Radiator.Outline.Node
alias Radiator.Outline.NodeRepository

def validate_consistency_for_move(
Expand Down Expand Up @@ -44,9 +45,25 @@ defmodule Radiator.Outline.Validations do
# 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
if Enum.count(tree_nodes) == NodeRepository.count_nodes_by_episode(episode_id) do
# iterate through the levels of the tree
tree_nodes
|> Enum.group_by(& &1.level)
|> Enum.map(fn {_level, nodes} ->
# get the node with prev_id nil
first_node = Enum.find(nodes, & &1.prev_id == nil)
# get the rest of the nodes
rest_nodes = Enum.reject(nodes, & &1.prev_id == nil)
# iterate through the rest of the nodes
Enum.reduce(rest_nodes, first_node, fn node, prev_node ->
is_node_prev_node_of(prev_node, node)
end)
end)
{:ok}
else
{:error, :node_count_not_consistent}
end
end

def is_node_prev_node_of(%Radiator.Outline.Node{uuid: id}, %Radiator.Outline.Node{prev_id: id} = node), do: node
def is_node_prev_node_of(_, _), do: {:error, :prev_id_not_consistent}
end
5 changes: 5 additions & 0 deletions test/radiator/outline/validations_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule Radiator.Outline.ValidationsTest do
@moduledoc false

use Radiator.DataCase
end

0 comments on commit 0923941

Please sign in to comment.