Skip to content

Commit

Permalink
list_nodes_by_episode
Browse files Browse the repository at this point in the history
  • Loading branch information
electronicbites committed Jul 23, 2024
1 parent a23176d commit 500a057
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
7 changes: 6 additions & 1 deletion lib/radiator/outline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ defmodule Radiator.Outline do
@doc """
Returns a list of direct child nodes in correct order.
"""

def order_child_nodes(%Node{} = node) do
node
|> get_all_child_nodes()
Expand All @@ -42,6 +41,12 @@ defmodule Radiator.Outline do
|> order_nodes_by_index(nil, [])
end

def list_nodes_by_episode_sorted(episode_id) do
episode_id
|> NodeRepository.list_nodes_by_episode()
|> order_sibling_nodes()
end

defp order_nodes_by_index(index, prev_id, collection) do
case index[prev_id] do
%{uuid: uuid} = node -> order_nodes_by_index(index, uuid, [node | collection])
Expand Down
5 changes: 3 additions & 2 deletions lib/radiator_web/live/episode_live/index.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
defmodule RadiatorWeb.EpisodeLive.Index do
use RadiatorWeb, :live_view

alias Radiator.Outline.{Dispatch, NodeRepository}
alias Radiator.Outline
alias Radiator.Outline.Dispatch

alias Radiator.Outline.Event.{
NodeContentChangedEvent,
Expand Down Expand Up @@ -227,7 +228,7 @@ defmodule RadiatorWeb.EpisodeLive.Index do
[]
end

defp get_nodes(%{id: id}), do: NodeRepository.list_nodes_by_episode(id)
defp get_nodes(%{id: id}), do: Outline.list_nodes_by_episode_sorted(id)
defp get_nodes(_), do: []

defp generate_event_id(id), do: Ecto.UUID.generate() <> ":" <> id
Expand Down
38 changes: 28 additions & 10 deletions test/radiator_web/live/episode_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,42 @@ defmodule RadiatorWeb.EpisodeLiveTest do
show = show_fixture()
episode = episode_fixture(%{show_id: show.id})

node_2 = node_fixture(%{episode_id: episode.id, content: "node 2"})
node_1 =
node_fixture(
episode_id: episode.id,
parent_id: nil,
prev_id: nil,
content: "node_1"
)

node_1 = node_fixture(%{episode_id: episode.id, content: "node 1", prev_id: node_2.uuid})
node_3 =
node_fixture(
episode_id: episode.id,
parent_id: node_1.uuid,
prev_id: nil,
content: "node_3"
)

Outline.move_node(node_1.uuid, nil, nil)
node_2 =
node_fixture(
episode_id: episode.id,
parent_id: node_1.uuid,
prev_id: node_3.uuid,
content: "node_2"
)

%{conn: log_in_user(conn, user), show: show, episode: episode, nodes: [node_1, node_2]}
Outline.move_node(node_3.uuid, node_2.uuid, node_1.uuid)

%{conn: log_in_user(conn, user), show: show, episode: episode, nodes: [node_3, node_2]}
end

test "lists all nodes", %{conn: conn, show: show, episode: episode} do
{:ok, live, _html} = live(conn, ~p"/admin/podcast/#{show.id}")

nodes =
episode.id
|> Outline.list_nodes_by_episode_sorted()


NodeRepository.list_nodes_by_episode(episode.id)
nodes = episode.id
|> NodeRepository.list_nodes_by_episode()
|> Outline.order_sibling_nodes
assert_push_event(live, "list", %{nodes: ^nodes})
end

Expand Down Expand Up @@ -100,7 +118,7 @@ defmodule RadiatorWeb.EpisodeLiveTest do
assert_push_event(other_live, "insert", %{node: ^new_node})
end

test "update node content", %{conn: conn, show: show, nodes: [%{uuid: uuid} | _] } do
test "update node content", %{conn: conn, show: show, nodes: [%{uuid: uuid} | _]} do
{:ok, live, _html} = live(conn, ~p"/admin/podcast/#{show.id}")
{:ok, other_live, _html} = live(conn, ~p"/admin/podcast/#{show.id}")

Expand Down

0 comments on commit 500a057

Please sign in to comment.