From cd2efb0d39b24c9e329963aa73dfb62617d5ee54 Mon Sep 17 00:00:00 2001 From: Liam Date: Sun, 14 Jul 2024 14:48:11 -0400 Subject: [PATCH] Remove Repo fetch from Channel module --- lib/philomena/channels.ex | 21 +++++++++++++++++++++ lib/philomena/channels/channel.ex | 16 ++++++++-------- lib/philomena/tags.ex | 16 ++++++++++++++++ 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/lib/philomena/channels.ex b/lib/philomena/channels.ex index ea07f0d5..0f00cbe9 100644 --- a/lib/philomena/channels.ex +++ b/lib/philomena/channels.ex @@ -8,6 +8,7 @@ defmodule Philomena.Channels do alias Philomena.Channels.AutomaticUpdater alias Philomena.Channels.Channel + alias Philomena.Tags use Philomena.Subscriptions, actor_types: ~w(Channel LivestreamChannel), @@ -50,6 +51,7 @@ defmodule Philomena.Channels do """ def create_channel(attrs \\ %{}) do %Channel{} + |> update_artist_tag(attrs) |> Channel.changeset(attrs) |> Repo.insert() end @@ -68,10 +70,29 @@ defmodule Philomena.Channels do """ def update_channel(%Channel{} = channel, attrs) do channel + |> update_artist_tag(attrs) |> Channel.changeset(attrs) |> Repo.update() end + @doc """ + Adds the artist tag from the `"artist_tag"` tag name attribute. + + ## Examples + + iex> update_artist_tag(%Channel{}, %{"artist_tag" => "artist:nighty"}) + %Ecto.Changeset{} + + """ + def update_artist_tag(%Channel{} = channel, attrs) do + tag = + attrs + |> Map.get("artist_tag", "") + |> Tags.get_tag_by_name() + + Channel.artist_tag_changeset(channel, tag) + end + @doc """ Updates a channel's state when it goes live. diff --git a/lib/philomena/channels/channel.ex b/lib/philomena/channels/channel.ex index 3af5a8b1..54bf30ba 100644 --- a/lib/philomena/channels/channel.ex +++ b/lib/philomena/channels/channel.ex @@ -3,7 +3,6 @@ defmodule Philomena.Channels.Channel do import Ecto.Changeset alias Philomena.Tags.Tag - alias Philomena.Repo schema "channels" do belongs_to :associated_artist_tag, Tag @@ -36,19 +35,13 @@ defmodule Philomena.Channels.Channel do @doc false def changeset(channel, attrs) do - tag_id = - case Repo.get_by(Tag, name: attrs["artist_tag"] || "") do - %{id: id} -> id - _ -> nil - end - channel |> cast(attrs, [:type, :short_name]) |> validate_required([:type, :short_name]) |> validate_inclusion(:type, ["PicartoChannel", "PiczelChannel"]) - |> put_change(:associated_artist_tag_id, tag_id) end + @doc false def update_changeset(channel, attrs) do cast(channel, attrs, [ :title, @@ -60,4 +53,11 @@ defmodule Philomena.Channels.Channel do :last_live_at ]) end + + @doc false + def artist_tag_changeset(channel, tag) do + tag_id = Map.get(tag || %{}, :id) + + change(channel, associated_artist_tag_id: tag_id) + end end diff --git a/lib/philomena/tags.ex b/lib/philomena/tags.ex index 0d759e93..d6c6898a 100644 --- a/lib/philomena/tags.ex +++ b/lib/philomena/tags.ex @@ -81,6 +81,22 @@ defmodule Philomena.Tags do """ def get_tag!(id), do: Repo.get!(Tag, id) + @doc """ + Gets a single tag. + + Returns nil if the Tag does not exist. + + ## Examples + + iex> get_tag_by_name("safe") + %Tag{} + + iex> get_tag_by_name("nonexistent") + nil + + """ + def get_tag_by_name(name), do: Repo.get_by(Tag, name: name) + @doc """ Gets a single tag by its name, or the tag it is aliased to, if it is aliased.