Skip to content

Commit

Permalink
Optional parent node for move node (#561)
Browse files Browse the repository at this point in the history
* parent id is optional for move node
in order to achieve this we need a different signature, use a keyword list instead of fixed parameters

* rename prev_node_id, parent_node_id => prev_id, parent_id

---------

Co-authored-by: sorax <[email protected]>
  • Loading branch information
electronicbites and sorax authored Sep 6, 2024
1 parent ff7da6b commit 71291e8
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 43 deletions.
46 changes: 32 additions & 14 deletions lib/radiator/outline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,23 @@ defmodule Radiator.Outline do
# if no previous node is given, the new node will be inserted as the first child of the parent node
def insert_node(attrs) do
Repo.transaction(fn ->
prev_node_id = attrs["prev_id"]
parent_node_id = attrs["parent_id"]
prev_id = attrs["prev_id"]
parent_id = attrs["parent_id"]
episode_id = attrs["episode_id"]
# find Node which has been previously connected to prev_node
next_node =
Node
|> where(episode_id: ^episode_id)
|> where_prev_node_equals(prev_node_id)
|> where_parent_node_equals(parent_node_id)
|> where_prev_node_equals(prev_id)
|> where_parent_node_equals(parent_id)
|> Repo.one()

with prev_node <- NodeRepository.get_node_if(prev_node_id),
parent_node <- find_parent_node(prev_node, parent_node_id),
with prev_node <- NodeRepository.get_node_if(prev_id),
parent_node <- find_parent_node(prev_node, parent_id),
true <- parent_and_prev_consistent?(parent_node, prev_node),
true <- episode_valid?(episode_id, parent_node, prev_node),
{:ok, node} <- NodeRepository.create_node(set_parent_id_if(attrs, parent_node)),
{:ok, _node_to_move} <- move_node_if(next_node, parent_node_id, node.uuid) do
{:ok, _node_to_move} <- move_node_if(next_node, parent_id, node.uuid) do
%NodeRepoResult{node: node, next_id: get_node_id(next_node)}
else
false ->
Expand Down 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, node_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, _new_prev_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, other_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, new_prev_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,19 @@ defmodule Radiator.Outline do
end
end

def move_node(node_id, parent_id: parent_id, prev_id: new_prev_id),
do: move_node(node_id, prev_id: new_prev_id, parent_id: parent_id)

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 +299,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 +319,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 Expand Up @@ -437,11 +455,11 @@ defmodule Radiator.Outline do

defp move_node_if(nil, _parent_node_id, _prev_node_id), do: {:ok, nil}

defp move_node_if(node, parent_node_id, prev_node_id) do
defp move_node_if(node, parent_id, prev_id) do
node
|> Node.move_node_changeset(%{
parent_id: parent_node_id,
prev_id: prev_node_id
parent_id: parent_id,
prev_id: prev_id
})
|> Repo.update()
end
Expand Down
6 changes: 3 additions & 3 deletions lib/radiator/outline/command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ defmodule Radiator.Outline.Command do
}
end

def build("move_node", node_id, parent_node_id, prev_node_id, user_id, event_id) do
def build("move_node", node_id, parent_id, prev_id, user_id, event_id) do
%MoveNodeCommand{
event_id: event_id,
user_id: user_id,
node_id: node_id,
parent_id: parent_node_id,
prev_id: prev_node_id
parent_id: parent_id,
prev_id: prev_id
}
end
end
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, parent_id)
|> Outline.move_node(prev_id: prev_id, parent_id: parent_id)
|> handle_move_node_result(command)
end

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

def move_node(node_id, parent_node_id, prev_node_id, user_id, event_id) do
def move_node(node_id, user_id, event_id,
parent_id: parent_id,
prev_id: prev_id
) do
"move_node"
|> Command.build(node_id, parent_node_id, prev_node_id, user_id, event_id)
|> Command.build(node_id, parent_id, prev_id, user_id, event_id)
|> EventProducer.enqueue()
end

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, prev_id, nil, user_id, generate_event_id(socket.id))
Dispatch.move_node(uuid, user_id, generate_event_id(socket.id), prev_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, nil, parent_id, user_id, generate_event_id(socket.id))
Dispatch.move_node(uuid, user_id, generate_event_id(socket.id), parent_id: parent_id)

socket
|> stream_insert(:nodes, to_change_form(node, %{}))
Expand Down
Loading

0 comments on commit 71291e8

Please sign in to comment.