tusker_push/lib/tusker_push.ex

62 lines
1.7 KiB
Elixir

defmodule TuskerPush do
@moduledoc """
TuskerPush keeps the contexts that define your domain
and business logic.
Contexts are also responsible for managing your data, regardless
if it comes from the database, an external API or others.
"""
alias TuskerPush.Registration
alias TuskerPush.Repo
@spec register(Map.t()) ::
{:ok, Registration.t()} | {:error, Ecto.Changeset.t()}
def register(params) do
params
|> Registration.create_changeset()
|> Repo.insert()
end
@spec update_registration(Registration.t(), Map.t()) ::
{:ok, Registration.t()} | {:error, Ecto.Changeset.t()}
def update_registration(registration, params) do
registration
|> Registration.update_changeset(params)
|> Repo.update()
end
@spec unregister(String.t()) :: :ok | {:error, :no_registration | Ecto.Changeset.t()}
def unregister(id) when is_binary(id) do
with registration when not is_nil(registration) <- Repo.get(Registration, id),
{:ok, _} <- Repo.delete(registration) do
:ok
else
nil ->
{:error, :no_registration}
{:error, reason} ->
{:error, reason}
end
end
@spec unregister(Registration.t()) :: :ok | {:error, Ecto.Changeset.t()}
def unregister(%Registration{} = registration) do
case Repo.delete(registration) do
{:ok, _} -> :ok
{:error, reason} -> {:error, reason}
end
end
@spec get_registration(String.t()) :: Registration.t() | nil
def get_registration(id) do
Repo.get(Registration, id)
end
@spec check_registration_expired(Registration.t()) :: :ok | {:expired, Registration.t()}
def check_registration_expired(_registration) do
# TODO: expiration & grace period
:ok
end
end