Skip to content

Commit

Permalink
create nodecontainers also for episodes
Browse files Browse the repository at this point in the history
like for shows: inbox and root
  • Loading branch information
electronicbites committed Dec 17, 2024
1 parent f709abc commit ff9ab64
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
22 changes: 18 additions & 4 deletions lib/radiator/podcast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ defmodule Radiator.Podcast do
{:error, %Ecto.Changeset{}}
"""

def create_show(attrs \\ %{}) do
Ecto.Multi.new()
|> Ecto.Multi.insert(:inbox, NodeContainer.changeset(%NodeContainer{}, %{}))
Expand Down Expand Up @@ -474,6 +473,7 @@ defmodule Radiator.Podcast do
from e in Episode,
where: [show_id: ^show_id, is_deleted: false],
order_by: [desc: e.number],
preload: [:inbox_node_container, :outline_node_container],
limit: 1
)
end
Expand All @@ -491,9 +491,23 @@ defmodule Radiator.Podcast do
"""
def create_episode(attrs \\ %{}) do
%Episode{}
|> Episode.changeset(attrs)
|> Repo.insert()
Ecto.Multi.new()
|> Ecto.Multi.insert(:inbox, NodeContainer.changeset(%NodeContainer{}, %{}))
|> Ecto.Multi.insert(:root, NodeContainer.changeset(%NodeContainer{}, %{}))
|> Ecto.Multi.insert(:episode, fn %{root: root, inbox: inbox} ->
%Episode{}
|> Episode.changeset(attrs)
|> Ecto.Changeset.put_assoc(:inbox_node_container, inbox)
|> Ecto.Changeset.put_assoc(:outline_node_container, root)
end)
|> Repo.transaction()
|> case do
{:ok, %{episode: episode}} ->
{:ok, episode}

{:error, _tag, error, _others} ->
{:error, error}
end
end

def get_next_episode_number(show_id) do
Expand Down
21 changes: 15 additions & 6 deletions test/radiator/podcast_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ defmodule Radiator.PodcastTest do
episode = episode_fixture(%{show_id: show.id})

assert %Show{episodes: episodes} = Podcast.get_show!(show.id, preload: :episodes)
assert episodes == [episode]
assert Enum.map(episodes, fn e -> e.id end) == [episode.id]
end

test "create_show/1 with valid data creates a show" do
Expand Down Expand Up @@ -203,14 +203,14 @@ defmodule Radiator.PodcastTest do
describe "episodes" do
@invalid_attrs %{title: nil}

test "list_all_episodes/0 returns all episodes" do
test "list_all_episodes/0 returns also deleted episodes" do
deleted_episode =
episode_fixture(
is_deleted: true,
deleted_at: DateTime.utc_now() |> DateTime.truncate(:second)
)

assert Podcast.list_all_episodes() == [deleted_episode]
assert Enum.map(Podcast.list_all_episodes(), fn e -> e.id end) == [deleted_episode.id]
end

test "list_available_episodes/0 returns all episodes" do
Expand All @@ -223,12 +223,13 @@ defmodule Radiator.PodcastTest do
)

found_episodes = Podcast.list_available_episodes()
assert found_episodes == [episode]
assert Enum.map(found_episodes, fn e -> e.id end) == [episode.id]
end

test "get_episode!/1 returns the episode with given id" do
episode = episode_fixture()
assert Podcast.get_episode!(episode.id) == episode
episode_id = episode.id
assert %Episode{id: ^episode_id} = Podcast.get_episode!(episode.id)
end

test "create_episode/1 with valid data creates a episode" do
Expand Down Expand Up @@ -257,10 +258,18 @@ defmodule Radiator.PodcastTest do
assert episode.slug == "some-updated-title"
end

test "create_episode/1 creates local inbox and local root node containers" do
show = show_fixture()
valid_attrs = %{title: "some title", show_id: show.id, number: 5}

assert {:ok, %Episode{} = episode} = Podcast.create_episode(valid_attrs)
refute(is_nil(episode.inbox_node_container_id))
refute(is_nil(episode.outline_node_container_id))
end

test "update_episode/2 with invalid data returns error changeset" do
episode = episode_fixture()
assert {:error, %Ecto.Changeset{}} = Podcast.update_episode(episode, @invalid_attrs)
assert episode == Podcast.get_episode!(episode.id)
end

test "delete_episode/1 deletes the episode" do
Expand Down

0 comments on commit ff9ab64

Please sign in to comment.