Skip to content

Commit

Permalink
WIP: tree node function
Browse files Browse the repository at this point in the history
fixup 3
  • Loading branch information
electronicbites committed Feb 14, 2024
1 parent 928e679 commit fce68ce
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
42 changes: 39 additions & 3 deletions lib/radiator/outline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,18 @@ defmodule Radiator.Outline do
Node
|> where([n], is_nil(n.parent_id))
|> where([n], n.episode_id == ^episode_id)
|> select([n], %{uuid: n.uuid, content: n.content, parent_id: n.parent_id, prev_id: n.prev_id, level: 0})

node_tree_recursion_query =
Node
|> join(:inner, [n], nd in "node_tree", on: n.parent_id == nd.uuid)
# node_tree_recursion_query =
# Node
# |> join(:inner, [n], nd in "node_tree", on: n.parent_id == nd.uuid)
# |> fragmet("JOIN node_tree ON outline_nodes.parent_id = node_tree.uuid")
# |> select([n], %{uuid: n.uuid, content: n.content, parent_id: n.parent_id, prev_id: n.prev_id, level: fragment("'node_tree.level + 1'")})


node_tree_recursion_query = from outline_node in "outline_nodes",
join: node_tree in "node_tree", on: outline_node.parent_id == node_tree.uuid,
select: [outline_node.uuid, outline_node.content, outline_node.parent_id, outline_node.prev_id, node_tree.level + 1]

node_tree_query =
node_tree_initial_query
Expand All @@ -96,6 +104,34 @@ defmodule Radiator.Outline do
{:ok, tree}
end



# WITH RECURSIVE cte AS (
# SELECT uuid, content, parent_id, prev_id, 0 AS level
# FROM outline_nodes
# WHERE episode_id = 2 and parent_id is NULL
# UNION ALL
# SELECT outline_nodes.uuid, outline_nodes.content, outline_nodes.parent_id, outline_nodes.prev_id, cte.level + 1
# FROM outline_nodes
# JOIN cte ON outline_nodes.parent_id = cte.uuid
# )
# SELECT * FROM cte;
#

#
# WITH RECURSIVE "node_tree" AS (
# SELECT so0."uuid" AS "uuid", so0."content" AS "content", so0."parent_id" AS "parent_id", so0."prev_id" AS "prev_id", 0 AS "level"

Check warning on line 123 in lib/radiator/outline.ex

View workflow job for this annotation

GitHub Actions / Build & Test

Line is too long (max is 120, was 139).
# FROM "outline_nodes" AS so0
# WHERE (so0."parent_id" IS NULL) AND (so0."episode_id" = $1)
# UNION ALL (
# SELECT so0."uuid", so0."content", so0."parent_id", so0."prev_id", 'node_tree.level + 1'
# FROM "outline_nodes" AS so0
# INNER JOIN "node_tree" AS sn1 ON so0."parent_id" = sn1."uuid")) SELECT o0."uuid", o0."content", o0."creator_id", o0."parent_id", o0."prev_id", o0."episode_id", o0."inserted_at", o0."updated_at" FROM "outline_nodes" AS o0

Check warning on line 129 in lib/radiator/outline.ex

View workflow job for this annotation

GitHub Actions / Build & Test

Line is too long (max is 120, was 226).





@doc """
Creates a node.
Expand Down
1 change: 1 addition & 0 deletions lib/radiator/outline/node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule Radiator.Outline.Node do
field :creator_id, :integer
field :parent_id, Ecto.UUID
field :prev_id, Ecto.UUID
field :level, :integer, virtual: true

belongs_to :episode, Episode

Expand Down
6 changes: 5 additions & 1 deletion priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ alias Radiator.{Accounts, Outline, Podcast}
{:ok, show} =
Podcast.create_show(%{title: "Tech Weekly", network_id: network.id})

{:ok, _episode} =
{:ok, past_episode} =
Podcast.create_episode(%{title: "past episode", show_id: show.id})

{:ok, current_episode} =
Expand Down Expand Up @@ -60,3 +60,7 @@ alias Radiator.{Accounts, Outline, Podcast}
episode_id: current_episode.id,
prev_id: node211.uuid
})


{:ok, past_parent_node} =
Outline.create_node(%{content: "Old Content", episode_id: past_episode.id})

0 comments on commit fce68ce

Please sign in to comment.