Skip to content

Commit

Permalink
handle move_node event
Browse files Browse the repository at this point in the history
  • Loading branch information
electronicbites committed May 26, 2024
1 parent 03c475a commit 1f07873
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/radiator/outline/command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ defmodule Radiator.Outline.Command do
event_id: event_id,
user_id: user_id,
node_id: node_id,
parent_node_id: parent_node_id,
prev_node_id: prev_node_id
parent_id: parent_node_id,
prev_id: prev_node_id
}
end
end
6 changes: 3 additions & 3 deletions lib/radiator/outline/command/move_node_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ defmodule Radiator.Outline.Command.MoveNodeCommand do
event_id: binary(),
user_id: binary(),
node_id: binary(),
parent_node_id: binary() | nil,
prev_node_id: binary() | nil
parent_id: binary() | nil,
prev_id: binary() | nil
}

defstruct [:event_id, :user_id, :node_id, :parent_node_id, :prev_node_id]
defstruct [:event_id, :user_id, :node_id, :parent_id, :prev_id]
end
42 changes: 34 additions & 8 deletions lib/radiator/outline/event_consumer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ defmodule Radiator.Outline.EventConsumer do
}

alias Radiator.Outline.Dispatch
alias Radiator.Outline.Event.{NodeContentChangedEvent, NodeDeletedEvent, NodeInsertedEvent}

alias Radiator.Outline.Event.{
NodeContentChangedEvent,
NodeDeletedEvent,
NodeInsertedEvent,
NodeMovedEvent
}

alias Radiator.Outline.NodeRepository

require Logger
Expand Down Expand Up @@ -50,14 +57,14 @@ defmodule Radiator.Outline.EventConsumer do

defp process_command(
%MoveNodeCommand{
node_id: _node_id,
parent_node_id: _parent_node_id,
prev_node_id: _prev_node_id
} = _command
node_id: node_id,
parent_id: parent_id,
prev_id: prev_id
} = command
) do
# node_id
# |> Outline.update_node_content(content)
# |> handle_change_node_content_result(command)
node_id
|> Outline.move_node(prev_id, parent_id)
|> handle_move_node_result(command)
end

defp process_command(%DeleteNodeCommand{node_id: node_id} = command) do
Expand Down Expand Up @@ -86,6 +93,25 @@ defmodule Radiator.Outline.EventConsumer do
:error
end

def handle_move_node_result({:ok, node}, %MoveNodeCommand{} = command) do
%NodeMovedEvent{
node_id: node.uuid,
parent_id: command.parent_id,
prev_id: command.prev_id,
user_id: command.user_id,
event_id: command.event_id
}
|> EventStore.persist_event()
|> Dispatch.broadcast()

{:ok, node}
end

def handle_move_node_result({:error, changeset}, _command) do
Logger.error("Move node failed. #{inspect(changeset)}")
:error
end

def handle_change_node_content_result({:ok, node}, %ChangeNodeContentCommand{} = command) do
%NodeContentChangedEvent{
node_id: node.uuid,
Expand Down
11 changes: 7 additions & 4 deletions test/radiator_web/live/episode_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,21 @@ defmodule RadiatorWeb.EpisodeLiveTest do
})

{:ok, live, _html} = live(conn, ~p"/admin/podcast/#{show.id}")
{:ok, _other_live, _html} = live(conn, ~p"/admin/podcast/#{show.id}")
{:ok, other_live, _html} = live(conn, ~p"/admin/podcast/#{show.id}")

params = node2 |> Map.merge(%{parent_id: uuid1, prev_id: nil}) |> Map.from_struct()

assert live |> render_hook(:move_node, params)

keep_liveview_alive()

### assert %Node{parent_id: ^uuid1} = NodeRepository.get_node!(uuid2)
assert %Node{parent_id: ^uuid1} = NodeRepository.get_node!(uuid2)

assert_push_event(live, "clean", %{node: %{uuid: ^uuid2}})

### assert_push_event(live, "clean", %{node: %{uuid: ^uuid2}})
### assert_push_event(other_live, "move", %{node: %{uuid: ^uuid2, parent_id: ^uuid1, prev_id: nil}})
assert_push_event(other_live, "move", %{
node: %{uuid: ^uuid2, parent_id: ^uuid1, prev_id: nil}
})
end

test "delete node", %{conn: conn, show: show, node: %{uuid: uuid}} do
Expand Down

0 comments on commit 1f07873

Please sign in to comment.