Skip to content

Commit

Permalink
Merge pull request #594 from podlove/feature/add-show-id-to-nodes
Browse files Browse the repository at this point in the history
Feature/add show id to nodes
  • Loading branch information
electronicbites authored Nov 24, 2024
2 parents a9ce11c + 1d666cb commit 70c4407
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 7 deletions.
17 changes: 15 additions & 2 deletions lib/radiator/outline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ defmodule Radiator.Outline do
alias Radiator.Outline.NodeRepoResult
alias Radiator.Outline.NodeRepository
alias Radiator.Outline.Validations, as: NodeValidator
alias Radiator.Podcast
alias Radiator.Podcast.Episode
alias Radiator.Repo

require Logger
Expand Down Expand Up @@ -74,7 +76,7 @@ defmodule Radiator.Outline do
# if a previous node is given, the new node will be inserted after the previous node
# if no parent is given, the new node will be inserted as a root node
# if no previous node is given, the new node will be inserted as the first child of the parent node
def insert_node(attrs) do
def insert_node(%{"show_id" => _show_id} = attrs) do
Repo.transaction(fn ->
prev_id = attrs["prev_id"]
parent_id = attrs["parent_id"]
Expand All @@ -88,7 +90,10 @@ defmodule Radiator.Outline do

with true <- parent_and_prev_consistent?(parent_node, prev_node),
true <- episode_valid?(episode_id, parent_node, prev_node),
{:ok, node} <- NodeRepository.create_node(set_parent_id_if(attrs, parent_node)),
{:ok, node} <-
attrs
|> set_parent_id_if(parent_node)
|> NodeRepository.create_node(),
{:ok, _node_to_move} <-
NodeRepository.move_node_if(next_node, get_node_id(parent_node), node.uuid) do
%NodeRepoResult{node: node, next: get_node_result_info(next_node), episode_id: episode_id}
Expand All @@ -103,6 +108,14 @@ defmodule Radiator.Outline do
end)
end

def insert_node(%{"episode_id" => episode_id} = attrs) do
%Episode{show_id: show_id} = Podcast.get_episode!(episode_id)

attrs
|> Map.put("show_id", show_id)
|> insert_node()
end

@doc """
Intends a node given by its id (by using the tab key).
Expand Down
14 changes: 12 additions & 2 deletions lib/radiator/outline/node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Radiator.Outline.Node do
use Ecto.Schema
import Ecto.Changeset
alias Radiator.Outline.Node
alias Radiator.Podcast.Episode
alias Radiator.Podcast.{Episode, Show}
alias Radiator.Resources.Url

@derive {Jason.Encoder, only: [:uuid, :content, :creator_id, :parent_id, :prev_id]}
Expand All @@ -17,6 +17,7 @@ defmodule Radiator.Outline.Node do
field :level, :integer, virtual: true

belongs_to :episode, Episode
belongs_to :show, Show
belongs_to :parent, Node, references: :uuid, type: Ecto.UUID
belongs_to :prev, Node, references: :uuid, type: Ecto.UUID
has_many :urls, Url, foreign_key: :node_id
Expand All @@ -31,9 +32,18 @@ defmodule Radiator.Outline.Node do
"""
def insert_changeset(node, attributes) do
node
|> cast(attributes, [:uuid, :content, :episode_id, :creator_id, :parent_id, :prev_id])
|> cast(attributes, [
:uuid,
:content,
:episode_id,
:creator_id,
:parent_id,
:prev_id,
:show_id
])
|> put_uuid()
|> validate_required([:episode_id])
# |> validate_required([:show_id]) # TODO
|> validate_format(:uuid, ~r/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i)
|> unique_constraint(:uuid, name: "outline_nodes_pkey")
end
Expand Down
2 changes: 2 additions & 0 deletions lib/radiator/podcast/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Radiator.Podcast.Show do
use Ecto.Schema
import Ecto.Changeset
alias Radiator.Accounts.User
alias Radiator.Outline.Node
alias Radiator.Podcast.{Episode, Network}

schema "shows" do
Expand All @@ -15,6 +16,7 @@ defmodule Radiator.Podcast.Show do
belongs_to :network, Network

has_many(:episodes, Episode)
has_many(:outline_nodes, Node)
many_to_many(:hosts, User, join_through: "show_hosts")

timestamps(type: :utc_datetime)
Expand Down
11 changes: 11 additions & 0 deletions priv/repo/migrations/20241124094719_add_show_id_to_nodes.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Radiator.Repo.Migrations.AddShowIdToNodes do
use Ecto.Migration

def change do
alter table(:outline_nodes) do
add :show_id, references(:shows, on_delete: :nothing)
end

create index(:outline_nodes, [:show_id])
end
end
21 changes: 19 additions & 2 deletions test/radiator/outline_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Radiator.OutlineTest do

describe "node_tree" do
test "generate from template" do
%{id: episode_id} = episode_fixture()
%{id: episode_id, show_id: show_id} = episode_fixture()

nodes =
[
Expand All @@ -39,7 +39,7 @@ defmodule Radiator.OutlineTest do
]},
{"node-5"}
]
|> node_tree_fixture(%{episode_id: episode_id})
|> node_tree_fixture(%{episode_id: episode_id, show_id: show_id})

assert length(nodes) == 13
assert Enum.all?(nodes, &match?(%Node{episode_id: ^episode_id}, &1))
Expand Down Expand Up @@ -152,6 +152,23 @@ defmodule Radiator.OutlineTest do
assert new_node.prev_id == nested_node_1.uuid
end

test "the show_id gets set - even if not given", %{
node_3: node_3,
nested_node_1: nested_node_1
} do
node_attrs = %{
"content" => "new node",
"episode_id" => node_3.episode_id,
"parent_id" => node_3.uuid,
"prev_id" => nested_node_1.uuid
}

{:ok, %{node: new_node}} = Outline.insert_node(node_attrs)

episode = Podcast.get_episode!(node_3.episode_id)
assert new_node.show_id == episode.show_id
end

test "the next node - if existing - changes its prev_id and gets returned", %{
node_2: node_2,
node_3: node_3
Expand Down
13 changes: 13 additions & 0 deletions test/support/data_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ defmodule Radiator.DataCase do
node_1 =
node_fixture(
episode_id: episode.id,
show_id: episode.show_id,
parent_id: nil,
prev_id: nil,
content: "node_1"
Expand All @@ -74,6 +75,7 @@ defmodule Radiator.DataCase do
node_2 =
node_fixture(
episode_id: episode.id,
show_id: episode.show_id,
parent_id: nil,
prev_id: node_1.uuid,
content: "node_2"
Expand All @@ -96,6 +98,7 @@ defmodule Radiator.DataCase do
node_1 =
node_fixture(
episode_id: episode.id,
show_id: episode.show_id,
parent_id: nil,
prev_id: nil,
content: "node_1"
Expand All @@ -104,6 +107,7 @@ defmodule Radiator.DataCase do
node_2 =
node_fixture(
episode_id: episode.id,
show_id: episode.show_id,
parent_id: node_1.uuid,
prev_id: nil,
content: "node_2"
Expand All @@ -121,6 +125,7 @@ defmodule Radiator.DataCase do
parent_node =
node_fixture(
episode_id: episode.id,
show_id: episode.show_id,
parent_id: nil,
prev_id: nil,
content: "root of all evil"
Expand All @@ -129,6 +134,7 @@ defmodule Radiator.DataCase do
node_1 =
node_fixture(
episode_id: episode.id,
show_id: episode.show_id,
parent_id: parent_node.uuid,
prev_id: nil,
content: "node_1"
Expand All @@ -137,6 +143,7 @@ defmodule Radiator.DataCase do
node_2 =
node_fixture(
episode_id: episode.id,
show_id: episode.show_id,
parent_id: parent_node.uuid,
prev_id: node_1.uuid,
content: "node_2"
Expand All @@ -145,6 +152,7 @@ defmodule Radiator.DataCase do
node_3 =
node_fixture(
episode_id: episode.id,
show_id: episode.show_id,
parent_id: parent_node.uuid,
prev_id: node_2.uuid,
content: "node_3"
Expand All @@ -153,6 +161,7 @@ defmodule Radiator.DataCase do
node_4 =
node_fixture(
episode_id: episode.id,
show_id: episode.show_id,
parent_id: parent_node.uuid,
prev_id: node_3.uuid,
content: "node_4"
Expand All @@ -161,6 +170,7 @@ defmodule Radiator.DataCase do
node_5 =
node_fixture(
episode_id: episode.id,
show_id: episode.show_id,
parent_id: parent_node.uuid,
prev_id: node_4.uuid,
content: "node_5"
Expand All @@ -169,6 +179,7 @@ defmodule Radiator.DataCase do
node_6 =
node_fixture(
episode_id: episode.id,
show_id: episode.show_id,
parent_id: parent_node.uuid,
prev_id: node_5.uuid,
content: "node_6"
Expand All @@ -177,6 +188,7 @@ defmodule Radiator.DataCase do
nested_node_1 =
node_fixture(
episode_id: episode.id,
show_id: episode.show_id,
parent_id: node_3.uuid,
prev_id: nil,
content: "nested_node_1"
Expand All @@ -185,6 +197,7 @@ defmodule Radiator.DataCase do
nested_node_2 =
node_fixture(
episode_id: episode.id,
show_id: episode.show_id,
parent_id: node_3.uuid,
prev_id: nested_node_1.uuid,
content: "nested_node_2"
Expand Down
3 changes: 2 additions & 1 deletion test/support/fixtures/outline_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ defmodule Radiator.OutlineFixtures do
attrs
|> Enum.into(%{
content: "some content",
episode_id: episode.id
episode_id: episode.id,
show_id: episode.show_id
})
|> NodeRepository.create_node()

Expand Down

0 comments on commit 70c4407

Please sign in to comment.