mobilizon.chapril.org-mobil.../lib/eventos_web/controllers/user_controller.ex

68 lines
1.7 KiB
Elixir
Raw Normal View History

defmodule EventosWeb.UserController do
@moduledoc """
Controller for Users
"""
use EventosWeb, :controller
alias Eventos.Actors
alias Eventos.Actors.User
alias Eventos.Repo
action_fallback EventosWeb.FallbackController
def index(conn, _params) do
users = Actors.list_users_with_actors()
render(conn, "index.json", users: users)
end
def register(conn, %{"username" => username, "email" => email, "password" => password}) do
with {:ok, %User{} = user} <- Actors.register(%{email: email, password: password, username: username}),
{:ok, token, _claims} <- EventosWeb.Guardian.encode_and_sign(user) do
conn
|> put_status(:created)
|> render("show_with_token.json", %{token: token, user: user})
end
end
def show_current_actor(conn, _params) do
user = Guardian.Plug.current_resource(conn)
user
|> Repo.preload(:actor)
render(conn, "show_simple.json", user: user)
end
defp handle_changeset_errors(errors) do
errors
|> Enum.map(fn {field, detail} ->
"#{field} " <> render_detail(detail)
end)
|> Enum.join
end
defp render_detail({message, values}) do
Enum.reduce values, message, fn {k, v}, acc ->
String.replace(acc, "%{#{k}}", to_string(v))
end
end
defp render_detail(message) do
message
end
def update(conn, %{"id" => id, "user" => user_params}) do
user = Actors.get_user!(id)
with {:ok, %User{} = user} <- Actors.update_user(user, user_params) do
render(conn, "show.json", user: user)
end
end
def delete(conn, %{"id" => id}) do
user = Actors.get_user!(id)
with {:ok, %User{}} <- Actors.delete_user(user) do
send_resp(conn, :no_content, "")
end
end
end