270 lines
5.1 KiB
Elixir
270 lines
5.1 KiB
Elixir
defmodule Bright.Streams do
|
|
@moduledoc """
|
|
The Streams context.
|
|
"""
|
|
|
|
import Ecto.Query, warn: false
|
|
alias Bright.Repo
|
|
|
|
alias Bright.Streams.{Stream,Vod}
|
|
alias Bright.Vtubers.Vtuber
|
|
alias Bright.Tags.Tag
|
|
alias Bright.Platforms.Platform
|
|
|
|
@doc """
|
|
Returns the list of streams.
|
|
|
|
## Examples
|
|
|
|
iex> list_streams()
|
|
[%Stream{}, ...]
|
|
|
|
"""
|
|
def list_streams do
|
|
Stream
|
|
|> Repo.all()
|
|
|> Repo.preload([:tags, :vods, :vtubers, :platforms])
|
|
end
|
|
|
|
@doc """
|
|
Gets a single stream.
|
|
|
|
Raises `Ecto.NoResultsError` if the Stream does not exist.
|
|
|
|
## Examples
|
|
|
|
iex> get_stream!(123)
|
|
%Stream{}
|
|
|
|
iex> get_stream!(456)
|
|
** (Ecto.NoResultsError)
|
|
|
|
"""
|
|
def get_stream!(id) do
|
|
Stream
|
|
|> Repo.get!(id)
|
|
|> Repo.preload([:tags, :vods, :vtubers, :platforms])
|
|
end
|
|
|
|
@doc """
|
|
Creates a stream.
|
|
|
|
## Examples
|
|
|
|
iex> create_stream(%{field: value})
|
|
{:ok, %Stream{}}
|
|
|
|
iex> create_stream(%{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def create_stream(attrs \\ %{}) do
|
|
%Stream{}
|
|
# |> Stream.changeset(attrs)
|
|
|> change_stream(attrs)
|
|
|> Repo.insert()
|
|
end
|
|
|
|
@doc """
|
|
Updates a stream.
|
|
|
|
## Examples
|
|
|
|
iex> update_stream(stream, %{field: new_value})
|
|
{:ok, %Stream{}}
|
|
|
|
iex> update_stream(stream, %{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def update_stream(%Stream{} = stream, attrs) do
|
|
stream
|
|
|> change_stream(attrs)
|
|
|> Repo.update()
|
|
end
|
|
|
|
@doc """
|
|
Deletes a stream.
|
|
|
|
## Examples
|
|
|
|
iex> delete_stream(stream)
|
|
{:ok, %Stream{}}
|
|
|
|
iex> delete_stream(stream)
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def delete_stream(%Stream{} = stream) do
|
|
Repo.delete(stream)
|
|
end
|
|
|
|
@doc """
|
|
Returns an `%Ecto.Changeset{}` for tracking stream changes.
|
|
|
|
## Examples
|
|
|
|
iex> change_stream(stream)
|
|
%Ecto.Changeset{data: %Stream{}}
|
|
|
|
"""
|
|
def change_stream(%Stream{} = stream, attrs \\ %{}) do
|
|
tags = list_tags_by_id(attrs["tag_ids"])
|
|
vods = list_vods_by_id(attrs["vod_ids"])
|
|
vtubers = list_vtubers_by_id(attrs["vtuber_ids"])
|
|
platforms = list_platforms_by_id(attrs["platform_ids"])
|
|
stream
|
|
|> Repo.preload([:tags, :vods, :vtubers])
|
|
|> Stream.changeset(attrs)
|
|
|> Ecto.Changeset.put_assoc(:tags, tags)
|
|
|> Ecto.Changeset.put_assoc(:vods, vods)
|
|
|> Ecto.Changeset.put_assoc(:vtubers, vtubers)
|
|
|> Ecto.Changeset.put_assoc(:platforms, platforms)
|
|
end
|
|
|
|
def inc_page_views(%Stream{} = stream) do
|
|
{1, [%Stream{views: views}]} =
|
|
from(s in Stream, where: s.id == ^stream.id, select: [:views])
|
|
|> Repo.update_all(inc: [views: 1])
|
|
put_in(stream.views, views)
|
|
end
|
|
|
|
def list_tags_by_id(nil), do: []
|
|
def list_tags_by_id(tag_ids) do
|
|
Repo.all(from t in Tag, where: t.id in ^tag_ids)
|
|
end
|
|
|
|
def list_vods_by_id(nil), do: []
|
|
def list_vods_by_id(vod_ids) do
|
|
Repo.all(from v in Vod, where: v.id in ^vod_ids)
|
|
end
|
|
|
|
def list_vtubers_by_id(nil), do: []
|
|
def list_vtubers_by_id(vtuber_ids) do
|
|
Repo.all(from v in Vtuber, where: v.id in ^vtuber_ids)
|
|
end
|
|
|
|
def list_platforms_by_id(nil), do: []
|
|
def list_platforms_by_id(platform_ids) do
|
|
Repo.all(from p in Platform, where: p.id in ^platform_ids)
|
|
end
|
|
|
|
alias Bright.Streams.Vod
|
|
|
|
@doc """
|
|
Returns the list of vods.
|
|
|
|
## Examples
|
|
|
|
iex> list_vods()
|
|
[%Vod{}, ...]
|
|
|
|
"""
|
|
def list_vods do
|
|
Repo.all(Vod)
|
|
end
|
|
|
|
@doc """
|
|
Gets a single vod.
|
|
|
|
Raises `Ecto.NoResultsError` if the Vod does not exist.
|
|
|
|
## Examples
|
|
|
|
iex> get_vod!(123)
|
|
%Vod{}
|
|
|
|
iex> get_vod!(456)
|
|
** (Ecto.NoResultsError)
|
|
|
|
"""
|
|
def get_vod!(id), do: Repo.get!(Vod, id)
|
|
|
|
@doc """
|
|
Creates a vod.
|
|
|
|
## Examples
|
|
|
|
iex> create_vod(%{field: value})
|
|
{:ok, %Vod{}}
|
|
|
|
iex> create_vod(%{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def create_vod(attrs \\ %{}) do
|
|
%Vod{}
|
|
|> Vod.changeset(attrs)
|
|
|> Repo.insert()
|
|
|> case do
|
|
{:ok, vod} ->
|
|
maybe_enqueue_process_vod(vod)
|
|
{:ok, vod}
|
|
|
|
{:error, changeset} ->
|
|
{:error, changeset}
|
|
end
|
|
end
|
|
|
|
|
|
defp maybe_enqueue_process_vod(%Vod{id: id, origin_temp_input_url: origin_temp_input_url} = vod) do
|
|
if origin_temp_input_url do
|
|
|
|
%{id: id, origin_temp_input_url: origin_temp_input_url}
|
|
|> Bright.Jobs.ProcessVod.new()
|
|
|> Oban.insert()
|
|
|
|
end
|
|
vod
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
Updates a vod.
|
|
|
|
## Examples
|
|
|
|
iex> update_vod(vod, %{field: new_value})
|
|
{:ok, %Vod{}}
|
|
|
|
iex> update_vod(vod, %{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def update_vod(%Vod{} = vod, attrs) do
|
|
vod
|
|
|> Vod.changeset(attrs)
|
|
|> Repo.update()
|
|
end
|
|
|
|
@doc """
|
|
Deletes a vod.
|
|
|
|
## Examples
|
|
|
|
iex> delete_vod(vod)
|
|
{:ok, %Vod{}}
|
|
|
|
iex> delete_vod(vod)
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def delete_vod(%Vod{} = vod) do
|
|
Repo.delete(vod)
|
|
end
|
|
|
|
@doc """
|
|
Returns an `%Ecto.Changeset{}` for tracking vod changes.
|
|
|
|
## Examples
|
|
|
|
iex> change_vod(vod)
|
|
%Ecto.Changeset{data: %Vod{}}
|
|
|
|
"""
|
|
def change_vod(%Vod{} = vod, attrs \\ %{}) do
|
|
Vod.changeset(vod, attrs)
|
|
end
|
|
end
|