Commit 3cbe02147107129eae7c499ef1296e20b37ef2ea

Thomas de Grivel 2022-01-16T09:50:29

[admin] organisations: create, index, show repositories

diff --git a/lib/kmxgit/organisation_manager.ex b/lib/kmxgit/organisation_manager.ex
index 3909bc2..0a0954b 100644
--- a/lib/kmxgit/organisation_manager.ex
+++ b/lib/kmxgit/organisation_manager.ex
@@ -15,11 +15,18 @@ defmodule Kmxgit.OrganisationManager do
       order_by: s.slug
   end
 
+  def get_organisation(id) do
+    Repo.one from organisation in Organisation,
+      where: [id: ^id],
+      preload: [:slug,
+                owned_repositories: [organisation: :slug,
+                                     user: :slug],
+                users: :slug],
+      limit: 1
+  end
+
   def get_organisation!(id) do
-    org = Repo.one from org in Organisation,
-      where: org.id == ^id,
-      preload: [:slug, :users]
-    org || raise Ecto.NoResultsError
+    get_organisation(id) || raise Ecto.NoResultsError
   end
 
   def change_organisation(organisation \\ %Organisation{}) do
@@ -39,14 +46,6 @@ defmodule Kmxgit.OrganisationManager do
     |> Repo.update()
   end
 
-  def get_organisation(id) do
-    Repo.one from organisation in Organisation,
-      where: [id: ^id],
-      preload: :slug,
-      preload: [users: :slug],
-      limit: 1
-  end
-
   def get_organisation_by_slug(slug) do
     Repo.one from o in Organisation,
       join: s in Slug,
@@ -90,4 +89,10 @@ defmodule Kmxgit.OrganisationManager do
     |> Organisation.changeset(%{})
     |> Repo.delete()
   end
+
+  def admin_create_organisation(attrs \\ %{}) do
+    %Organisation{}
+    |> Organisation.changeset(attrs)
+    |> Repo.insert()
+  end
 end
diff --git a/lib/kmxgit_web/controllers/admin/organisation_controller.ex b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
index dace002..ac3da0b 100644
--- a/lib/kmxgit_web/controllers/admin/organisation_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
@@ -19,31 +19,23 @@ defmodule KmxgitWeb.Admin.OrganisationController do
     conn
     |> assign(:action, Routes.admin_organisation_path(conn, :create))
     |> assign(:changeset, changeset)
+    |> assign(:org, nil)
     |> render("new.html")
   end
 
   def create(conn, params) do
     current_user = conn.assigns.current_user
     org_params = params["organisation"]
-    Repo.transaction fn ->
-      case SlugManager.create_slug(org_params["slug"]["slug"]) do
-        {:ok, slug} ->
-          case OrganisationManager.create_organisation(Map.merge(org_params, %{slug: slug, user: current_user})) do
-            {:ok, org} ->
-              conn
-              |> redirect(to: Routes.admin_organisation_path(conn, :show, org))
-            {:error, changeset} ->
-              conn
-              |> assign(:action, Routes.admin_organisation_path(conn, :create))
-              |> assign(:changeset, changeset)
-              |> render("new.html")
-          end
-        {:error, changeset} ->
-          conn
-          |> assign(:action, Routes.admin_organisation_path(conn, :create))
-          |> assign(:changeset, changeset)
-          |> render("new.html")
-      end
+    case OrganisationManager.admin_create_organisation(org_params) do
+      {:ok, org} ->
+        conn
+        |> redirect(to: Routes.admin_organisation_path(conn, :show, org))
+      {:error, changeset} ->
+        conn
+        |> assign(:action, Routes.admin_organisation_path(conn, :create))
+        |> assign(:changeset, changeset)
+        |> assign(:org, nil)
+        |> render("new.html")
     end
   end
 
diff --git a/lib/kmxgit_web/templates/admin/organisation/edit.html.heex b/lib/kmxgit_web/templates/admin/organisation/edit.html.heex
index 303a3c0..107b1b1 100644
--- a/lib/kmxgit_web/templates/admin/organisation/edit.html.heex
+++ b/lib/kmxgit_web/templates/admin/organisation/edit.html.heex
@@ -1,35 +1,5 @@
 <div class="container-fluid">
   <h1><%= gettext("Edit organisation %{organisation}", organisation: @org.name || @org.slug.slug) %></h1>
 
-  <%= form_for @changeset, @action, fn f -> %>
-
-    <%= 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, :name, class: "form-label" %>
-      <%= text_input f, :name, class: "form-control" %>
-      <%= error_tag f, :name %>
-    </div>
-
-    <div class="mb-3">
-      <%= label f, :description, class: "form-label" %>
-      <%= textarea f, :description, class: "form-control" %>
-      <%= error_tag f, :description %>
-    </div>
-
-    <div class="mb-3">
-      <%= link gettext("Cancel"),
-          to: Routes.admin_organisation_path(@conn, :show, @org),
-          class: "btn btn-secondary" %>
-      <%= submit gettext("Submit"), class: "btn btn-primary" %>
-    </div>
-
-  <% end %>
-
+  <%= render("form.html", assigns) %>
 </div>
diff --git a/lib/kmxgit_web/templates/admin/organisation/form.html.heex b/lib/kmxgit_web/templates/admin/organisation/form.html.heex
new file mode 100644
index 0000000..f67a37b
--- /dev/null
+++ b/lib/kmxgit_web/templates/admin/organisation/form.html.heex
@@ -0,0 +1,30 @@
+<%= form_for @changeset, @action, fn f -> %>
+
+  <%= 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, :name, class: "form-label" %>
+    <%= text_input f, :name, class: "form-control" %>
+    <%= error_tag f, :name %>
+  </div>
+
+  <div class="mb-3">
+    <%= label f, :description, class: "form-label" %>
+    <%= textarea f, :description, class: "form-control" %>
+    <%= error_tag f, :description %>
+  </div>
+
+  <div class="mb-3">
+    <%= if @org do %>
+      <%= link gettext("Cancel"), to: Routes.admin_organisation_path(@conn, :show, @org), class: "btn btn-secondary" %>
+    <% end %>
+    <%= submit gettext("Submit"), class: "btn btn-primary" %>
+  </div>
+
+<% end %>
diff --git a/lib/kmxgit_web/templates/admin/organisation/index.html.heex b/lib/kmxgit_web/templates/admin/organisation/index.html.heex
index b878284..092578f 100644
--- a/lib/kmxgit_web/templates/admin/organisation/index.html.heex
+++ b/lib/kmxgit_web/templates/admin/organisation/index.html.heex
@@ -1,22 +1,32 @@
 <div class="container-fluid">
-  <h1><%= gettext "Organisations" %></h1>
-
+  <div class="row">
+    <div class="col col-12 col-md-7">
+      <h1><%= gettext "Organisations" %></h1>
+    </div>
+    <div class="col col-12 col-md-5">
+      <%= link gettext("Create"), to: Routes.admin_organisation_path(@conn, :new), class: "btn btn-primary" %>
+    </div>
+  </div>
   <table class="table admin-index">
-    <tr>
-      <th><%= gettext "Id" %></th>
-      <th><%= gettext "Name" %></th>
-      <th><%= gettext "Slug" %></th>
-      <th><%= gettext "Actions" %></th>
-    </tr>
-    <%= Enum.map @orgs, fn org -> %>
+    <thead>
       <tr>
-        <td><%= link org.id, to: Routes.admin_organisation_path(@conn, :show, org) %></td>
-        <td><%= link org.name, to: Routes.admin_organisation_path(@conn, :show, org) %></td>
-        <td><%= link org.slug.slug, to: Routes.admin_organisation_path(@conn, :show, org) %></td>
-        <td>
-          <%= link gettext("Show"), to: Routes.slug_path(@conn, :show, org.slug.slug), class: "btn btn-sm btn-primary" %>
-        </td>
+        <th><%= gettext "Id" %></th>
+        <th><%= gettext "Name" %></th>
+        <th><%= gettext "Slug" %></th>
+        <th><%= gettext "Actions" %></th>
       </tr>
-    <% end %>
+    </thead>
+    <tbody>
+      <%= Enum.map @orgs, fn org -> %>
+        <tr>
+          <td><%= link org.id, to: Routes.admin_organisation_path(@conn, :show, org) %></td>
+          <td><%= link org.name, to: Routes.admin_organisation_path(@conn, :show, org) %></td>
+          <td><%= link org.slug.slug, to: Routes.admin_organisation_path(@conn, :show, org) %></td>
+          <td>
+            <%= link gettext("Show"), to: Routes.slug_path(@conn, :show, org.slug.slug), class: "btn btn-sm btn-primary" %>
+          </td>
+        </tr>
+      <% end %>
+    </tbody>
   </table>
 </div>
diff --git a/lib/kmxgit_web/templates/admin/organisation/new.html.heex b/lib/kmxgit_web/templates/admin/organisation/new.html.heex
new file mode 100644
index 0000000..81f1dd8
--- /dev/null
+++ b/lib/kmxgit_web/templates/admin/organisation/new.html.heex
@@ -0,0 +1,5 @@
+<div class="container-fluid">
+  <h1><%= gettext("Create organisation") %></h1>
+
+  <%= render("form.html", assigns) %>
+</div>
diff --git a/lib/kmxgit_web/templates/admin/organisation/show.html.heex b/lib/kmxgit_web/templates/admin/organisation/show.html.heex
index 7022a7b..b0cf547 100644
--- a/lib/kmxgit_web/templates/admin/organisation/show.html.heex
+++ b/lib/kmxgit_web/templates/admin/organisation/show.html.heex
@@ -36,6 +36,18 @@
             class: "btn btn-primary btn-sm" %>
       </td>
     </tr>
+    <tr>
+      <th><%= gettext "Repositories" %></th>
+      <td>
+        <ul>
+          <%= for repo <- @org.owned_repositories do %>
+            <li>
+              <%= link(Repository.full_slug(repo), to: Routes.admin_repository_path(@conn, :show, repo), class: "repo") %>
+            </li>
+          <% end %>
+        </ul>
+      </td>
+    </tr>
   </table>
 
   <%= link gettext("Delete organisation"),
diff --git a/lib/kmxgit_web/views/admin/organisation_view.ex b/lib/kmxgit_web/views/admin/organisation_view.ex
index 2743368..acd51c5 100644
--- a/lib/kmxgit_web/views/admin/organisation_view.ex
+++ b/lib/kmxgit_web/views/admin/organisation_view.ex
@@ -1,3 +1,5 @@
 defmodule KmxgitWeb.Admin.OrganisationView do
   use KmxgitWeb, :view
+
+  alias Kmxgit.RepositoryManager.Repository
 end