Skip to content

Commit

Permalink
clean up duplicated code for creating ShowHosts
Browse files Browse the repository at this point in the history
  • Loading branch information
mwingert committed Oct 10, 2024
1 parent dbaf92f commit 53a42a5
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions lib/radiator/podcast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,7 @@ defmodule Radiator.Podcast do
# Suggested to run inside a transaction.
defp associate_hosts(show, hosts) do
Enum.each(hosts, fn host ->
%ShowHosts{}
|> ShowHosts.changeset(%{show_id: show.id, user_id: host.id})
|> Repo.insert!()
create_showhosts!(%{show_id: show.id, user_id: host.id})
end)
end

Expand Down Expand Up @@ -271,9 +269,7 @@ defmodule Radiator.Podcast do

# Add users to hosts
Enum.each(add_hosts, fn host ->
%ShowHosts{}
|> ShowHosts.changeset(%{show_id: show.id, user_id: host.id})
|> Repo.insert!()
create_showhosts!(%{show_id: show.id, user_id: host.id})
end)

# Remove users from hosts
Expand Down Expand Up @@ -364,6 +360,24 @@ defmodule Radiator.Podcast do
Repo.preload(show, fields_or_query, force: true)
end

@doc """
Creates ShowHosts, the association be show and user
## Examples
iex> create_showhosts(%{field: value})
%ShowHosts{}
iex> create_showhosts(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_showhosts!(attrs \\ %{}) do
%ShowHosts{}
|> ShowHosts.changeset(attrs)
|> Repo.insert!()
end

@doc """
Returns the list of episodes.
Expand Down

0 comments on commit 53a42a5

Please sign in to comment.