From 1f0787365d7c9251a42f47caf5f8b8f28af1831e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Wo=CC=88ginger?= Date: Sun, 26 May 2024 22:08:49 +0200 Subject: [PATCH] handle move_node event --- lib/radiator/outline/command.ex | 4 +- .../outline/command/move_node_command.ex | 6 +-- lib/radiator/outline/event_consumer.ex | 42 +++++++++++++++---- test/radiator_web/live/episode_live_test.exs | 11 +++-- 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/lib/radiator/outline/command.ex b/lib/radiator/outline/command.ex index c41dacaf..fa333c04 100644 --- a/lib/radiator/outline/command.ex +++ b/lib/radiator/outline/command.ex @@ -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 diff --git a/lib/radiator/outline/command/move_node_command.ex b/lib/radiator/outline/command/move_node_command.ex index 9da5da8d..92f12888 100644 --- a/lib/radiator/outline/command/move_node_command.ex +++ b/lib/radiator/outline/command/move_node_command.ex @@ -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 diff --git a/lib/radiator/outline/event_consumer.ex b/lib/radiator/outline/event_consumer.ex index 42ff058c..1d85a21b 100644 --- a/lib/radiator/outline/event_consumer.ex +++ b/lib/radiator/outline/event_consumer.ex @@ -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 @@ -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 @@ -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, diff --git a/test/radiator_web/live/episode_live_test.exs b/test/radiator_web/live/episode_live_test.exs index a04ffb2a..be33f391 100644 --- a/test/radiator_web/live/episode_live_test.exs +++ b/test/radiator_web/live/episode_live_test.exs @@ -123,7 +123,7 @@ 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() @@ -131,10 +131,13 @@ defmodule RadiatorWeb.EpisodeLiveTest do 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