Skip to content

Commit

Permalink
WIP: add root nodes as virtual attributes to show and enhance the que…
Browse files Browse the repository at this point in the history
…ry to fetch these nodes with the show
  • Loading branch information
electronicbites committed Dec 3, 2024
1 parent d254b03 commit f157e93
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
41 changes: 41 additions & 0 deletions lib/radiator/podcast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,47 @@ defmodule Radiator.Podcast do
|> Repo.preload(preload)
end

@doc """
Gets a single show and preloads its virtual nodes (root and inbox) in a single query.
Sets the virtual attributes root_node_id and inbox_node_id.
## Examples
iex> get_show(123)
%Show{root_node_id: "uuid-1", inbox_node_id: "uuid-2"}
iex> get_show(456)
nil
"""
def get_show(id, preload: preload) do
Show
|> join(:inner, [s], n in Node,
on: n.show_id == s.id and n._type in [:global_root, :global_inbox])
|> where([s, _], s.id == ^id)
|> select([s, n], %{
show: s,
nodes: n
})
|> Repo.all()
# |> Repo.preload(preload) # FIXME
|> case do
[] ->
nil
nodes_and_show ->
show = hd(nodes_and_show).show
{root_id, inbox_id} = nodes_and_show
|> Enum.map(& &1.nodes)
|> Enum.reduce({nil, nil}, fn node, {root, inbox} ->
case node._type do
:global_root -> {node.uuid, inbox}
:global_inbox -> {root, node.uuid}
end
end)
%{show | root_node_id: root_id, inbox_node_id: inbox_id}
end
end

@doc """
Creates a show.
Expand Down
3 changes: 2 additions & 1 deletion lib/radiator/podcast/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ defmodule Radiator.Podcast.Show do
schema "shows" do
field :title, :string
field :description, :string
field :root_node_id, :binary_id, virtual: true
field :inbox_node_id, :binary_id, virtual: true

belongs_to :network, Network

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

0 comments on commit f157e93

Please sign in to comment.