297 lines
4.6 KiB
Elixir
297 lines
4.6 KiB
Elixir
defmodule Homeman.Accounts do
|
|
@moduledoc """
|
|
The Accounts context.
|
|
"""
|
|
|
|
import Ecto.Query, warn: false
|
|
alias Homeman.Repo
|
|
|
|
alias Homeman.Accounts.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{data: %User{}}
|
|
|
|
"""
|
|
def change_user(%User{} = user, attrs \\ %{}) do
|
|
User.changeset(user, attrs)
|
|
end
|
|
|
|
alias Homeman.Accounts.Todo
|
|
|
|
@doc """
|
|
Returns the list of todos.
|
|
|
|
## Examples
|
|
|
|
iex> list_todos()
|
|
[%Todo{}, ...]
|
|
|
|
"""
|
|
def list_todos do
|
|
Repo.all(Todo)
|
|
end
|
|
|
|
@doc """
|
|
Gets a single todo.
|
|
|
|
Raises `Ecto.NoResultsError` if the Todo does not exist.
|
|
|
|
## Examples
|
|
|
|
iex> get_todo!(123)
|
|
%Todo{}
|
|
|
|
iex> get_todo!(456)
|
|
** (Ecto.NoResultsError)
|
|
|
|
"""
|
|
def get_todo!(id), do: Repo.get!(Todo, id)
|
|
|
|
@doc """
|
|
Creates a todo.
|
|
|
|
## Examples
|
|
|
|
iex> create_todo(%{field: value})
|
|
{:ok, %Todo{}}
|
|
|
|
iex> create_todo(%{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def create_todo(attrs \\ %{}) do
|
|
%Todo{}
|
|
|> Todo.changeset(attrs)
|
|
|> Repo.insert()
|
|
end
|
|
|
|
@doc """
|
|
Updates a todo.
|
|
|
|
## Examples
|
|
|
|
iex> update_todo(todo, %{field: new_value})
|
|
{:ok, %Todo{}}
|
|
|
|
iex> update_todo(todo, %{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def update_todo(%Todo{} = todo, attrs) do
|
|
todo
|
|
|> Todo.changeset(attrs)
|
|
|> Repo.update()
|
|
end
|
|
|
|
@doc """
|
|
Deletes a todo.
|
|
|
|
## Examples
|
|
|
|
iex> delete_todo(todo)
|
|
{:ok, %Todo{}}
|
|
|
|
iex> delete_todo(todo)
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def delete_todo(%Todo{} = todo) do
|
|
Repo.delete(todo)
|
|
end
|
|
|
|
@doc """
|
|
Returns an `%Ecto.Changeset{}` for tracking todo changes.
|
|
|
|
## Examples
|
|
|
|
iex> change_todo(todo)
|
|
%Ecto.Changeset{data: %Todo{}}
|
|
|
|
"""
|
|
def change_todo(%Todo{} = todo, attrs \\ %{}) do
|
|
Todo.changeset(todo, attrs)
|
|
end
|
|
|
|
alias Homeman.Accounts.Task
|
|
|
|
@doc """
|
|
Returns the list of tasks.
|
|
|
|
## Examples
|
|
|
|
iex> list_tasks()
|
|
[%Task{}, ...]
|
|
|
|
"""
|
|
def list_tasks do
|
|
Repo.all(Task)
|
|
end
|
|
|
|
@doc """
|
|
Gets a single task.
|
|
|
|
Raises `Ecto.NoResultsError` if the Task does not exist.
|
|
|
|
## Examples
|
|
|
|
iex> get_task!(123)
|
|
%Task{}
|
|
|
|
iex> get_task!(456)
|
|
** (Ecto.NoResultsError)
|
|
|
|
"""
|
|
def get_task!(id), do: Repo.get!(Task, id)
|
|
|
|
@doc """
|
|
Creates a task.
|
|
|
|
## Examples
|
|
|
|
iex> create_task(%{field: value})
|
|
{:ok, %Task{}}
|
|
|
|
iex> create_task(%{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def create_task(attrs \\ %{}) do
|
|
%Task{}
|
|
|> Task.changeset(attrs)
|
|
|> Repo.insert()
|
|
end
|
|
|
|
@doc """
|
|
Updates a task.
|
|
|
|
## Examples
|
|
|
|
iex> update_task(task, %{field: new_value})
|
|
{:ok, %Task{}}
|
|
|
|
iex> update_task(task, %{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def update_task(%Task{} = task, attrs) do
|
|
task
|
|
|> Task.changeset(attrs)
|
|
|> Repo.update()
|
|
end
|
|
|
|
@doc """
|
|
Deletes a task.
|
|
|
|
## Examples
|
|
|
|
iex> delete_task(task)
|
|
{:ok, %Task{}}
|
|
|
|
iex> delete_task(task)
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def delete_task(%Task{} = task) do
|
|
Repo.delete(task)
|
|
end
|
|
|
|
@doc """
|
|
Returns an `%Ecto.Changeset{}` for tracking task changes.
|
|
|
|
## Examples
|
|
|
|
iex> change_task(task)
|
|
%Ecto.Changeset{data: %Task{}}
|
|
|
|
"""
|
|
def change_task(%Task{} = task, attrs \\ %{}) do
|
|
Task.changeset(task, attrs)
|
|
end
|
|
end
|