From 857358fd3cda6871aec88872395e3318be6db3a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Wo=CC=88ginger?= Date: Sun, 22 Dec 2024 19:38:26 +0100 Subject: [PATCH 1/2] add a direct episode link to urls Since nodes looses this direct connection it would take some effort to find all urls of an episode for instance --- lib/radiator/resources.ex | 1 - lib/radiator/resources/node_changed_worker.ex | 9 +++++++-- lib/radiator/resources/url.ex | 3 ++- .../20241222183518_add_episodes_to_urls.exs | 9 +++++++++ test/radiator/podcast_test.exs | 2 +- test/radiator/resources_test.exs | 11 +++++++++-- test/support/fixtures/resources_fixtures.ex | 8 +++++++- 7 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 priv/repo/migrations/20241222183518_add_episodes_to_urls.exs diff --git a/lib/radiator/resources.ex b/lib/radiator/resources.ex index f760c794..67bc6cab 100644 --- a/lib/radiator/resources.ex +++ b/lib/radiator/resources.ex @@ -58,7 +58,6 @@ defmodule Radiator.Resources do Enum.map(url_attributes, fn attributes -> {:ok, url} = attributes - |> Map.put(:node_id, node_id) |> create_url() url diff --git a/lib/radiator/resources/node_changed_worker.ex b/lib/radiator/resources/node_changed_worker.ex index 75d7650b..27d82def 100644 --- a/lib/radiator/resources/node_changed_worker.ex +++ b/lib/radiator/resources/node_changed_worker.ex @@ -16,11 +16,16 @@ defmodule Radiator.Resources.NodeChangedWorker do def perform(node_id) do analyzers = [Radiator.NodeAnalyzer.UrlAnalyzer] + node = NodeRepository.get_node!(node_id) url_attributes = - node_id - |> NodeRepository.get_node!() + node |> NodeAnalyzer.do_analyze(analyzers) + |> Enum.map(fn attributes -> + attributes + |> Map.put(:node_id, node_id) + |> Map.put(:episode_id, node.episode_id) + end) _created_urls = Resources.rebuild_node_urls(node_id, url_attributes) :ok diff --git a/lib/radiator/resources/url.ex b/lib/radiator/resources/url.ex index ccf63523..5cf55057 100644 --- a/lib/radiator/resources/url.ex +++ b/lib/radiator/resources/url.ex @@ -31,13 +31,14 @@ defmodule Radiator.Resources.Url do embeds_one :meta_data, MetaData belongs_to :node, Radiator.Outline.Node, type: :binary_id, references: :uuid + belongs_to :episode, Radiator.Podcast.Episode timestamps(type: :utc_datetime) end @doc false def changeset(url, attrs) do url - |> cast(attrs, [:url, :start_bytes, :size_bytes, :node_id]) + |> cast(attrs, [:url, :start_bytes, :size_bytes, :node_id, :episode_id]) |> validate_required([:url, :start_bytes, :size_bytes, :node_id]) |> cast_embed(:meta_data, with: &MetaData.changeset/2) end diff --git a/priv/repo/migrations/20241222183518_add_episodes_to_urls.exs b/priv/repo/migrations/20241222183518_add_episodes_to_urls.exs new file mode 100644 index 00000000..790806f9 --- /dev/null +++ b/priv/repo/migrations/20241222183518_add_episodes_to_urls.exs @@ -0,0 +1,9 @@ +defmodule Radiator.Repo.Migrations.AddEpisodesToUrls do + use Ecto.Migration + + def change do + alter table(:urls) do + add :episode_id, references(:episodes, on_delete: :nothing) + end + end +end diff --git a/test/radiator/podcast_test.exs b/test/radiator/podcast_test.exs index 4e85c58e..4b4228ac 100644 --- a/test/radiator/podcast_test.exs +++ b/test/radiator/podcast_test.exs @@ -12,7 +12,7 @@ defmodule Radiator.PodcastTest do test "list_networks/0 returns all networks" do network = network_fixture() - assert Podcast.list_networks() == [network] + assert Enum.map(Podcast.list_networks(), fn n -> n.id end) == [network.id] end test "list_networks/1 returns all networks with preloaded shows" do diff --git a/test/radiator/resources_test.exs b/test/radiator/resources_test.exs index 75397f9d..6a1001a1 100644 --- a/test/radiator/resources_test.exs +++ b/test/radiator/resources_test.exs @@ -52,10 +52,17 @@ defmodule Radiator.ResourcesbTest do url_text = "https://hexdocs.pm" node = OutlineFixtures.node_fixture() old_url = url_fixture(node_id: node.uuid) + episode_id = node.episode_id - assert [%Url{url: ^url_text, start_bytes: 42, size_bytes: 42}] = + assert [%Url{url: ^url_text, start_bytes: 42, size_bytes: 42, episode_id: ^episode_id}] = Resources.rebuild_node_urls(node.uuid, [ - %{url: url_text, start_bytes: 42, size_bytes: 42} + %{ + url: url_text, + start_bytes: 42, + size_bytes: 42, + node_id: node.uuid, + episode_id: episode_id + } ]) assert_raise Ecto.NoResultsError, fn -> Resources.get_url!(old_url.id) end diff --git a/test/support/fixtures/resources_fixtures.ex b/test/support/fixtures/resources_fixtures.ex index ab050e41..53636a5f 100644 --- a/test/support/fixtures/resources_fixtures.ex +++ b/test/support/fixtures/resources_fixtures.ex @@ -4,12 +4,14 @@ defmodule Radiator.ResourcesFixtures do entities via the `Radiator.Resources` context. """ alias Radiator.OutlineFixtures + alias Radiator.PodcastFixtures alias Radiator.Resources @doc """ Generate a url. """ def url_fixture(attrs \\ %{}) do + episode_id = get_episode_id(attrs) node_id = get_node_id(attrs) {:ok, url} = @@ -19,7 +21,8 @@ defmodule Radiator.ResourcesFixtures do start_bytes: 23, url: "https://elixirschool.com", meta_data: %{title: "Elixir School"}, - node_id: node_id + node_id: node_id, + episode_id: episode_id }) |> Resources.create_url() @@ -28,4 +31,7 @@ defmodule Radiator.ResourcesFixtures do defp get_node_id(%{node_id: id}), do: id defp get_node_id(_), do: OutlineFixtures.node_fixture().uuid + + defp get_episode_id(%{episode_id: id}), do: id + defp get_episode_id(_), do: PodcastFixtures.episode_fixture().id end From f505e7469d7e3c040b0e12fd4258a92cf874845a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20W=C3=B6ginger?= Date: Sun, 22 Dec 2024 20:39:11 +0100 Subject: [PATCH 2/2] move file --- lib/radiator/{radiator => }/podcast/show_hosts.ex | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lib/radiator/{radiator => }/podcast/show_hosts.ex (100%) diff --git a/lib/radiator/radiator/podcast/show_hosts.ex b/lib/radiator/podcast/show_hosts.ex similarity index 100% rename from lib/radiator/radiator/podcast/show_hosts.ex rename to lib/radiator/podcast/show_hosts.ex