typespec out contexts

This commit is contained in:
shibao 2022-01-31 20:10:48 -05:00
parent 616b20e62b
commit d549b732e1
4 changed files with 129 additions and 93 deletions

View File

@ -23,9 +23,7 @@ defmodule Cannery.Accounts do
""" """
@spec get_user_by_email(String.t()) :: User.t() | nil @spec get_user_by_email(String.t()) :: User.t() | nil
def get_user_by_email(email) when is_binary(email) do def get_user_by_email(email) when is_binary(email), do: Repo.get_by(User, email: email)
Repo.get_by(User, email: email)
end
@doc """ @doc """
Gets a user by email and password. Gets a user by email and password.
@ -61,13 +59,11 @@ defmodule Cannery.Accounts do
** (Ecto.NoResultsError) ** (Ecto.NoResultsError)
""" """
@spec get_user!(Ecto.UUID.t()) :: User.t() @spec get_user!(User.t()) :: User.t()
def get_user!(id), do: Repo.get!(User, id) def get_user!(id), do: Repo.get!(User, id)
@spec list_users_by_role(atom()) :: [User.t()] @spec list_users_by_role(atom()) :: [User.t()]
def list_users_by_role(role) do def list_users_by_role(role), do: Repo.all(from u in User, where: u.role == ^role)
Repo.all(from u in User, where: u.role == ^role)
end
@spec list_all_users(boolean()) :: [User.t()] @spec list_all_users(boolean()) :: [User.t()]
def list_all_users(confirmed_users_only \\ true) do def list_all_users(confirmed_users_only \\ true) do
@ -115,9 +111,8 @@ defmodule Cannery.Accounts do
""" """
@spec change_user_registration(User.t() | User.new_user()) :: Changeset.t() @spec change_user_registration(User.t() | User.new_user()) :: Changeset.t()
@spec change_user_registration(User.t() | User.new_user(), map()) :: Changeset.t() @spec change_user_registration(User.t() | User.new_user(), map()) :: Changeset.t()
def change_user_registration(user, attrs \\ %{}) do def change_user_registration(user, attrs \\ %{}),
User.registration_changeset(user, attrs, hash_password: false) do: User.registration_changeset(user, attrs, hash_password: false)
end
## Settings ## Settings
@ -131,9 +126,7 @@ defmodule Cannery.Accounts do
""" """
@spec change_user_email(User.t(), map()) :: Changeset.t() @spec change_user_email(User.t(), map()) :: Changeset.t()
def change_user_email(user, attrs \\ %{}) do def change_user_email(user, attrs \\ %{}), do: User.email_changeset(user, attrs)
User.email_changeset(user, attrs)
end
@doc """ @doc """
Returns an `%Changeset{}` for changing the user role. Returns an `%Changeset{}` for changing the user role.
@ -145,9 +138,7 @@ defmodule Cannery.Accounts do
""" """
@spec change_user_role(User.t(), atom()) :: Changeset.t() @spec change_user_role(User.t(), atom()) :: Changeset.t()
def change_user_role(user, role) do def change_user_role(user, role), do: User.role_changeset(user, role)
User.role_changeset(user, role)
end
@doc """ @doc """
Emulates that the email will change without actually changing Emulates that the email will change without actually changing
@ -228,9 +219,8 @@ defmodule Cannery.Accounts do
""" """
@spec change_user_password(User.t(), map()) :: Changeset.t() @spec change_user_password(User.t(), map()) :: Changeset.t()
def change_user_password(user, attrs \\ %{}) do def change_user_password(user, attrs \\ %{}),
User.password_changeset(user, attrs, hash_password: false) do: User.password_changeset(user, attrs, hash_password: false)
end
@doc """ @doc """
Updates the user password. Updates the user password.
@ -263,9 +253,7 @@ defmodule Cannery.Accounts do
end end
@spec delete_user!(User.t()) :: User.t() @spec delete_user!(User.t()) :: User.t()
def delete_user!(user) do def delete_user!(user), do: user |> Repo.delete!()
user |> Repo.delete!()
end
## Session ## Session
@ -350,6 +338,7 @@ defmodule Cannery.Accounts do
end end
end end
@spec confirm_user_multi(User.t()) :: Multi.t()
defp confirm_user_multi(user) do defp confirm_user_multi(user) do
Multi.new() Multi.new()
|> Multi.update(:user, User.confirm_changeset(user)) |> Multi.update(:user, User.confirm_changeset(user))

View File

@ -4,8 +4,9 @@ defmodule Cannery.Containers do
""" """
import Ecto.Query, warn: false import Ecto.Query, warn: false
alias Cannery.{Containers.Container, Repo} alias Cannery.{Containers.Container, Repo, Tags.Tag}
alias Ecto.{Changeset, UUID} alias Cannery.Containers.{Container, ContainerTag}
alias Ecto.{Changeset}
@doc """ @doc """
Returns the list of containers. Returns the list of containers.
@ -16,10 +17,9 @@ defmodule Cannery.Containers do
[%Container{}, ...] [%Container{}, ...]
""" """
@spec list_containers() :: [Container.t()] @spec list_containers(user_or_user_id :: User.t() | User.id()) :: [Container.t()]
def list_containers do def list_containers(%{id: user_id}), do: list_containers(user_id)
Repo.all(Container) def list_containers(user_id), do: Repo.all(from c in Container, where: c.user_id == ^user_id)
end
@doc """ @doc """
Gets a single container. Gets a single container.
@ -35,7 +35,7 @@ defmodule Cannery.Containers do
** (Ecto.NoResultsError) ** (Ecto.NoResultsError)
""" """
@spec get_container!(container_id :: UUID.t()) :: Container.t() @spec get_container!(Container.id()) :: Container.t()
def get_container!(id), do: Repo.get!(Container, id) def get_container!(id), do: Repo.get!(Container, id)
@doc """ @doc """
@ -67,7 +67,7 @@ defmodule Cannery.Containers do
{:error, %Ecto.Changeset{}} {:error, %Ecto.Changeset{}}
""" """
@spec update_container(Container.t() | Ecto.Changeset.t(), map()) :: @spec update_container(Container.t() | Ecto.Changeset.t(), attrs :: map()) ::
{:ok, Container.t()} | {:error, Ecto.Changeset.t()} {:ok, Container.t()} | {:error, Ecto.Changeset.t()}
def update_container(container, attrs) do def update_container(container, attrs) do
container |> Container.changeset(attrs) |> Repo.update() container |> Container.changeset(attrs) |> Repo.update()
@ -101,7 +101,54 @@ defmodule Cannery.Containers do
%Ecto.Changeset{data: %Container{}} %Ecto.Changeset{data: %Container{}}
""" """
@spec change_container(Container.t()) :: Changeset.t() @spec change_container(Container.t() | Container.new_container()) :: Changeset.t()
@spec change_container(Container.t(), map()) :: Changeset.t() @spec change_container(Container.t() | Container.new_container(), attrs :: map()) ::
Changeset.t()
def change_container(container, attrs \\ %{}), do: container |> Container.changeset(attrs) def change_container(container, attrs \\ %{}), do: container |> Container.changeset(attrs)
@doc """
Adds a tag to a container
## Examples
iex> add_tag!(container, tag)
%Container{}
iex> add_tag!(container_id, tag_id)
%Container{}
"""
@spec add_tag!(Container.t(), Tag.t()) :: Container.t()
def add_tag!(%{id: container_id}, %{id: tag_id}), do: add_tag!(container_id, tag_id)
@spec add_tag!(Container.id(), Tag.id()) :: Container.t()
def add_tag!(container_id, tag_id)
when not (container_id |> is_nil()) and not (tag_id |> is_nil()) do
%ContainerTag{}
|> ContainerTag.changeset(%{"container_id" => container_id, "tag_id" => tag_id})
|> Repo.insert!()
end
@doc """
Removes a tag from a container
## Examples
iex> remove_tag!(container, tag)
%Container{}
iex> remove_tag!(container_id, tag_id)
%Container{}
"""
@spec remove_tag!(Container.t(), Tag.t()) :: Container.t()
def remove_tag!(%{id: container_id}, %{id: tag_id}), do: remove_tag!(container_id, tag_id)
@spec remove_tag!(Container.id(), Tag.id()) :: Container.t()
def remove_tag!(container_id, tag_id)
when not (container_id |> is_nil()) and not (tag_id |> is_nil()) do
Repo.delete_all(
from ct in ContainerTag,
where: ct.container_id == ^container_id,
where: ct.tag_id == ^tag_id
)
end
end end

View File

@ -4,7 +4,7 @@ defmodule Cannery.Invites do
""" """
import Ecto.Query, warn: false import Ecto.Query, warn: false
alias Ecto.{Changeset, UUID} alias Ecto.{Changeset}
alias Cannery.{Accounts.User, Invites.Invite, Repo} alias Cannery.{Accounts.User, Invites.Invite, Repo}
@invite_token_length 20 @invite_token_length 20
@ -35,7 +35,7 @@ defmodule Cannery.Invites do
** (Ecto.NoResultsError) ** (Ecto.NoResultsError)
""" """
@spec get_invite!(Ecto.UUID.t()) :: Invite.t() @spec get_invite!(Invite.id()) :: Invite.t()
def get_invite!(id), do: Repo.get!(Invite, id) def get_invite!(id), do: Repo.get!(Invite, id)
@doc """ @doc """
@ -49,7 +49,7 @@ defmodule Cannery.Invites do
iex> get_invite_by_token("invalid_token") iex> get_invite_by_token("invalid_token")
nil nil
""" """
@spec get_invite_by_token(String.t() | nil) :: Invite.t() | nil @spec get_invite_by_token(token :: String.t() | nil) :: Invite.t() | nil
def get_invite_by_token(nil), do: nil def get_invite_by_token(nil), do: nil
def get_invite_by_token(""), do: nil def get_invite_by_token(""), do: nil
@ -96,22 +96,17 @@ defmodule Cannery.Invites do
{:error, %Ecto.Changeset{}} {:error, %Ecto.Changeset{}}
""" """
@spec create_invite(user :: User.t(), attrs :: map()) :: @spec create_invite(User.t() | User.id(), attrs :: map()) ::
{:ok, Invite.t()} | {:error, Changeset.t()} {:ok, Invite.t()} | {:error, Changeset.t()}
def create_invite(%{id: user_id}, attrs), do: create_invite(user_id, attrs) def create_invite(%{id: user_id}, attrs), do: create_invite(user_id, attrs)
@spec create_invite(user_id :: UUID.t(), attrs :: map()) ::
{:ok, Invite.t()} | {:error, Changeset.t()}
def create_invite(user_id, attrs) when not (user_id |> is_nil()) do def create_invite(user_id, attrs) when not (user_id |> is_nil()) do
attrs = token =
attrs :crypto.strong_rand_bytes(@invite_token_length)
|> Map.merge(%{ |> Base.url_encode64()
"user_id" => user_id, |> binary_part(0, @invite_token_length)
"token" =>
:crypto.strong_rand_bytes(@invite_token_length) attrs = attrs |> Map.merge(%{"user_id" => user_id, "token" => token})
|> Base.url_encode64()
|> binary_part(0, @invite_token_length)
})
%Invite{} |> Invite.changeset(attrs) |> Repo.insert() %Invite{} |> Invite.changeset(attrs) |> Repo.insert()
end end
@ -128,10 +123,9 @@ defmodule Cannery.Invites do
{:error, %Ecto.Changeset{}} {:error, %Ecto.Changeset{}}
""" """
@spec update_invite(Invite.t(), map()) :: {:ok, Invite.t()} | {:error, Ecto.Changeset.t()} @spec update_invite(Invite.t(), attrs :: map()) ::
def update_invite(invite, attrs) do {:ok, Invite.t()} | {:error, Ecto.Changeset.t()}
invite |> Invite.changeset(attrs) |> Repo.update() def update_invite(invite, attrs), do: invite |> Invite.changeset(attrs) |> Repo.update()
end
@doc """ @doc """
Deletes a invite. Deletes a invite.
@ -146,9 +140,19 @@ defmodule Cannery.Invites do
""" """
@spec delete_invite(Invite.t()) :: {:ok, Invite.t()} | {:error, Ecto.Changeset.t()} @spec delete_invite(Invite.t()) :: {:ok, Invite.t()} | {:error, Ecto.Changeset.t()}
def delete_invite(invite) do def delete_invite(invite), do: invite |> Repo.delete()
Repo.delete(invite)
end @doc """
Deletes a invite.
## Examples
iex> delete_invite(invite)
%Invite{}
"""
@spec delete_invite!(Invite.t()) :: Invite.t()
def delete_invite!(invite), do: invite |> Repo.delete!()
@doc """ @doc """
Returns an `%Ecto.Changeset{}` for tracking invite changes. Returns an `%Ecto.Changeset{}` for tracking invite changes.
@ -159,9 +163,7 @@ defmodule Cannery.Invites do
%Ecto.Changeset{data: %Invite{}} %Ecto.Changeset{data: %Invite{}}
""" """
@spec change_invite(Invite.t()) :: Ecto.Changeset.t() @spec change_invite(Invite.t() | Invite.new_invite()) :: Ecto.Changeset.t()
@spec change_invite(Invite.t(), map()) :: Ecto.Changeset.t() @spec change_invite(Invite.t() | Invite.new_invite(), attrs :: map()) :: Ecto.Changeset.t()
def change_invite(invite, attrs \\ %{}) do def change_invite(invite, attrs \\ %{}), do: invite |> Invite.changeset(attrs)
Invite.changeset(invite, attrs)
end
end end

View File

@ -4,9 +4,8 @@ defmodule Cannery.Tags do
""" """
import Ecto.Query, warn: false import Ecto.Query, warn: false
alias Cannery.{Accounts, Repo} alias Cannery.{Accounts.User, Repo, Tags.Tag}
alias Ecto.{Changeset}
alias Cannery.Tags.Tag
@doc """ @doc """
Returns the list of tags. Returns the list of tags.
@ -17,14 +16,9 @@ defmodule Cannery.Tags do
[%Tag{}, ...] [%Tag{}, ...]
""" """
@spec list_tags(Accounts.User.t()) :: [Tag.t()] @spec list_tags(User.t() | User.id()) :: [Tag.t()]
def list_tags(%{id: user_id}) do def list_tags(%{id: user_id}), do: list_tags(user_id)
list_tags(user_id) def list_tags(user_id), do: Repo.all(from t in Tag, where: t.user_id == ^user_id)
end
def list_tags(user_id) do
Repo.all(from t in Tag, where: t.user_id == ^user_id)
end
@doc """ @doc """
Gets a single tag. Gets a single tag.
@ -40,7 +34,7 @@ defmodule Cannery.Tags do
** (Ecto.NoResultsError) ** (Ecto.NoResultsError)
""" """
@spec get_tag!(Ecto.UUID.t()) :: Tag.t() @spec get_tag!(Tag.id()) :: Tag.t()
def get_tag!(id), do: Repo.get!(Tag, id) def get_tag!(id), do: Repo.get!(Tag, id)
@doc """ @doc """
@ -52,13 +46,11 @@ defmodule Cannery.Tags do
{:ok, %Tag{}} {:ok, %Tag{}}
iex> create_tag(%{field: bad_value}) iex> create_tag(%{field: bad_value})
{:error, %Ecto.Changeset{}} {:error, %Changeset{}}
""" """
@spec create_tag(map()) :: {:ok, Tag.t()} | {:error, Ecto.Changeset.t()} @spec create_tag(attrs :: map()) :: {:ok, Tag.t()} | {:error, Changeset.t()}
def create_tag(attrs) do def create_tag(attrs), do: %Tag{} |> Tag.changeset(attrs) |> Repo.insert()
%Tag{} |> Tag.changeset(attrs) |> Repo.insert()
end
@doc """ @doc """
Updates a tag. Updates a tag.
@ -69,13 +61,11 @@ defmodule Cannery.Tags do
{:ok, %Tag{}} {:ok, %Tag{}}
iex> update_tag(tag, %{field: bad_value}) iex> update_tag(tag, %{field: bad_value})
{:error, %Ecto.Changeset{}} {:error, %Changeset{}}
""" """
@spec update_tag(Tag.t(), map()) :: {:ok, Tag.t()} | {:error, Ecto.Changeset.t()} @spec update_tag(Tag.t(), attrs :: map()) :: {:ok, Tag.t()} | {:error, Changeset.t()}
def update_tag(tag, attrs) do def update_tag(tag, attrs), do: tag |> Tag.changeset(attrs) |> Repo.update()
tag |> Tag.changeset(attrs) |> Repo.update()
end
@doc """ @doc """
Deletes a tag. Deletes a tag.
@ -86,28 +76,36 @@ defmodule Cannery.Tags do
{:ok, %Tag{}} {:ok, %Tag{}}
iex> delete_tag(tag) iex> delete_tag(tag)
{:error, %Ecto.Changeset{}} {:error, %Changeset{}}
""" """
@spec delete_tag(Tag.t()) :: {:ok, Tag.t()} | {:error, Ecto.Changeset.t()} @spec delete_tag(Tag.t()) :: {:ok, Tag.t()} | {:error, Changeset.t()}
def delete_tag(tag) do def delete_tag(tag), do: tag |> Repo.delete()
Repo.delete(tag)
end
@doc """ @doc """
Returns an `%Ecto.Changeset{}` for tracking tag changes. Deletes a tag.
## Examples
iex> delete_tag!(tag)
%Tag{}
"""
@spec delete_tag!(Tag.t()) :: Tag.t()
def delete_tag!(tag), do: tag |> Repo.delete!()
@doc """
Returns an `%Changeset{}` for tracking tag changes.
## Examples ## Examples
iex> change_tag(tag) iex> change_tag(tag)
%Ecto.Changeset{data: %Tag{}} %Changeset{data: %Tag{}}
""" """
@spec change_tag(Tag.t()) :: Ecto.Changeset.t() @spec change_tag(Tag.t() | Tag.new_tag()) :: Changeset.t()
@spec change_tag(Tag.t(), map()) :: Ecto.Changeset.t() @spec change_tag(Tag.t() | Tag.new_tag(), attrs :: map()) :: Changeset.t()
def change_tag(tag, attrs \\ %{}) do def change_tag(tag, attrs \\ %{}), do: Tag.changeset(tag, attrs)
Tag.changeset(tag, attrs)
end
@doc """ @doc """
Get a random tag bg_color in `#ffffff` hex format Get a random tag bg_color in `#ffffff` hex format