Commit 30d1749c2ccf4e9d9f61d2e8c9f15abaac305dae

Thomas de Grivel 2021-11-22T13:59:31

admin

diff --git a/lib/kmxgit/repository_manager.ex b/lib/kmxgit/repository_manager.ex
index 5ff2673..936ba10 100644
--- a/lib/kmxgit/repository_manager.ex
+++ b/lib/kmxgit/repository_manager.ex
@@ -42,11 +42,16 @@ defmodule Kmxgit.RepositoryManager do
   end
 
   def get_repository(id) do
-    Repo.one from repository in Repository,
-      where: [id: ^id],
-      preload: :slug,
-      preload: [users: :slug],
-      limit: 1
+    Repo.one(from repo in Repository,
+      where: repo.id == ^id,
+      limit: 1,
+      preload: [members: :slug,
+                organisation: [:slug, [users: :slug]],
+                user: :slug])
+  end
+
+  def get_repository!(id) do
+    get_repository(id) || raise Ecto.NoResultsError
   end
 
   def get_repository_by_owner_and_slug(owner, slug) do
@@ -63,7 +68,7 @@ defmodule Kmxgit.RepositoryManager do
       on: us.user_id == u.id,
       where: (fragment("lower(?)", os.slug) == ^downcase_owner or fragment("lower(?)", us.slug) == ^downcase_owner) and fragment("lower(?)", r.slug) == ^downcase_slug,
       preload: [members: :slug,
-                organisation: [:slug, :users],
+                organisation: [:slug, [users: :slug]],
                 user: :slug]
   end
 
@@ -71,6 +76,7 @@ defmodule Kmxgit.RepositoryManager do
     user = UserManager.get_user_by_slug(login)
     if user do
       members = [user | repo.members]
+      IO.inspect(members)
       repo
       |> Repository.changeset(%{})
       |> Ecto.Changeset.put_assoc(:members, members)
diff --git a/lib/kmxgit_web/controllers/admin/repository_controller.ex b/lib/kmxgit_web/controllers/admin/repository_controller.ex
index 2da8883..95cc903 100644
--- a/lib/kmxgit_web/controllers/admin/repository_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/repository_controller.ex
@@ -2,6 +2,7 @@ defmodule KmxgitWeb.Admin.RepositoryController do
   use KmxgitWeb, :controller
 
   alias Kmxgit.RepositoryManager
+  alias Kmxgit.RepositoryManager.Repository
   alias Kmxgit.Repo
   alias Kmxgit.SlugManager
   alias KmxgitWeb.ErrorView
@@ -73,46 +74,35 @@ defmodule KmxgitWeb.Admin.RepositoryController do
   end
 
   def show(conn, params) do
-    repo = RepositoryManager.get_repository(params["id"])
-    if repo do
-      conn
-      |> assign(:repo, repo)
-      |> render("show.html")
-    else
-      not_found(conn)
-    end
+    repo = RepositoryManager.get_repository!(params["id"])
+    conn
+    |> assign(:members, Repository.members(repo))
+    |> assign(:repo, repo)
+    |> render("show.html")
   end
 
   def edit(conn, params) do
-    repo = RepositoryManager.get_repository(params["id"])
-    if repo do
-      changeset = RepositoryManager.change_repository(repo)
-      conn
-      |> assign(:action, Routes.admin_repository_path(conn, :update, repo))
-      |> assign(:changeset, changeset)
-      |> assign(:repo, repo)
-      |> render("edit.html")
-    else
-      not_found(conn)
-    end
+    repo = RepositoryManager.get_repository!(params["id"])
+    changeset = RepositoryManager.change_repository(repo)
+    conn
+    |> assign(:action, Routes.admin_repository_path(conn, :update, repo))
+    |> assign(:changeset, changeset)
+    |> assign(:repo, repo)
+    |> render("edit.html")
   end
 
   def update(conn, params) do
-    repo = RepositoryManager.get_repository(params["id"])
-    if repo do
-      case RepositoryManager.update_repository(repo, params["repository"]) do
-        {:ok, repo} ->
-          conn
-          |> redirect(to: Routes.admin_repository_path(conn, :show, repo))
-        {:error, changeset} ->
-          IO.inspect(changeset)
-          conn
-          |> assign(:action, Routes.admin_repository__path(conn, :update, repo))
-          |> assign(:changeset, changeset)
-          |> render("edit.html")
-      end
-    else
-      not_found(conn)
+    repo = RepositoryManager.get_repository!(params["id"])
+    case RepositoryManager.update_repository(repo, params["repository"]) do
+      {:ok, repo} ->
+        conn
+        |> redirect(to: Routes.admin_repository_path(conn, :show, repo))
+      {:error, changeset} ->
+        IO.inspect(changeset)
+        conn
+        |> assign(:action, Routes.admin_repository__path(conn, :update, repo))
+        |> assign(:changeset, changeset)
+        |> render("edit.html")
     end
   end
 
@@ -127,7 +117,7 @@ defmodule KmxgitWeb.Admin.RepositoryController do
   def add_user_post(conn, params) do
     login = params["repository"]["login"]
     repo = RepositoryManager.get_repository!(params["repository_id"])
-    case RepositoryManager.add_user(repo, login) do
+    case RepositoryManager.add_member(repo, login) do
       {:ok, repo} ->
         conn
         |> redirect(to: Routes.admin_repository_path(conn, :show, repo))
@@ -150,7 +140,7 @@ defmodule KmxgitWeb.Admin.RepositoryController do
   def remove_user_post(conn, params) do
     login = params["repository"]["login"]
     repo = RepositoryManager.get_repository!(params["repository_id"])
-    case RepositoryManager.remove_user(repo, login) do
+    case RepositoryManager.remove_member(repo, login) do
       {:ok, repo} ->
         conn
         |> redirect(to: Routes.admin_repository_path(conn, :show, repo))
diff --git a/lib/kmxgit_web/templates/admin/organisation/edit.html.heex b/lib/kmxgit_web/templates/admin/organisation/edit.html.heex
index ed84074..303a3c0 100644
--- a/lib/kmxgit_web/templates/admin/organisation/edit.html.heex
+++ b/lib/kmxgit_web/templates/admin/organisation/edit.html.heex
@@ -24,6 +24,9 @@
     </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>
 
diff --git a/lib/kmxgit_web/templates/admin/organisation/show.html.heex b/lib/kmxgit_web/templates/admin/organisation/show.html.heex
index a9c0b0b..b76344c 100644
--- a/lib/kmxgit_web/templates/admin/organisation/show.html.heex
+++ b/lib/kmxgit_web/templates/admin/organisation/show.html.heex
@@ -38,13 +38,13 @@
     </tr>
   </table>
 
-  <%= link gettext("Delete org"),
+  <%= link gettext("Delete organisation"),
       to: Routes.admin_organisation_path(@conn, :delete, @org),
       class: "btn btn-danger",
       data: [confirm: gettext("Are you sure you want to delete this org ?")],
       method: :delete %>
 
-  <%= link gettext("Edit org"),
+  <%= link gettext("Edit organisation"),
       to: Routes.admin_organisation_path(@conn, :edit, @org),
       class: "btn btn-primary" %>
 </div>
diff --git a/lib/kmxgit_web/templates/admin/user/form.html.heex b/lib/kmxgit_web/templates/admin/user/form.html.heex
index 954c41f..a8b2175 100644
--- a/lib/kmxgit_web/templates/admin/user/form.html.heex
+++ b/lib/kmxgit_web/templates/admin/user/form.html.heex
@@ -39,10 +39,15 @@
   </div>
 
   <div class="form-group">
-    <%= link gettext("Cancel"),
-             to: Routes.admin_user_path(@conn, :index),
-             class: "btn btn-secondary" %>
-    &nbsp;
+    <%= if @conn.assigns[:user] do %>
+      <%= link gettext("Cancel"),
+          to: Routes.admin_user_path(@conn, :show, @user),
+          class: "btn btn-secondary" %>
+    <% else %>
+      <%= link gettext("Cancel"),
+          to: Routes.admin_user_path(@conn, :index),
+          class: "btn btn-secondary" %>
+    <% end %>
     <%= submit gettext("Submit"), class: "btn btn-primary" %>
   </div>
 
diff --git a/lib/kmxgit_web/templates/admin/user/index.html.heex b/lib/kmxgit_web/templates/admin/user/index.html.heex
index 150ee82..028a6c1 100644
--- a/lib/kmxgit_web/templates/admin/user/index.html.heex
+++ b/lib/kmxgit_web/templates/admin/user/index.html.heex
@@ -1,20 +1,23 @@
-<h1>Users</h1>
+<div class="container-fluid">
+  <h1>Users</h1>
 
-<table class="table admin-index">
-  <tr>
-    <th><%= gettext "Id" %></th>
-    <th><%= gettext "Name" %></th>
-    <th><%= gettext "Email" %></th>
-    <th><%= gettext "Login" %></th>
-    <th><%= gettext "Admin" %></th>
-  </tr>
-  <%= Enum.map @users, fn(user) -> %>
+  <table class="table admin-index">
     <tr>
-      <td><%= link user.id, to: Routes.admin_user_path(@conn, :show, user) %></td>
-      <td><%= user.name %></td>
-      <td><%= link user.email, to: "mailto:#{user.email}" %></td>
-      <td><%= link user.slug.slug, to: Routes.slug_path(@conn, :show, user.slug.slug) %></td>
-      <td><%= user.is_admin %></td>
+      <th><%= gettext "Id" %></th>
+      <th><%= gettext "Name" %></th>
+      <th><%= gettext "Email" %></th>
+      <th><%= gettext "Login" %></th>
+      <th><%= gettext "Admin" %></th>
     </tr>
-  <% end %>
-</table>
+    <%= Enum.map @users, fn(user) -> %>
+      <tr>
+        <td><%= link user.id, to: Routes.admin_user_path(@conn, :show, user) %></td>
+        <td><%= user.name %></td>
+        <td><%= link user.email, to: "mailto:#{user.email}" %></td>
+        <td><%= link user.slug.slug, to: Routes.slug_path(@conn, :show, user.slug.slug) %></td>
+        <td><%= user.is_admin %></td>
+      </tr>
+    <% end %>
+  </table>
+
+</div>
diff --git a/lib/kmxgit_web/templates/admin/user/show.html.heex b/lib/kmxgit_web/templates/admin/user/show.html.heex
index 2003993..dcc1371 100644
--- a/lib/kmxgit_web/templates/admin/user/show.html.heex
+++ b/lib/kmxgit_web/templates/admin/user/show.html.heex
@@ -1,52 +1,55 @@
-<h1>User <%= @user.slug.slug %></h1>
+<div class="container-fluid">
+  <h1>User <%= @user.slug.slug %></h1>
 
-<table class="table admin-properties">
-  <tr>
-    <th><%= gettext "Id" %></th>
-    <td><%= @user.id %></td>
-  </tr>
-  <tr>
-    <th><%= gettext "Name" %></th>
-    <td><%= @user.name %></td>
-  </tr>
-  <tr>
-    <th><%= gettext "Email" %></th>
-    <td><%= link @user.email, to: "mailto:#{@user.email}" %></td>
-  </tr>
-  <tr>
-    <th><%= gettext "Login" %></th>
-    <td><%= link @user.slug.slug, to: Routes.slug_path(@conn, :show, @user.slug.slug) %></td>
-  </tr>
-  <tr>
-    <th><%= gettext "Description" %></th>
-    <td>
-      <%= if @user.description do %>
-      <%= raw Earmark.as_html!(@user.description) %>
-      <% end %>
-    </td>
-  </tr>
-  <tr>
-    <th>
-      <%= gettext "Organisations" %>
-    </th>
-    <td>
-      <%= for org <- @user.organisations do %>
-        <%= link(org.name || org.slug.slug, to: Routes.admin_organisation_path(@conn, :show, org), class: "org") %>
-      <% end %>
-    </td>
-  </tr>
-  <tr>
-    <th><%= gettext "Admin" %></th>
-    <td><%= @user.is_admin %></td>
-  </tr>
-</table>
+  <table class="table admin-properties">
+    <tr>
+      <th><%= gettext "Id" %></th>
+      <td><%= @user.id %></td>
+    </tr>
+    <tr>
+      <th><%= gettext "Name" %></th>
+      <td><%= @user.name %></td>
+    </tr>
+    <tr>
+      <th><%= gettext "Email" %></th>
+      <td><%= link @user.email, to: "mailto:#{@user.email}" %></td>
+    </tr>
+    <tr>
+      <th><%= gettext "Login" %></th>
+      <td><%= link @user.slug.slug, to: Routes.slug_path(@conn, :show, @user.slug.slug) %></td>
+    </tr>
+    <tr>
+      <th><%= gettext "Description" %></th>
+      <td>
+        <%= if @user.description do %>
+        <%= raw Earmark.as_html!(@user.description) %>
+        <% end %>
+      </td>
+    </tr>
+    <tr>
+      <th>
+        <%= gettext "Organisations" %>
+      </th>
+      <td>
+        <%= for org <- @user.organisations do %>
+          <%= link(org.name || org.slug.slug, to: Routes.admin_organisation_path(@conn, :show, org), class: "org") %>
+        <% end %>
+      </td>
+    </tr>
+    <tr>
+      <th><%= gettext "Admin" %></th>
+      <td><%= @user.is_admin %></td>
+    </tr>
+  </table>
 
-<%= link gettext("Delete user"),
-    to: Routes.admin_user_path(@conn, :delete, @user),
-    class: "btn btn-danger",
-    data: [confirm: gettext("Are you sure you want to delete this user ?")],
-    method: :delete %>
+  <%= link gettext("Delete user"),
+      to: Routes.admin_user_path(@conn, :delete, @user),
+      class: "btn btn-danger",
+      data: [confirm: gettext("Are you sure you want to delete this user ?")],
+      method: :delete %>
 
-<%= link gettext("Edit user"),
-         to: Routes.admin_user_path(@conn, :edit, @user),
-         class: "btn btn-primary" %>
+  <%= link gettext("Edit user"),
+      to: Routes.admin_user_path(@conn, :edit, @user),
+      class: "btn btn-primary" %>
+
+</div>