Commit c6f292d6b869ea36b422c70eab0035e5998fe269

Thomas de Grivel 2022-07-07T09:57:12

[new_admin] slug_

diff --git a/lib/kmxgit_web/controllers/page_controller.ex b/lib/kmxgit_web/controllers/page_controller.ex
index 8322694..02a97c5 100644
--- a/lib/kmxgit_web/controllers/page_controller.ex
+++ b/lib/kmxgit_web/controllers/page_controller.ex
@@ -5,8 +5,10 @@ defmodule KmxgitWeb.PageController do
 
   alias Kmxgit.Git
   alias Kmxgit.OrganisationManager
+  alias Kmxgit.Repo
   alias Kmxgit.RepositoryManager
   alias Kmxgit.RepositoryManager.Repository
+  alias Kmxgit.SlugManager
   alias Kmxgit.UserManager
   alias Kmxgit.UserManager.User
   alias KmxgitWeb.UserAuth
@@ -70,17 +72,27 @@ defmodule KmxgitWeb.PageController do
   def new_admin_post(conn, params) do
     if ! UserManager.admin_user_present? do
       user_params = Map.merge(params["user"], %{"is_admin" => true})
-      case UserManager.admin_create_user(user_params) do
+      case Repo.transaction(fn ->
+            case UserManager.admin_create_user(user_params) do
+              {:ok, user} ->
+                case SlugManager.create_slug(user) do
+                  {:ok, _slug} -> user
+                  {:error, changeset} -> Repo.rollback(changeset)
+                end
+              {:error, changeset} ->
+                Repo.rollback(changeset)
+            end
+          end) do
         {:ok, user} ->
           conn
           |> UserAuth.log_in_user(user, user_params)
           |> redirect(to: "/")
-          {:error, changeset} ->
-            conn
-            |> assign(:no_navbar_links, true)
-            |> assign(:changeset, changeset)
-            |> assign(:action, Routes.page_path(conn, :new_admin))
-            |> render("new_admin.html")
+        {:error, changeset} ->
+          conn
+          |> assign(:no_navbar_links, true)
+          |> assign(:changeset, changeset)
+          |> assign(:action, Routes.page_path(conn, :new_admin))
+          |> render("new_admin.html")
       end
     else
       redirect(conn, to: "/")
diff --git a/lib/kmxgit_web/templates/page/new_admin.html.heex b/lib/kmxgit_web/templates/page/new_admin.html.heex
index 9901753..9c3364e 100644
--- a/lib/kmxgit_web/templates/page/new_admin.html.heex
+++ b/lib/kmxgit_web/templates/page/new_admin.html.heex
@@ -23,13 +23,11 @@
       <%= error_tag f, :email %>
     </div>
 
-    <%= inputs_for f, :slug, fn ff -> %>
-      <div class="mb-3">
-        <%= label ff, :slug, class: "form-label" %>
-        <%= text_input ff, :slug, class: "form-control" %>
-        <%= error_tag ff, :slug %>
-      </div>
-    <% end %>
+    <div class="mb-3">
+      <%= label f, :slug_, class: "form-label" %>
+      <%= text_input f, :slug_, class: "form-control" %>
+      <%= error_tag f, :slug_ %>
+    </div>
 
     <div class="mb-3">
       <%= label f, :password, class: "form-label" %>