Skip to content

Commit

Permalink
group by parent id
Browse files Browse the repository at this point in the history
  • Loading branch information
electronicbites committed Jul 23, 2024
1 parent 500a057 commit 4f44af7
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/radiator/outline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,27 @@ defmodule Radiator.Outline do
|> order_sibling_nodes()
end


@doc """
Orders a given list of nodes by their prev_id.
"""
def order_sibling_nodes(nodes) do
nodes
|> Enum.map(fn node -> {node.prev_id, node} end)
|> Map.new()
|> order_nodes_by_index(nil, [])
end


@doc """
Returns a list of all child nodes.
"""
def list_nodes_by_episode_sorted(episode_id) do
episode_id
|> NodeRepository.list_nodes_by_episode()
|> order_sibling_nodes()
|> Enum.group_by(& &1.parent_id)
|> Enum.map(fn {_parent_id, children} -> order_sibling_nodes(children) end)
|> List.flatten()
end

defp order_nodes_by_index(index, prev_id, collection) do
Expand Down

0 comments on commit 4f44af7

Please sign in to comment.