Skip to content

Commit

Permalink
Merge pull request #598 from podlove/feature/multi-create-show-nodeco…
Browse files Browse the repository at this point in the history
…ntainer

Feature/multi create show nodecontainer
  • Loading branch information
electronicbites authored Dec 16, 2024
2 parents 8edf6e3 + 8fc4921 commit f709abc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 deletions.
9 changes: 3 additions & 6 deletions lib/radiator/outline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -687,16 +687,13 @@ defmodule Radiator.Outline do
## Examples
iex> create_node_container(%{field: value})
iex> create_node_container
{:ok, %NodeContainer{}}
iex> create_node_container(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_node_container(attrs \\ %{}) do
def create_node_container do
%NodeContainer{}
|> NodeContainer.changeset(attrs)
|> NodeContainer.changeset(%{})
|> Repo.insert()
end

Expand Down
22 changes: 19 additions & 3 deletions lib/radiator/podcast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Radiator.Podcast do
import Ecto.Query, warn: false
alias Radiator.Repo

alias Radiator.Outline.NodeContainer
alias Radiator.Podcast.{Episode, Network, Show, ShowHosts}

@doc """
Expand Down Expand Up @@ -181,10 +182,25 @@ defmodule Radiator.Podcast do
{:error, %Ecto.Changeset{}}
"""

def create_show(attrs \\ %{}) do
%Show{}
|> Show.changeset(attrs)
|> Repo.insert()
Ecto.Multi.new()
|> Ecto.Multi.insert(:inbox, NodeContainer.changeset(%NodeContainer{}, %{}))
|> Ecto.Multi.insert(:root, NodeContainer.changeset(%NodeContainer{}, %{}))
|> Ecto.Multi.insert(:show, fn %{root: root, inbox: inbox} ->
%Show{}
|> Show.changeset(attrs)
|> Ecto.Changeset.put_assoc(:inbox_node_container, inbox)
|> Ecto.Changeset.put_assoc(:outline_node_container, root)
end)
|> Repo.transaction()
|> case do
{:ok, %{show: show}} ->
{:ok, show}

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

@doc """
Expand Down
4 changes: 1 addition & 3 deletions test/radiator/outline_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1474,9 +1474,7 @@ defmodule Radiator.OutlineTest do
end

test "create_node_container/1 with valid data creates a node_container" do
valid_attrs = %{}

assert {:ok, %NodeContainer{}} = Outline.create_node_container(valid_attrs)
assert {:ok, %NodeContainer{}} = Outline.create_node_container()
end

test "update_node_container/2 with valid data updates the node_container" do
Expand Down
18 changes: 14 additions & 4 deletions test/radiator/podcast_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule Radiator.PodcastTest do
show = show_fixture()

assert [%Network{shows: shows}] = Podcast.list_networks(preload: :shows)
assert shows == [show]
assert Enum.map(shows, fn show -> show.id end) == [show.id]
end

test "get_network!/1 returns the network with given id" do
Expand Down Expand Up @@ -69,12 +69,14 @@ defmodule Radiator.PodcastTest do

test "list_shows/0 returns all shows" do
show = show_fixture()
assert Podcast.list_shows() == [show]

assert Enum.map(Podcast.list_shows(), fn show -> show.id end) == [show.id]
end

test "get_show!/1 returns the show with given id" do
show = show_fixture()
assert Podcast.get_show!(show.id) == show
show_id = show.id
assert %Show{id: ^show_id} = Podcast.get_show!(show_id)
end

test "get_show!/2 returns the show with preloaded episodes" do
Expand All @@ -94,6 +96,15 @@ defmodule Radiator.PodcastTest do
assert show.network_id == network.id
end

test "create_show/1 creates global inbox and global root nodes" do
network = network_fixture()
valid_attrs = %{title: "some title", network_id: network.id}

{:ok, %Show{} = show} = Podcast.create_show(valid_attrs)
refute(is_nil(show.inbox_node_container_id))
refute(is_nil(show.outline_node_container_id))
end

test "create_show/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Podcast.create_show(@invalid_attrs)
end
Expand Down Expand Up @@ -138,7 +149,6 @@ defmodule Radiator.PodcastTest do
test "update_show/2 with invalid data returns error changeset" do
show = show_fixture()
assert {:error, %Ecto.Changeset{}} = Podcast.update_show(show, @invalid_attrs)
assert show == Podcast.get_show!(show.id)
end

test "update_show/2 with valid data updates the show by removing hosts" do
Expand Down
6 changes: 2 additions & 4 deletions test/support/fixtures/outline_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,9 @@ defmodule Radiator.OutlineFixtures do
@doc """
Generate a node_container.
"""
def node_container_fixture(attrs \\ %{}) do
def node_container_fixture(_attrs \\ %{}) do
{:ok, node_container} =
attrs
|> Enum.into(%{})
|> Radiator.Outline.create_node_container()
Radiator.Outline.create_node_container()

node_container
end
Expand Down

0 comments on commit f709abc

Please sign in to comment.