60 lines
1.4 KiB
Elixir
60 lines
1.4 KiB
Elixir
defmodule BrightWeb.UserController do
|
|
use BrightWeb, :controller
|
|
alias Bright.{User, Repo}
|
|
require Logger
|
|
|
|
def index(conn, _params) do
|
|
render(conn, :index)
|
|
end
|
|
|
|
# def show(conn) do
|
|
# conn
|
|
# |> render(:show)
|
|
# end
|
|
|
|
def show(conn = %{assigns: %{current_user: me}}, _params) do
|
|
Logger.info(">>> me=#{inspect(me)}")
|
|
render(conn, :show, changeset: User.update_changeset(me))
|
|
end
|
|
|
|
# def show(conn) do
|
|
# user = User.get_user!(id)
|
|
# render(conn, :show, user: user)
|
|
# end
|
|
|
|
|
|
|
|
def join(conn = %{method: "GET"}, params) do
|
|
user = %User{
|
|
name: Map.get(params, "name"),
|
|
github_handle: Map.get(params, "github_handle"),
|
|
patreon_handle: Map.get(params, "patreon_handle")
|
|
}
|
|
|
|
render(conn, :join, changeset: User.insert_changeset(user), user: nil)
|
|
end
|
|
|
|
def join(conn = %{method: "POST"}, params = %{"user" => user_params}) do
|
|
changeset = User.insert_changeset(%User{}, user_params)
|
|
|
|
case Repo.insert(changeset) do
|
|
{:ok, user} ->
|
|
welcome_community(conn, user)
|
|
|
|
{:error, changeset} ->
|
|
conn
|
|
|> put_flash(:error, "Something went wrong. 😭")
|
|
|> render(:join, changeset: changeset, user: nil)
|
|
end
|
|
end
|
|
|
|
defp welcome_community(conn, user) do
|
|
user = User.refresh_auth_token(user)
|
|
|
|
conn
|
|
|> put_flash(:success, "Welcome #{user}")
|
|
|> redirect(to: ~p"/")
|
|
end
|
|
|
|
end
|