tusker_push/lib/tusker_push.ex

62 lines
1.7 KiB
Elixir
Raw Normal View History

2024-04-05 18:23:00 +00:00
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.
"""
2024-04-05 19:04:38 +00:00
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
2024-04-07 14:56:23 +00:00
@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
2024-04-05 19:04:38 +00:00
@spec unregister(String.t()) :: :ok | {:error, :no_registration | Ecto.Changeset.t()}
2024-04-05 22:26:10 +00:00
def unregister(id) when is_binary(id) do
2024-04-05 19:04:38 +00:00
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
2024-04-05 22:26:10 +00:00
@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
2024-04-05 18:23:00 +00:00
end