Hash :
1f16488e
Author :
Thomas de Grivel
Date :
2021-11-29T07:27:06
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
defmodule KmxgitWeb.UserController do
use KmxgitWeb, :controller
alias Kmxgit.GitManager
alias Kmxgit.Repo
alias Kmxgit.UserManager
alias Kmxgit.UserManager.User
alias KmxgitWeb.ErrorView
defp not_found(conn) do
conn
|> put_status(:not_found)
|> put_view(ErrorView)
|> render(:"404")
end
def edit(conn, params) do
current_user = conn.assigns.current_user
if params["login"] == current_user.slug.slug do
changeset = User.changeset(current_user)
conn
|> assign(:changeset, changeset)
|> assign(:page_title, gettext("Edit user %{login}", login: current_user.slug.slug))
|> render("edit.html")
else
not_found(conn)
end
end
def update(conn, params) do
current_user = conn.assigns.current_user
if params["login"] == current_user.slug.slug do
case Repo.transaction(fn ->
case UserManager.update_user(current_user, params["user"]) do
{:ok, user} ->
if user.slug.slug != current_user.slug.slug do
case GitManager.rename_dir(current_user.slug.slug, user.slug.slug) do
:ok -> user
{:error, err} -> Repo.rollback(err)
end
else
user
end
{:error, changeset} -> Repo.rollback(changeset)
end
end) do
{:ok, user} ->
conn
|> redirect(to: Routes.slug_path(conn, :show, user.slug.slug))
{:error, changeset} ->
conn
|> assign(:page_title, gettext("Edit user %{login}", login: current_user.slug.slug))
|> assign(:changeset, changeset)
|> render("edit.html")
end
else
not_found(conn)
end
end
def delete(conn, params) do
current_user = conn.assigns.current_user
if params["login"] == current_user.slug.slug do
case Repo.transaction(fn ->
case UserManager.delete_user(current_user) do
{:ok, _} ->
case GitManager.delete_dir(current_user.slug.slug) do
:ok -> :ok
{:error, out} -> Repo.rollback(status: out)
end
{:error, e} -> Repo.rollback(e)
end
end) do
{:ok, _} ->
conn
|> redirect(to: "/")
{:error, changeset} ->
conn
|> assign(:changeset, changeset)
|> assign(:page_title, gettext("Edit user %{login}", login: current_user.slug.slug))
|> render("edit.html")
end
else
not_found(conn)
end
end
end