Skip to content

Commit

Permalink
add commands and events for intend node actions
Browse files Browse the repository at this point in the history
  • Loading branch information
electronicbites committed Sep 9, 2024
1 parent 45ec445 commit faa5692
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/radiator/outline/command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Radiator.Outline.Command do
ChangeNodeContentCommand,
DeleteNodeCommand,
InsertNodeCommand,
IntendNodeCommand,
MoveNodeCommand
}

Expand All @@ -24,6 +25,14 @@ defmodule Radiator.Outline.Command do
}
end

def build("indent_node", node_id, user_id, event_id) do
%IntendNodeCommand{
event_id: event_id,
user_id: user_id,
node_id: node_id
}
end

def build("change_node_content", node_id, content, user_id, event_id) do
%ChangeNodeContentCommand{
event_id: event_id,
Expand Down
12 changes: 12 additions & 0 deletions lib/radiator/outline/command/indent_node_command.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule Radiator.Outline.Command.IntendNodeCommand do
@moduledoc """
Command to intend a node.
"""
@type t() :: %__MODULE__{
event_id: binary(),
user_id: binary(),
node_id: binary()
}

defstruct [:event_id, :user_id, :node_id]
end
21 changes: 21 additions & 0 deletions lib/radiator/outline/command_processor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Radiator.Outline.CommandProcessor do
ChangeNodeContentCommand,
DeleteNodeCommand,
InsertNodeCommand,
IntendNodeCommand,
MoveNodeCommand
}

Expand All @@ -20,6 +21,7 @@ defmodule Radiator.Outline.CommandProcessor do
NodeContentChangedEvent,
NodeDeletedEvent,
NodeInsertedEvent,
NodeIntendedEvent,
NodeMovedEvent
}

Expand Down Expand Up @@ -68,6 +70,25 @@ defmodule Radiator.Outline.CommandProcessor do
|> handle_move_node_result(command)
end

defp process_command(%IntendNodeCommand{node_id: node_id} = command) do
case Outline.indent_node(node_id) do
{:error, reason} ->
Logger.error("Could not intend node. Reason: #{reason}")

{:ok, node_result} ->
%NodeIntendedEvent{
node_id: node_id,
episode_id: node_result.node.episode_id,
uuid: command.event_id,
user_id: command.user_id,
children: node_result.children,
next_id: node_result.next_id
}
|> EventStore.persist_event()
|> Dispatch.broadcast()
end
end

defp process_command(%DeleteNodeCommand{node_id: node_id} = command) do
case NodeRepository.get_node(node_id) do
nil ->
Expand Down
6 changes: 6 additions & 0 deletions lib/radiator/outline/dispatch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ defmodule Radiator.Outline.Dispatch do
|> EventProducer.enqueue()
end

def indent_node(node_id, user_id, event_id) do
"indent_node"
|> Command.build(node_id, user_id, event_id)
|> EventProducer.enqueue()
end

def move_node(node_id, user_id, event_id,
parent_id: parent_id,
prev_id: prev_id
Expand Down
4 changes: 4 additions & 0 deletions lib/radiator/outline/event/node_indented_event.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule Radiator.Outline.Event.NodeIntendedEvent do
@moduledoc false
defstruct [:uuid, :node_id, :user_id, :children, :next_id, :episode_id]
end

0 comments on commit faa5692

Please sign in to comment.