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