mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-02-12 17:14:22 +01:00
200 lines
3.7 KiB
Elixir
200 lines
3.7 KiB
Elixir
defmodule Philomena.Channels do
|
|
@moduledoc """
|
|
The Channels context.
|
|
"""
|
|
|
|
import Ecto.Query, warn: false
|
|
alias Philomena.Repo
|
|
|
|
alias Philomena.Channels.Channel
|
|
|
|
@doc """
|
|
Returns the list of channels.
|
|
|
|
## Examples
|
|
|
|
iex> list_channels()
|
|
[%Channel{}, ...]
|
|
|
|
"""
|
|
def list_channels do
|
|
Repo.all(Channel)
|
|
end
|
|
|
|
@doc """
|
|
Gets a single channel.
|
|
|
|
Raises `Ecto.NoResultsError` if the Channel does not exist.
|
|
|
|
## Examples
|
|
|
|
iex> get_channel!(123)
|
|
%Channel{}
|
|
|
|
iex> get_channel!(456)
|
|
** (Ecto.NoResultsError)
|
|
|
|
"""
|
|
def get_channel!(id), do: Repo.get!(Channel, id)
|
|
|
|
@doc """
|
|
Creates a channel.
|
|
|
|
## Examples
|
|
|
|
iex> create_channel(%{field: value})
|
|
{:ok, %Channel{}}
|
|
|
|
iex> create_channel(%{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def create_channel(attrs \\ %{}) do
|
|
%Channel{}
|
|
|> Channel.changeset(attrs)
|
|
|> Repo.insert()
|
|
end
|
|
|
|
@doc """
|
|
Updates a channel.
|
|
|
|
## Examples
|
|
|
|
iex> update_channel(channel, %{field: new_value})
|
|
{:ok, %Channel{}}
|
|
|
|
iex> update_channel(channel, %{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def update_channel(%Channel{} = channel, attrs) do
|
|
channel
|
|
|> Channel.changeset(attrs)
|
|
|> Repo.update()
|
|
end
|
|
|
|
@doc """
|
|
Deletes a Channel.
|
|
|
|
## Examples
|
|
|
|
iex> delete_channel(channel)
|
|
{:ok, %Channel{}}
|
|
|
|
iex> delete_channel(channel)
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def delete_channel(%Channel{} = channel) do
|
|
Repo.delete(channel)
|
|
end
|
|
|
|
@doc """
|
|
Returns an `%Ecto.Changeset{}` for tracking channel changes.
|
|
|
|
## Examples
|
|
|
|
iex> change_channel(channel)
|
|
%Ecto.Changeset{source: %Channel{}}
|
|
|
|
"""
|
|
def change_channel(%Channel{} = channel) do
|
|
Channel.changeset(channel, %{})
|
|
end
|
|
|
|
alias Philomena.Channels.Subscription
|
|
|
|
@doc """
|
|
Returns the list of channel_subscriptions.
|
|
|
|
## Examples
|
|
|
|
iex> list_channel_subscriptions()
|
|
[%Subscription{}, ...]
|
|
|
|
"""
|
|
def list_channel_subscriptions do
|
|
Repo.all(Subscription)
|
|
end
|
|
|
|
@doc """
|
|
Gets a single subscription.
|
|
|
|
Raises `Ecto.NoResultsError` if the Subscription does not exist.
|
|
|
|
## Examples
|
|
|
|
iex> get_subscription!(123)
|
|
%Subscription{}
|
|
|
|
iex> get_subscription!(456)
|
|
** (Ecto.NoResultsError)
|
|
|
|
"""
|
|
def get_subscription!(id), do: Repo.get!(Subscription, id)
|
|
|
|
@doc """
|
|
Creates a subscription.
|
|
|
|
## Examples
|
|
|
|
iex> create_subscription(%{field: value})
|
|
{:ok, %Subscription{}}
|
|
|
|
iex> create_subscription(%{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def create_subscription(attrs \\ %{}) do
|
|
%Subscription{}
|
|
|> Subscription.changeset(attrs)
|
|
|> Repo.insert()
|
|
end
|
|
|
|
@doc """
|
|
Updates a subscription.
|
|
|
|
## Examples
|
|
|
|
iex> update_subscription(subscription, %{field: new_value})
|
|
{:ok, %Subscription{}}
|
|
|
|
iex> update_subscription(subscription, %{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def update_subscription(%Subscription{} = subscription, attrs) do
|
|
subscription
|
|
|> Subscription.changeset(attrs)
|
|
|> Repo.update()
|
|
end
|
|
|
|
@doc """
|
|
Deletes a Subscription.
|
|
|
|
## Examples
|
|
|
|
iex> delete_subscription(subscription)
|
|
{:ok, %Subscription{}}
|
|
|
|
iex> delete_subscription(subscription)
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def delete_subscription(%Subscription{} = subscription) do
|
|
Repo.delete(subscription)
|
|
end
|
|
|
|
@doc """
|
|
Returns an `%Ecto.Changeset{}` for tracking subscription changes.
|
|
|
|
## Examples
|
|
|
|
iex> change_subscription(subscription)
|
|
%Ecto.Changeset{source: %Subscription{}}
|
|
|
|
"""
|
|
def change_subscription(%Subscription{} = subscription) do
|
|
Subscription.changeset(subscription, %{})
|
|
end
|
|
end
|