Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task/add episodes to urls #602

Merged
merged 2 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/radiator/resources.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions lib/radiator/resources/node_changed_worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/radiator/resources/url.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions priv/repo/migrations/20241222183518_add_episodes_to_urls.exs
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion test/radiator/podcast_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions test/radiator/resources_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion test/support/fixtures/resources_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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} =
Expand All @@ -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()

Expand All @@ -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
Loading