From 4f44af7d3f05091992f91a87e948c5c547588015 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20W=C3=B6ginger?= Date: Tue, 23 Jul 2024 20:59:32 +0200 Subject: [PATCH] group by parent id --- lib/radiator/outline.ex | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/radiator/outline.ex b/lib/radiator/outline.ex index 01bde984..8be437d9 100644 --- a/lib/radiator/outline.ex +++ b/lib/radiator/outline.ex @@ -34,6 +34,10 @@ 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) @@ -41,10 +45,16 @@ defmodule Radiator.Outline do |> 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