Skip to content

Commit

Permalink
first implementation of a simple validation after every change
Browse files Browse the repository at this point in the history
error log during running the tests
  • Loading branch information
electronicbites committed Aug 20, 2024
1 parent f91865e commit 6b9a64f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
13 changes: 8 additions & 5 deletions lib/radiator/outline/validations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
38 changes: 21 additions & 17 deletions lib/radiator/outline/workers.ex
Original file line number Diff line number Diff line change
@@ -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.Dispatch
alias Radiator.Outline.Event.{NodeContentChangedEvent, NodeInsertedEvent, NodeMovedEvent}
alias Radiator.Outline.NodeRepository

def start_link(_) do
GenServer.start_link(__MODULE__, :ok, [])
Expand All @@ -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
2 changes: 0 additions & 2 deletions lib/radiator_web/live/episode_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ defmodule RadiatorWeb.EpisodeLive.Index do
}

alias Radiator.Outline.NodeRepository
# alias Radiator.EventStore
alias Radiator.Podcast
alias Radiator.Podcast.Episode

alias RadiatorWeb.OutlineComponents

@impl true
Expand Down

0 comments on commit 6b9a64f

Please sign in to comment.