Skip to content

Commit

Permalink
more fixups for datamodel
Browse files Browse the repository at this point in the history
  • Loading branch information
electronicbites committed Dec 3, 2024
1 parent 40a908f commit a8560b2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
6 changes: 5 additions & 1 deletion lib/radiator/outline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 44 additions & 1 deletion lib/radiator/outline/node_repository.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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(%{
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/radiator/podcast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion test/support/data_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down

0 comments on commit a8560b2

Please sign in to comment.