Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
electronicbites committed Sep 5, 2024
1 parent a97f545 commit 1100910
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 29 deletions.
23 changes: 19 additions & 4 deletions lib/radiator/outline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,19 @@ defmodule Radiator.Outline do
iex> move_node(node_id, new_prev_id, new_parent_id)
{:ok, %Node{}}
"""
def move_node(node_id, [prev_id: node_id, parent_id: _new_parent_id]) do
def move_node(node_id, prev_id: node_id, parent_id: _new_parent_id) do
{:error, :self_link}
end

def move_node(node_id, [prev_id: _new_prev_id, parent_id: node_id]) do
def move_node(node_id, prev_id: _new_prev_id, parent_id: node_id) do
{:error, :circle_link}
end

def move_node(_node_id, [prev_id: other_id, parent_id: other_id]) when not is_nil(other_id) do
def move_node(_node_id, prev_id: other_id, parent_id: other_id) when not is_nil(other_id) do
{:error, :parent_and_prev_not_consistent}
end

def move_node(node_id, [prev_id: new_prev_id, parent_id: new_parent_id]) do
def move_node(node_id, prev_id: new_prev_id, parent_id: new_parent_id) do
case NodeRepository.get_node(node_id) do
nil ->
{:error, :not_found}
Expand All @@ -174,6 +174,16 @@ defmodule Radiator.Outline do
end
end

def move_node(node_id, prev_id: new_prev_id) do
parent_id =
new_prev_id
|> NodeRepository.get_node_if()
|> get_parent_node
|> get_node_id

move_node(node_id, prev_id: new_prev_id, parent_id: parent_id)
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}
Expand Down Expand Up @@ -286,6 +296,7 @@ defmodule Radiator.Outline do
%Node{uuid: 42}
"""
def get_prev_node(nil), do: nil
def get_prev_node(node) when is_nil(node.prev_id), do: nil

def get_prev_node(node) do
Expand All @@ -305,7 +316,11 @@ defmodule Radiator.Outline do
iex> get_parent_node(%Node{parent_id: 42})
%Node{uuid: 42}
iex> get_parent_node(nil)
nil
"""
def get_parent_node(nil), do: nil
def get_parent_node(node) when is_nil(node.parent_id), do: nil

def get_parent_node(node) do
Expand Down
2 changes: 1 addition & 1 deletion lib/radiator/outline/command_processor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ defmodule Radiator.Outline.CommandProcessor do
} = command
) do
node_id
|> Outline.move_node([prev_id: prev_id, parent_id: parent_id])
|> Outline.move_node(prev_id: prev_id, parent_id: parent_id)
|> handle_move_node_result(command)
end

Expand Down
5 changes: 4 additions & 1 deletion lib/radiator/outline/dispatch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ defmodule Radiator.Outline.Dispatch do
|> EventProducer.enqueue()
end

def move_node(node_id, user_id, event_id, [parent_node_id: parent_node_id, prev_node_id: prev_node_id]) do
def move_node(node_id, user_id, event_id,
parent_node_id: parent_node_id,
prev_node_id: prev_node_id
) do
"move_node"
|> Command.build(node_id, parent_node_id, prev_node_id, user_id, event_id)
|> EventProducer.enqueue()
Expand Down
4 changes: 2 additions & 2 deletions lib/radiator_web/live/outline_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ defmodule RadiatorWeb.OutlineComponent do
}

user_id = socket.assigns.user_id
Dispatch.move_node(uuid, user_id, generate_event_id(socket.id), [prev_node_id: prev_id])
Dispatch.move_node(uuid, user_id, generate_event_id(socket.id), prev_node_id: prev_id)

socket
|> stream_insert(:nodes, to_change_form(node, %{}))
Expand All @@ -232,7 +232,7 @@ defmodule RadiatorWeb.OutlineComponent do
}

user_id = socket.assigns.user_id
Dispatch.move_node(uuid, user_id, generate_event_id(socket.id), [parent_node_id: parent_id])
Dispatch.move_node(uuid, user_id, generate_event_id(socket.id), parent_node_id: parent_id)

socket
|> stream_insert(:nodes, to_change_form(node, %{}))
Expand Down
57 changes: 36 additions & 21 deletions test/radiator/outline_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ defmodule Radiator.OutlineTest do
node_1: node_1,
node_2: node_2
} do
{:ok, _} = Outline.move_node(node_2.uuid, [prev_id: nil, parent_id: node_2.parent_id])
{:ok, _} = Outline.move_node(node_2.uuid, prev_id: nil, parent_id: node_2.parent_id)

# reload nodes
node_1 = Repo.reload!(node_1)
Expand All @@ -351,7 +351,21 @@ defmodule Radiator.OutlineTest do
node_1: node_1,
node_2: node_2
} do
{:ok, _} = Outline.move_node(node_1.uuid, [prev_id: node_2.uuid, parent_id: node_1.parent_id])
{:ok, _} = Outline.move_node(node_1.uuid, prev_id: node_2.uuid, parent_id: node_1.parent_id)

# reload nodes
node_1 = Repo.reload!(node_1)
node_2 = Repo.reload!(node_2)

assert node_1.prev_id == node_2.uuid
assert node_2.prev_id == nil
end

test "if prev_id has been given the parent_id can be omitted", %{
node_1: node_1,
node_2: node_2
} do
{:ok, _} = Outline.move_node(node_1.uuid, prev_id: node_2.uuid)

# reload nodes
node_1 = Repo.reload!(node_1)
Expand All @@ -365,7 +379,8 @@ defmodule Radiator.OutlineTest do
node_1: node_1,
node_2: node_2
} do
{:error, :noop} = Outline.move_node(node_2.uuid, [prev_id: node_1.uuid, parent_id: node_2.parent_id])
{:error, :noop} =
Outline.move_node(node_2.uuid, prev_id: node_1.uuid, parent_id: node_2.parent_id)

# reload nodes
node_1 = Repo.reload!(node_1)
Expand All @@ -379,7 +394,7 @@ defmodule Radiator.OutlineTest do
node_1: node_1,
node_2: node_2
} do
{:error, :noop} = Outline.move_node(node_1.uuid, [prev_id: nil, parent_id: node_2.parent_id])
{:error, :noop} = Outline.move_node(node_1.uuid, prev_id: nil, parent_id: node_2.parent_id)

# reload nodes
node_1 = Repo.reload!(node_1)
Expand All @@ -393,7 +408,7 @@ defmodule Radiator.OutlineTest do
node_1: node_1,
node_2: node_2
} do
{:ok, _} = Outline.move_node(node_2.uuid, [prev_id: nil, parent_id: node_1.uuid])
{:ok, _} = Outline.move_node(node_2.uuid, prev_id: nil, parent_id: node_1.uuid)

# reload nodes
node_1 = Repo.reload!(node_1)
Expand All @@ -411,7 +426,7 @@ defmodule Radiator.OutlineTest do
node_2: node_2
} do
{:error, :parent_and_prev_not_consistent} =
Outline.move_node(node_2.uuid, [prev_id: node_1.uuid, parent_id: node_1.uuid])
Outline.move_node(node_2.uuid, prev_id: node_1.uuid, parent_id: node_1.uuid)
end
end

Expand All @@ -426,7 +441,7 @@ defmodule Radiator.OutlineTest do
node_4: node_4,
node_5: node_5
} do
{:ok, _} = Outline.move_node(node_4.uuid, [prev_id: node_2.uuid, parent_id: node_4.parent_id])
{:ok, _} = Outline.move_node(node_4.uuid, prev_id: node_2.uuid, parent_id: node_4.parent_id)

# reload nodes
node_5 = Repo.reload!(node_5)
Expand All @@ -448,7 +463,7 @@ defmodule Radiator.OutlineTest do
node_4: node_4,
node_5: node_5
} do
{:ok, _} = Outline.move_node(node_4.uuid, [prev_id: nil, parent_id: node_4.parent_id])
{:ok, _} = Outline.move_node(node_4.uuid, prev_id: nil, parent_id: node_4.parent_id)

# reload nodes
node_5 = Repo.reload!(node_5)
Expand All @@ -473,7 +488,7 @@ defmodule Radiator.OutlineTest do
node_4: node_4,
node_5: node_5
} do
{:ok, _} = Outline.move_node(node_2.uuid, [prev_id: node_5.uuid, parent_id: node_2.parent_id])
{:ok, _} = Outline.move_node(node_2.uuid, prev_id: node_5.uuid, parent_id: node_2.parent_id)

# reload nodes
node_5 = Repo.reload!(node_5)
Expand All @@ -497,7 +512,7 @@ defmodule Radiator.OutlineTest do
node_4: node_4,
node_5: node_5
} do
{:ok, _} = Outline.move_node(node_1.uuid, [prev_id: node_5.uuid, parent_id: node_2.parent_id])
{:ok, _} = Outline.move_node(node_1.uuid, prev_id: node_5.uuid, parent_id: node_2.parent_id)

# reload nodes
node_5 = Repo.reload!(node_5)
Expand All @@ -521,7 +536,7 @@ defmodule Radiator.OutlineTest do
node_4: node_4,
node_5: node_5
} do
{:ok, _} = Outline.move_node(node_5.uuid, [prev_id: nil, parent_id: node_2.parent_id])
{:ok, _} = Outline.move_node(node_5.uuid, prev_id: nil, parent_id: node_2.parent_id)

# reload nodes
node_5 = Repo.reload!(node_5)
Expand All @@ -543,7 +558,7 @@ defmodule Radiator.OutlineTest do
nested_node_1: nested_node_1,
nested_node_2: nested_node_2
} do
{:ok, _} = Outline.move_node(nested_node_1.uuid, [prev_id: nil, parent_id: node_2.uuid])
{:ok, _} = Outline.move_node(nested_node_1.uuid, prev_id: nil, parent_id: node_2.uuid)

# reload nodes
nested_node_1 = Repo.reload!(nested_node_1)
Expand All @@ -561,7 +576,7 @@ defmodule Radiator.OutlineTest do
nested_node_1: nested_node_1,
nested_node_2: nested_node_2
} do
{:ok, _} = Outline.move_node(node_3.uuid, [prev_id: nil, parent_id: nil])
{:ok, _} = Outline.move_node(node_3.uuid, prev_id: nil, parent_id: nil)

# reload nodes
parent_node = Repo.reload!(parent_node)
Expand All @@ -586,7 +601,7 @@ defmodule Radiator.OutlineTest do
node_5: node_5
} do
{:ok, %NodeRepoResult{} = node_result} =
Outline.move_node(node_1.uuid, [prev_id: node_5.uuid, parent_id: node_2.parent_id])
Outline.move_node(node_1.uuid, prev_id: node_5.uuid, parent_id: node_2.parent_id)

assert node_result.node.uuid == node_1.uuid
assert node_result.old_prev_id == nil
Expand All @@ -597,15 +612,15 @@ defmodule Radiator.OutlineTest do
node_1: node_1
} do
{:error, :self_link} =
Outline.move_node(node_1.uuid, [prev_id: node_1.uuid, parent_id: node_1.parent_id])
Outline.move_node(node_1.uuid, prev_id: node_1.uuid, parent_id: node_1.parent_id)
end

test "error when parent id is the same as node id", %{
node_1: node_1,
node_5: node_5
} do
{:error, :circle_link} =
Outline.move_node(node_1.uuid, [prev_id: node_5.uuid, parent_id: node_1.uuid])
Outline.move_node(node_1.uuid, prev_id: node_5.uuid, parent_id: node_1.uuid)
end

test "error when parent is a child of new node", %{
Expand All @@ -614,7 +629,7 @@ defmodule Radiator.OutlineTest do
nested_node_1: nested_node_1
} do
{:error, :parent_and_prev_not_consistent} =
Outline.move_node(node_1.uuid, [prev_id: node_5.uuid, parent_id: nested_node_1.uuid])
Outline.move_node(node_1.uuid, prev_id: node_5.uuid, parent_id: nested_node_1.uuid)
end

test "error when parent is on same level of new node", %{
Expand All @@ -623,7 +638,7 @@ defmodule Radiator.OutlineTest do
node_3: node_3
} do
{:error, :parent_and_prev_not_consistent} =
Outline.move_node(node_1.uuid, [prev_id: node_5.uuid, parent_id: node_3.uuid])
Outline.move_node(node_1.uuid, prev_id: node_5.uuid, parent_id: node_3.uuid)
end

test "error when new prev is not a direct child of new parent", %{
Expand All @@ -632,15 +647,15 @@ defmodule Radiator.OutlineTest do
nested_node_1: nested_node_1
} do
{:error, :parent_and_prev_not_consistent} =
Outline.move_node(node_1.uuid, [prev_id: nested_node_1.uuid, parent_id: parent_node.uuid])
Outline.move_node(node_1.uuid, prev_id: nested_node_1.uuid, parent_id: parent_node.uuid)
end

test "error when new new parent is nil but prev is not on level 1", %{
node_1: node_1,
nested_node_1: nested_node_1
} do
{:error, :parent_and_prev_not_consistent} =
Outline.move_node(node_1.uuid, [prev_id: nested_node_1.uuid, parent_id: nil])
Outline.move_node(node_1.uuid, prev_id: nested_node_1.uuid, parent_id: nil)
end
end

Expand Down Expand Up @@ -927,7 +942,7 @@ defmodule Radiator.OutlineTest do
)

{:ok, %NodeRepoResult{} = _result} =
Outline.move_node(node_3.uuid, [prev_id: node_2.uuid, parent_id: node_1.uuid])
Outline.move_node(node_3.uuid, prev_id: node_2.uuid, parent_id: node_1.uuid)

assert node_1 |> Outline.order_child_nodes() |> Enum.map(& &1.content) ==
["node_2", "node_3"]
Expand Down

0 comments on commit 1100910

Please sign in to comment.