Hash :
c1d220d5
Author :
Thomas de Grivel
Date :
2021-11-18T16:58:30
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
defmodule KmxgitWeb.Admin.UserController do
use KmxgitWeb, :controller
alias Kmxgit.UserManager
alias KmxgitWeb.ErrorView
def index(conn, _params) do
users = UserManager.list_users
conn
|> assign(:page_title, gettext("Users"))
|> assign(:users, users)
|> render("index.html")
end
def show(conn, params) do
user = UserManager.get_user(params["id"])
show_user(conn, user)
end
defp not_found(conn) do
conn
|> put_status(:not_found)
|> put_view(ErrorView)
|> render(:"404")
end
defp show_user(conn, nil) do
not_found(conn)
end
defp show_user(conn, user) do
conn
|> assign(:page_title, gettext("User %{login}", login: user.slug.slug))
|> assign(:user, user)
|> render("show.html")
end
def edit(conn, params) do
user = UserManager.get_user(params["id"])
edit_user(conn, user)
end
defp edit_user(conn, nil) do
not_found(conn)
end
defp edit_user(conn, user) do
changeset = UserManager.change_user(user)
conn
|> render("edit.html", user: user, changeset: changeset)
end
def update(conn, params) do
user = UserManager.get_user(params["id"])
update_user(conn, user, params)
end
defp update_user(conn, nil, _params) do
not_found(conn)
end
defp update_user(conn, user, params) do
case UserManager.admin_update_user(user, params["user"]) do
{:ok, _updated_user} ->
conn
|> redirect(to: Routes.admin_user_path(conn, :show, user))
{:error, changeset} ->
conn
|> render("edit.html", changeset: changeset)
end
end
def delete(conn, params) do
user = UserManager.get_user(params["id"])
delete_user(conn, user)
end
defp delete_user(conn, nil) do
not_found(conn)
end
defp delete_user(conn, user) do
{:ok, _} = UserManager.delete_user(user)
conn
|> redirect(to: Routes.admin_user_path(conn, :index))
end
end