From a8560b2a48a8092c82cdea9478795ecd137f5448 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Wo=CC=88ginger?= Date: Tue, 3 Dec 2024 21:22:21 +0100 Subject: [PATCH] more fixups for datamodel --- lib/radiator/outline.ex | 6 +++- lib/radiator/outline/node_repository.ex | 45 ++++++++++++++++++++++++- lib/radiator/podcast.ex | 2 +- test/support/data_case.ex | 10 +++++- 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/lib/radiator/outline.ex b/lib/radiator/outline.ex index e4b50190..ca25a48c 100644 --- a/lib/radiator/outline.ex +++ b/lib/radiator/outline.ex @@ -482,7 +482,11 @@ defmodule Radiator.Outline do Repo.transaction(fn -> old_next_node = - NodeRepository.get_node_by_parent_and_prev(get_node_id(parent_node), node.uuid, node.episode_id) + NodeRepository.get_node_by_parent_and_prev( + get_node_id(parent_node), + node.uuid, + node.episode_id + ) new_next_node = NodeRepository.get_node_by_parent_and_prev(new_parent_id, new_prev_id, node.episode_id) diff --git a/lib/radiator/outline/node_repository.ex b/lib/radiator/outline/node_repository.ex index 95854bd4..372dc50a 100644 --- a/lib/radiator/outline/node_repository.ex +++ b/lib/radiator/outline/node_repository.ex @@ -30,7 +30,7 @@ defmodule Radiator.Outline.NodeRepository do Creates the internal nodes for a show, this is the global root and the global inbox. """ - def create_nodes_for_show(show_id) do + def create_virtual_nodes_for_show(show_id) do # create a root node for a show {:ok, show_root} = create_node(%{ @@ -51,6 +51,49 @@ defmodule Radiator.Outline.NodeRepository do {show_root, global_inbox} end + @doc """ + Creates the internal nodes for an episode, this is the episode root + and the episode inbox. + """ + def create_virtual_nodes_for_episode(%{id: episode_id, show_id: show_id}) do + # create a root node for a show + {show_root, _global_inbox} = get_virtual_nodes_for_show(show_id) + + {:ok, episode_root} = + create_node(%{ + episode_id: episode_id, + show_id: show_id, + parent_id: show_root.uuid, + prev_id: nil, + _type: "episode_root" + }) + + {:ok, episode_inbox} = + create_node(%{ + episode_id: episode_id, + show_id: show_id, + parent_id: episode_root.uuid, + prev_id: nil, + _type: "episode_inbox" + }) + + {episode_root, episode_inbox} + end + + def get_virtual_nodes_for_show(show_id) do + [node_1, node_2] = + Node + |> where([p], p.show_id == ^show_id) + |> where([p], p._type in [:global_root, :global_inbox]) + |> Repo.all() + + if node_1._type == :global_root do + {node_1, node_2} + else + {node_2, node_1} + end + end + @doc """ Deletes a node from the repository. diff --git a/lib/radiator/podcast.ex b/lib/radiator/podcast.ex index f0acefc4..7ff4f7a6 100644 --- a/lib/radiator/podcast.ex +++ b/lib/radiator/podcast.ex @@ -191,7 +191,7 @@ defmodule Radiator.Podcast do |> Show.changeset(attrs) |> Repo.insert() - {_show_root, _global_inbox} = NodeRepository.create_nodes_for_show(show.id) + {_show_root, _global_inbox} = NodeRepository.create_virtual_nodes_for_show(show.id) show end) end diff --git a/test/support/data_case.ex b/test/support/data_case.ex index 208f87df..136ba872 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -16,6 +16,7 @@ defmodule Radiator.DataCase do use ExUnit.CaseTemplate alias Ecto.Adapters.SQL.Sandbox + alias Radiator.Outline.NodeRepository alias Radiator.PodcastFixtures import Radiator.OutlineFixtures @@ -93,7 +94,10 @@ defmodule Radiator.DataCase do end def simple_node_fixture_hierachical(_) do - episode = PodcastFixtures.episode_fixture() + show = PodcastFixtures.show_fixture() + {_show_root, _global_inbox} = NodeRepository.create_virtual_nodes_for_show(show.id) + + episode = PodcastFixtures.episode_fixture(show_id: show.id) node_1 = node_fixture( @@ -120,8 +124,12 @@ defmodule Radiator.DataCase do end def complex_node_fixture(_) do + show = PodcastFixtures.show_fixture() + {_show_root, _global_inbox} = NodeRepository.create_virtual_nodes_for_show(show.id) episode = PodcastFixtures.episode_fixture() + {_show_root, _global_inbox} = NodeRepository.create_virtual_nodes_for_episode(episode) + parent_node = node_fixture( episode_id: episode.id,