mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 13:47:58 +01:00
Remove Repo fetch from Channel module
This commit is contained in:
parent
68352bcf9a
commit
cd2efb0d39
3 changed files with 45 additions and 8 deletions
|
@ -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.
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
|
Loading…
Reference in a new issue