Skip to content

Commit

Permalink
more events with new added eventdata
Browse files Browse the repository at this point in the history
  • Loading branch information
electronicbites committed Jul 2, 2024
1 parent b4d3ad5 commit 6bd634d
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 10 deletions.
12 changes: 9 additions & 3 deletions lib/radiator/outline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,15 @@ defmodule Radiator.Outline do
if get_node_id(prev_node) != new_prev_id || get_node_id(parent_node) != new_parent_id do
do_move_node(node, new_prev_id, new_parent_id, prev_node, parent_node)
else
{:ok, node}
{:ok, %NodeRepoResult{node: node}}
end
end
end

# low level function to move a node
defp do_move_node(node, new_prev_id, new_parent_id, prev_node, parent_node) do
node_repo_result = %NodeRepoResult{node: node}

Repo.transaction(fn ->
old_next_node =
Node
Expand All @@ -119,9 +121,13 @@ defmodule Radiator.Outline do
move_node_if(old_next_node, get_node_id(parent_node), get_node_id(prev_node))

{:ok, _new_next_node} = move_node_if(new_next_node, new_parent_id, get_node_id(node))
end)

{:ok, node}
Map.merge(node_repo_result, %{
old_next_id: get_node_id(old_next_node),
old_prev_id: get_node_id(prev_node),
next_id: get_node_id(new_next_node)
})
end)
end

@doc """
Expand Down
21 changes: 17 additions & 4 deletions lib/radiator/outline/event/abstract_event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ alias Radiator.Outline.Event.{

defimpl Radiator.Outline.Event.AbstractEvent, for: NodeInsertedEvent do
def payload(event) do
event.node
%{
node_id: event.node.uuid,
content: event.node.content,
parent_id: event.node.parent_id,
prev_id: event.node.prev_id,
next_id: event.next_id
}
end

def event_type(_event), do: "NodeInsertedEvent"
Expand All @@ -23,7 +29,7 @@ defimpl Radiator.Outline.Event.AbstractEvent, for: NodeContentChangedEvent do
%{node_id: event.node_id, content: event.content}
end

def event_type(_event), do: "NodeInsertedEvent"
def event_type(_event), do: "NodeContentChangedEvent"
end

defimpl Radiator.Outline.Event.AbstractEvent, for: NodeDeletedEvent do
Expand All @@ -36,8 +42,15 @@ end

defimpl Radiator.Outline.Event.AbstractEvent, for: NodeMovedEvent do
def payload(event) do
%{node_id: event.node_id, parent_id: event.parent_id, prev_id: event.prev_id}
%{
node_id: event.node_id,
parent_id: event.parent_id,
prev_id: event.prev_id,
old_prev_id: event.old_prev_id,
old_next_id: event.old_next_id,
next_id: event.next_id
}
end

def event_type(_event), do: "NodeInsertedEvent"
def event_type(_event), do: "NodeMovedEvent"
end
8 changes: 6 additions & 2 deletions lib/radiator/outline/event_consumer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,17 @@ defmodule Radiator.Outline.EventConsumer do
:error
end

def handle_move_node_result({:ok, node}, %MoveNodeCommand{} = command) do
def handle_move_node_result(
{:ok, %NodeRepoResult{node: node} = result},
%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
event_id: command.event_id,
next_id: result.next_id
}
|> EventStore.persist_event()
|> Dispatch.broadcast()
Expand Down
32 changes: 32 additions & 0 deletions test/radiator/event_store_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ defmodule Radiator.EventStoreTest do
assert EventStore.list_event_data() |> length() == num_events + 1
end

test "node_inserted_event persists all given data" do
user = AccountsFixtures.user_fixture()
event = node_inserted_event_fixture(user_id: user.id)

EventStore.persist_event(event)
stored_event = EventStore.get_event_data!(event.event_id)
assert stored_event.data["next_id"] == event.next_id
assert stored_event.user_id == event.user_id
assert stored_event.event_type == "NodeInsertedEvent"

assert stored_event.data["content"] == event.node.content
assert stored_event.data["parent_id"] == event.node.parent_id
assert stored_event.data["prev_id"] == event.node.prev_id
assert stored_event.data["node_id"] == event.node.uuid
end

test "persists node_content_changed_event" do
user = AccountsFixtures.user_fixture()
event = node_content_changed_event_fixture(user_id: user.id)
Expand All @@ -43,6 +59,22 @@ defmodule Radiator.EventStoreTest do
EventStore.persist_event(event)
assert EventStore.list_event_data() |> length() == num_events + 1
end

test "node_moved_event persists all given data" do
user = AccountsFixtures.user_fixture()
event = node_moved_event_fixture(user_id: user.id)

EventStore.persist_event(event)
stored_event = EventStore.get_event_data!(event.event_id)
assert stored_event.data["node_id"] == event.node_id
assert stored_event.data["parent_id"] == event.parent_id
assert stored_event.data["prev_id"] == event.prev_id
assert stored_event.data["old_next_id"] == event.old_next_id
assert stored_event.data["next_id"] == event.next_id

assert stored_event.user_id == event.user_id
assert stored_event.event_type == "NodeMovedEvent"
end
end

describe "list_event_data/0" do
Expand Down
16 changes: 16 additions & 0 deletions test/radiator/outline_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Radiator.OutlineTest do
alias Radiator.Outline.NodeRepoResult
use Radiator.DataCase

alias Radiator.Outline
Expand Down Expand Up @@ -490,6 +491,21 @@ defmodule Radiator.OutlineTest do
assert nested_node_1.prev_id == nil
assert nested_node_2.prev_id == nested_node_1.uuid
end

# before 1 2 3 4 5
# after 2 3 4 5 1
test "move first node to the end of the list returns all needed infos", %{
node_1: node_1,
node_2: node_2,
node_5: node_5
} do
{:ok, %NodeRepoResult{} = node_result} =
Outline.move_node(node_1.uuid, node_5.uuid, node_2.parent_id)

assert node_result.node.uuid == node_1.uuid
assert node_result.old_prev_id == nil
assert node_result.old_next_id == node_2.uuid
end
end

describe "remove_node/1" do
Expand Down
12 changes: 11 additions & 1 deletion test/support/fixtures/event_store_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ defmodule Radiator.EventStoreFixtures do

def node_inserted_event_fixture(user_id: user_id) do
node = OutlineFixtures.node_fixture()
next = OutlineFixtures.node_fixture(episode_id: node.episode_id)

%NodeInsertedEvent{
node: node,
user_id: user_id,
event_id: Ecto.UUID.generate()
event_id: Ecto.UUID.generate(),
next_id: next.uuid
}
end

Expand Down Expand Up @@ -68,12 +70,20 @@ defmodule Radiator.EventStoreFixtures do
node = OutlineFixtures.node_fixture()
parent = OutlineFixtures.node_fixture(episode_id: node.episode_id)
prev = OutlineFixtures.node_fixture(episode_id: node.episode_id)
next = OutlineFixtures.node_fixture(episode_id: node.episode_id)
old_next = OutlineFixtures.node_fixture(episode_id: node.episode_id)

old_prev =
OutlineFixtures.node_fixture(episode_id: node.episode_id)

%NodeMovedEvent{
node_id: node.uuid,
user_id: user_id,
parent_id: parent.uuid,
prev_id: prev.uuid,
next_id: next.uuid,
old_next_id: old_next.uuid,
old_prev_id: old_prev.uuid,
event_id: Ecto.UUID.generate()
}
end
Expand Down

0 comments on commit 6bd634d

Please sign in to comment.