Hash :
9b177275
Author :
Thomas de Grivel
Date :
2023-10-17T11:36:07
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
## kmxgit
## Copyright 2022 kmx.io <contact@kmx.io>
##
## Permission is hereby granted to use this software granted
## the above copyright notice and this permission paragraph
## are included in all copies and substantial portions of this
## software.
##
## THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
## PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
## AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
## THIS SOFTWARE.
defmodule KmxgitWeb.UserRegistrationController do
use KmxgitWeb, :controller
alias Kmxgit.Repo
alias Kmxgit.SlugManager
alias Kmxgit.UserManager
alias Kmxgit.UserManager.User
alias KmxgitWeb.UserAuth
def new(conn, _params) do
changeset = UserManager.change_user_registration(%User{})
render(conn, "new.html", changeset: changeset)
end
defp merge_errors(changeset, errors) do
%Ecto.Changeset{changeset | errors: changeset.errors ++ errors,
action: :insert,
valid?: false}
end
def create(conn, %{"user" => user_params}) do
case Repo.transaction(fn ->
error_changeset = %User{}
|> User.registration_changeset(user_params)
case UserManager.register_user(user_params) do
{:ok, user} ->
case SlugManager.create_slug(user) do
{:ok, _slug} ->
{:ok, _} =
UserManager.deliver_user_confirmation_instructions(
user,
&Routes.user_confirmation_url(conn, :edit, &1))
user
{:error, changeset} ->
IO.inspect(changeset)
error_changeset
|> merge_errors(changeset.errors)
|> Repo.rollback()
end
{:error, changeset} ->
IO.inspect(changeset)
Repo.rollback(changeset)
end
end) do
{:ok, user} ->
conn
|> put_flash(:info, "User created successfully.")
|> UserAuth.log_in_user(user)
{:error, changeset} ->
IO.inspect(changeset)
render(conn, "new.html", changeset: changeset)
end
end
end