mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-24 20:37:59 +01:00
104 lines
1.6 KiB
Elixir
104 lines
1.6 KiB
Elixir
defmodule Philomena.Roles do
|
|
@moduledoc """
|
|
The Roles context.
|
|
"""
|
|
|
|
import Ecto.Query, warn: false
|
|
alias Philomena.Repo
|
|
|
|
alias Philomena.Roles.Role
|
|
|
|
@doc """
|
|
Returns the list of roles.
|
|
|
|
## Examples
|
|
|
|
iex> list_roles()
|
|
[%Role{}, ...]
|
|
|
|
"""
|
|
def list_roles do
|
|
Repo.all(Role)
|
|
end
|
|
|
|
@doc """
|
|
Gets a single role.
|
|
|
|
Raises `Ecto.NoResultsError` if the Role does not exist.
|
|
|
|
## Examples
|
|
|
|
iex> get_role!(123)
|
|
%Role{}
|
|
|
|
iex> get_role!(456)
|
|
** (Ecto.NoResultsError)
|
|
|
|
"""
|
|
def get_role!(id), do: Repo.get!(Role, id)
|
|
|
|
@doc """
|
|
Creates a role.
|
|
|
|
## Examples
|
|
|
|
iex> create_role(%{field: value})
|
|
{:ok, %Role{}}
|
|
|
|
iex> create_role(%{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def create_role(attrs \\ %{}) do
|
|
%Role{}
|
|
|> Role.changeset(attrs)
|
|
|> Repo.insert()
|
|
end
|
|
|
|
@doc """
|
|
Updates a role.
|
|
|
|
## Examples
|
|
|
|
iex> update_role(role, %{field: new_value})
|
|
{:ok, %Role{}}
|
|
|
|
iex> update_role(role, %{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def update_role(%Role{} = role, attrs) do
|
|
role
|
|
|> Role.changeset(attrs)
|
|
|> Repo.update()
|
|
end
|
|
|
|
@doc """
|
|
Deletes a Role.
|
|
|
|
## Examples
|
|
|
|
iex> delete_role(role)
|
|
{:ok, %Role{}}
|
|
|
|
iex> delete_role(role)
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def delete_role(%Role{} = role) do
|
|
Repo.delete(role)
|
|
end
|
|
|
|
@doc """
|
|
Returns an `%Ecto.Changeset{}` for tracking role changes.
|
|
|
|
## Examples
|
|
|
|
iex> change_role(role)
|
|
%Ecto.Changeset{source: %Role{}}
|
|
|
|
"""
|
|
def change_role(%Role{} = role) do
|
|
Role.changeset(role, %{})
|
|
end
|
|
end
|