fp/services/bright/lib/bright_web/controllers/user_controller.ex

60 lines
1.4 KiB
Elixir
Raw Normal View History

2025-01-16 20:23:58 +00:00
defmodule BrightWeb.UserController do
use BrightWeb, :controller
2025-01-20 10:01:08 +00:00
alias Bright.{User, Repo}
require Logger
2025-01-16 20:23:58 +00:00
def index(conn, _params) do
render(conn, :index)
end
2025-01-20 10:01:08 +00:00
# def show(conn) do
# conn
# |> render(:show)
# end
2025-01-16 20:23:58 +00:00
2025-01-20 10:01:08 +00:00
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)
2025-01-16 20:23:58 +00:00
end
2025-01-20 10:01:08 +00:00
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
2025-01-16 20:23:58 +00:00
end