From 4f827c57c792a3a32889c1ac47b974abcd013803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Wo=CC=88ginger?= Date: Thu, 15 Aug 2024 16:49:20 +0200 Subject: [PATCH] first implementation of a simple validation after every change error log during running the tests --- lib/radiator/outline/validations.ex | 13 ++++++---- lib/radiator/outline/workers.ex | 38 ++++++++++++++++------------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/lib/radiator/outline/validations.ex b/lib/radiator/outline/validations.ex index c4fc0166..2fa72d70 100644 --- a/lib/radiator/outline/validations.ex +++ b/lib/radiator/outline/validations.ex @@ -2,7 +2,7 @@ defmodule Radiator.Outline.Validations do @moduledoc """ Collection of consistency validations for the outline tree. """ - + alias Radiator.Outline alias Radiator.Outline.NodeRepository def validate_consistency_for_move( @@ -38,12 +38,15 @@ defmodule Radiator.Outline.Validations do end end - def validate_tree(episode_id) do - tree_nodes = get_node_tree - all_nodes_by_episode = NodeRepository.list_nodes_by_episode(episode_id) + def validate_tree_for_episode(episode_id) do + {:ok, tree_nodes} = Outline.get_node_tree(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) + if Enum.count(tree_nodes) == NodeRepository.count_nodes_by_episode(episode_id) do + {:ok} + else + {:error, :node_count_not_consistent} + end end end diff --git a/lib/radiator/outline/workers.ex b/lib/radiator/outline/workers.ex index 118603ad..170e8b85 100644 --- a/lib/radiator/outline/workers.ex +++ b/lib/radiator/outline/workers.ex @@ -1,11 +1,15 @@ defmodule Radiator.Outline.Workers do @moduledoc """ Very WIP. Genserver (OR NOT) that listens to change events and starts workers - + Name is a placeholder. It should be more an orchestrator than a worker. First problem, when and how to start. When the first person works on an episode.... """ use GenServer + require Logger + + alias Radiator.Outline.Event.{NodeContentChangedEvent, NodeInsertedEvent, NodeMovedEvent} alias Radiator.Outline.Dispatch + alias Radiator.Outline.NodeRepository def start_link(_) do GenServer.start_link(__MODULE__, :ok, []) @@ -16,31 +20,31 @@ defmodule Radiator.Outline.Workers do {:ok, []} end - def handle_info(%Radiator.Outline.Event.NodeContentChangedEvent{} = _event, state) do + def handle_info(%NodeContentChangedEvent{} = _event, state) do {:noreply, state} end - def handle_info(%Radiator.Outline.Event.NodeInsertedEvent{} = _event, state) do + def handle_info(%NodeInsertedEvent{node: %{episode_id: episode_id}} = _event, state) do + validate_tree(episode_id) {:noreply, state} end - def handle_info(%Radiator.Outline.Event.NodeMovedEvent{} = _event, state) do + def handle_info(%NodeMovedEvent{node_id: node_id} = _event, state) do + node = NodeRepository.get_node(node_id) + validate_tree(node.episode_id) + {:noreply, state} end - # def handle_cast({:create_node, _params}, state) do - # {:noreply, state} - # end + alias Radiator.Outline.Validations - # def handle_cast({:update_node_content, %{"uuid" => _uuid, "content" => _content}}, state) do - # {:noreply, state} - # end + defp validate_tree(episode_id) do + case Validations.validate_tree_for_episode(episode_id) do + {:ok} -> + :ok - # def handle_cast({:move_node, %{"uuid" => _uuid}}, state) do - # {:noreply, state} - # end - - # def handle_cast({:delete_node, %{"uuid" => _uuid}}, state) do - # {:noreply, state} - # end + {:error, message} -> + Logger.error("Tree validation failed for episode #{episode_id}: #{message}") + end + end end