From f157e935c1f627cbc1731a4518ce205fb6d3488a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20W=C3=B6ginger?= Date: Tue, 3 Dec 2024 23:03:42 +0100 Subject: [PATCH] WIP: add root nodes as virtual attributes to show and enhance the query to fetch these nodes with the show --- lib/radiator/podcast.ex | 41 ++++++++++++++++++++++++++++++++++++ lib/radiator/podcast/show.ex | 3 ++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/radiator/podcast.ex b/lib/radiator/podcast.ex index 7ff4f7a6..f1895394 100644 --- a/lib/radiator/podcast.ex +++ b/lib/radiator/podcast.ex @@ -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. diff --git a/lib/radiator/podcast/show.ex b/lib/radiator/podcast/show.ex index 1bd5fe3c..747e2b94 100644 --- a/lib/radiator/podcast/show.ex +++ b/lib/radiator/podcast/show.ex @@ -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")