this is a really good first commit

This commit is contained in:
2023-01-18 16:13:51 +01:00
parent 6bc40e339a
commit be71f04838
117 changed files with 2972 additions and 3100 deletions

View File

@ -90,21 +90,11 @@ defmodule SomethingErlang.Accounts do
"""
def change_user_registration(%User{} = user, attrs \\ %{}) do
User.registration_changeset(user, attrs, hash_password: false)
User.registration_changeset(user, attrs, hash_password: false, validate_email: false)
end
## Settings
def change_user_sadata(%User{} = user, attrs \\ %{}) do
User.sadata_changeset(user, attrs)
end
def update_sadata(%User{} = user, attrs \\ %{}) do
user
|> change_user_sadata(attrs)
|> Repo.update()
end
@doc """
Returns an `%Ecto.Changeset{}` for changing the user email.
@ -115,7 +105,7 @@ defmodule SomethingErlang.Accounts do
"""
def change_user_email(user, attrs \\ %{}) do
User.email_changeset(user, attrs)
User.email_changeset(user, attrs, validate_email: false)
end
@doc """
@ -167,16 +157,16 @@ defmodule SomethingErlang.Accounts do
|> Ecto.Multi.delete_all(:tokens, UserToken.user_and_contexts_query(user, [context]))
end
@doc """
@doc ~S"""
Delivers the update email instructions to the given user.
## Examples
iex> deliver_update_email_instructions(user, current_email, &Routes.user_update_email_url(conn, :edit, &1))
iex> deliver_user_update_email_instructions(user, current_email, &url(~p"/users/settings/confirm_email/#{&1})")
{:ok, %{to: ..., body: ...}}
"""
def deliver_update_email_instructions(%User{} = user, current_email, update_email_url_fun)
def deliver_user_update_email_instructions(%User{} = user, current_email, update_email_url_fun)
when is_function(update_email_url_fun, 1) do
{encoded_token, user_token} = UserToken.build_email_token(user, "change:#{current_email}")
@ -247,22 +237,22 @@ defmodule SomethingErlang.Accounts do
@doc """
Deletes the signed token with the given context.
"""
def delete_session_token(token) do
def delete_user_session_token(token) do
Repo.delete_all(UserToken.token_and_context_query(token, "session"))
:ok
end
## Confirmation
@doc """
@doc ~S"""
Delivers the confirmation email instructions to the given user.
## Examples
iex> deliver_user_confirmation_instructions(user, &Routes.user_confirmation_url(conn, :edit, &1))
iex> deliver_user_confirmation_instructions(user, &url(~p"/users/confirm/#{&1}"))
{:ok, %{to: ..., body: ...}}
iex> deliver_user_confirmation_instructions(confirmed_user, &Routes.user_confirmation_url(conn, :edit, &1))
iex> deliver_user_confirmation_instructions(confirmed_user, &url(~p"/users/confirm/#{&1}"))
{:error, :already_confirmed}
"""
@ -301,12 +291,12 @@ defmodule SomethingErlang.Accounts do
## Reset password
@doc """
@doc ~S"""
Delivers the reset password email to the given user.
## Examples
iex> deliver_user_reset_password_instructions(user, &Routes.user_reset_password_url(conn, :edit, &1))
iex> deliver_user_reset_password_instructions(user, &url(~p"/users/reset_password/#{&1}"))
{:ok, %{to: ..., body: ...}}
"""

View File

@ -8,20 +8,9 @@ defmodule SomethingErlang.Accounts.User do
field :hashed_password, :string, redact: true
field :confirmed_at, :naive_datetime
field :bbuserid, :string
field :bbpassword, :string
timestamps()
end
@doc """
A user changeset for SA data.
"""
def sadata_changeset(user, attrs, _opts \\ []) do
user
|> cast(attrs, [:bbuserid, :bbpassword])
end
@doc """
A user changeset for registration.
@ -38,21 +27,26 @@ defmodule SomethingErlang.Accounts.User do
password field is not desired (like when using this changeset for
validations on a LiveView form), this option can be set to `false`.
Defaults to `true`.
* `:validate_email` - Validates the uniqueness of the email, in case
you don't want to validate the uniqueness of the email (like when
using this changeset for validations on a LiveView form before
submitting the form), this option can be set to `false`.
Defaults to `true`.
"""
def registration_changeset(user, attrs, opts \\ []) do
user
|> cast(attrs, [:email, :password])
|> validate_email()
|> validate_email(opts)
|> validate_password(opts)
end
defp validate_email(changeset) do
defp validate_email(changeset, opts) do
changeset
|> validate_required([:email])
|> validate_format(:email, ~r/^[^\s]+@[^\s]+$/, message: "must have the @ sign and no spaces")
|> validate_length(:email, max: 160)
|> unsafe_validate_unique(:email, SomethingErlang.Repo)
|> unique_constraint(:email)
|> maybe_validate_unique_email(opts)
end
defp validate_password(changeset, opts) do
@ -80,15 +74,25 @@ defmodule SomethingErlang.Accounts.User do
end
end
defp maybe_validate_unique_email(changeset, opts) do
if Keyword.get(opts, :validate_email, true) do
changeset
|> unsafe_validate_unique(:email, SomethingErlang.Repo)
|> unique_constraint(:email)
else
changeset
end
end
@doc """
A user changeset for changing the email.
It requires the email to change otherwise an error is added.
"""
def email_changeset(user, attrs) do
def email_changeset(user, attrs, opts \\ []) do
user
|> cast(attrs, [:email])
|> validate_email()
|> validate_email(opts)
|> case do
%{changes: %{email: _}} = changeset -> changeset
%{} = changeset -> add_error(changeset, :email, "did not change")

View File

@ -8,14 +8,14 @@ defmodule SomethingErlang.Application do
@impl true
def start(_type, _args) do
children = [
{Registry, [name: SomethingErlang.Registry.Grovers, keys: :unique]},
{DynamicSupervisor, [name: SomethingErlang.Supervisor.Grovers, strategy: :one_for_one]},
# Start the Ecto repository
SomethingErlang.Repo,
# Start the Telemetry supervisor
SomethingErlangWeb.Telemetry,
# Start the Ecto repository
SomethingErlang.Repo,
# Start the PubSub system
{Phoenix.PubSub, name: SomethingErlang.PubSub},
# Start Finch
{Finch, name: SomethingErlang.Finch},
# Start the Endpoint (http/https)
SomethingErlangWeb.Endpoint
# Start a worker by calling: SomethingErlang.Worker.start_link(arg)

View File

@ -1,106 +0,0 @@
defmodule SomethingErlang.Forums do
@moduledoc """
The Forums context.
"""
import Ecto.Query, warn: false
alias SomethingErlang.Repo
alias SomethingErlang.Forums.Thread
@doc """
Returns the list of threads.
## Examples
iex> list_threads()
[%Thread{}, ...]
"""
def list_threads do
Repo.all(Thread)
end
@doc """
Gets a single thread.
Raises `Ecto.NoResultsError` if the Thread does not exist.
## Examples
iex> get_thread!(123)
%Thread{}
iex> get_thread!(456)
** (Ecto.NoResultsError)
"""
def get_thread!(id),
# Repo.get!(Thread, id)
do: %Thread{id: id, thread_id: id, title: "foo"}
@doc """
Creates a thread.
## Examples
iex> create_thread(%{field: value})
{:ok, %Thread{}}
iex> create_thread(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_thread(attrs \\ %{}) do
%Thread{}
|> Thread.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a thread.
## Examples
iex> update_thread(thread, %{field: new_value})
{:ok, %Thread{}}
iex> update_thread(thread, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_thread(%Thread{} = thread, attrs) do
thread
|> Thread.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a thread.
## Examples
iex> delete_thread(thread)
{:ok, %Thread{}}
iex> delete_thread(thread)
{:error, %Ecto.Changeset{}}
"""
def delete_thread(%Thread{} = thread) do
Repo.delete(thread)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking thread changes.
## Examples
iex> change_thread(thread)
%Ecto.Changeset{data: %Thread{}}
"""
def change_thread(%Thread{} = thread, attrs \\ %{}) do
Thread.changeset(thread, attrs)
end
end

View File

@ -1,18 +0,0 @@
defmodule SomethingErlang.Forums.Thread do
use Ecto.Schema
import Ecto.Changeset
schema "threads" do
field :thread_id, :integer
field :title, :string
timestamps()
end
@doc false
def changeset(thread, attrs) do
thread
|> cast(attrs, [:title, :thread_id])
|> validate_required([:title, :thread_id])
end
end