Commit f5fa9ecd18336e033ecfb17ed422c21def0a1b88

Thomas de Grivel 2021-11-22T11:10:00

admin: repositories index

diff --git a/lib/kmxgit_web/controllers/admin/organisation_controller.ex b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
index f94f5b3..55f6928 100644
--- a/lib/kmxgit_web/controllers/admin/organisation_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
@@ -9,7 +9,8 @@ defmodule KmxgitWeb.Admin.OrganisationController do
   def index(conn, _params) do
     organisations = OrganisationManager.list_organisations
     conn
-    |> render("index.html", organisations: organisations)
+    |> assign(:organisations, organisations)
+    |> render("index.html")
   end
 
   def new(conn, _params) do
@@ -87,8 +88,10 @@ defmodule KmxgitWeb.Admin.OrganisationController do
         {:error, changeset} ->
           IO.inspect(changeset)
           conn
-          |> render("edit.html", changeset: changeset,
-                    action: Routes.admin_organisation__path(conn, :update, organisation))
+          |> assign(:action, Routes.admin_organisation_path(conn, :update, organisation))
+          |> assign(:changeset, changeset)
+          |> render("edit.html")
+                    
       end
     else
       not_found(conn)
diff --git a/lib/kmxgit_web/controllers/admin/repository_controller.ex b/lib/kmxgit_web/controllers/admin/repository_controller.ex
new file mode 100644
index 0000000..2da8883
--- /dev/null
+++ b/lib/kmxgit_web/controllers/admin/repository_controller.ex
@@ -0,0 +1,175 @@
+defmodule KmxgitWeb.Admin.RepositoryController do
+  use KmxgitWeb, :controller
+
+  alias Kmxgit.RepositoryManager
+  alias Kmxgit.Repo
+  alias Kmxgit.SlugManager
+  alias KmxgitWeb.ErrorView
+
+  def index(conn, _params) do
+    repos = RepositoryManager.list_repositories
+    conn
+    |> assign(:repos, repos)
+    |> render("index.html")
+  end
+
+  def new(conn, _params) do
+    changeset = RepositoryManager.change_repository
+    conn
+    |> assign(:action, Routes.admin_repository_path(conn, :create))
+    |> assign(:changeset, changeset)
+    |> render("new.html")
+  end
+
+  def create(conn, params) do
+    current_user = conn.assigns.current_user
+    slug = SlugManager.get_slug(params["owner"])
+    if slug do
+      user = slug.user
+      if user do
+        if user == current_user do
+          create_repo(conn, user, params["repository"])
+        else
+          not_found(conn)
+        end
+      else
+        org = slug.organisation
+        if org do
+          create_repo(conn, org, params["repository"])
+        else
+          not_found(conn)
+        end
+      end
+    else
+      not_found(conn)
+    end
+  end
+
+  defp create_repo(conn, owner, params) do
+    case Repo.transaction(fn ->
+          case RepositoryManager.create_repository(owner, params) do
+            {:ok, repo} -> repo
+            {:error, changeset} -> Repo.rollback changeset
+          end
+        end) 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, :create))
+        |> assign(:changeset, changeset)
+        |> assign(:owner, owner)
+        |> render("new.html")
+    end
+  end
+
+  defp not_found(conn) do
+    conn
+    |> put_status(:not_found)
+    |> put_view(ErrorView)
+    |> render(:"404")
+  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
+  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
+  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)
+    end
+  end
+
+  def add_user(conn, params) do
+    repo = RepositoryManager.get_repository!(params["repository_id"])
+    conn
+    |> assign(:action, Routes.admin_repository__path(conn, :add_user_post, repo))
+    |> assign(:repo, repo)
+    |> render("add_user.html")
+  end
+
+  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
+      {:ok, repo} ->
+        conn
+        |> redirect(to: Routes.admin_repository_path(conn, :show, repo))
+      {:error, _} ->
+        conn
+        |> assign(:action, Routes.admin_repository__path(conn, :add_user_post, repo))
+        |> assign(:repo, repo)
+        |> render("add_user.html")
+    end
+  end
+
+  def remove_user(conn, params) do
+    repo = RepositoryManager.get_repository!(params["repository_id"])
+    conn
+    |> assign(:action, Routes.admin_repository__path(conn, :remove_user_post, repo))
+    |> assign(:repo, repo)
+    |> render("remove_user.html")
+  end
+
+  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
+      {:ok, repo} ->
+        conn
+        |> redirect(to: Routes.admin_repository_path(conn, :show, repo))
+      {:error, _} ->
+        conn
+        |> assign(:action, Routes.admin_repository__path(conn, :remove_user_post, repo))
+        |> assign(:repo, repo)
+        |> render("remove_user.html")
+    end
+  end
+
+  def delete(conn, params) do
+    repository = RepositoryManager.get_repository(params["id"])
+    if repository do
+      {:ok, _} = RepositoryManager.delete_repository(repository)
+      conn
+      |> redirect(to: Routes.admin_repository_path(conn, :index))
+    else
+      not_found(conn)
+    end
+  end
+end
diff --git a/lib/kmxgit_web/controllers/repository_controller.ex b/lib/kmxgit_web/controllers/repository_controller.ex
index ea90d9b..b8789ec 100644
--- a/lib/kmxgit_web/controllers/repository_controller.ex
+++ b/lib/kmxgit_web/controllers/repository_controller.ex
@@ -16,7 +16,7 @@ defmodule KmxgitWeb.RepositoryController do
     else
       user = slug.user
       if user do
-        if user != current_user && !current_user.is_admin do
+        if user != current_user do
           not_found(conn)
         else
           conn
@@ -49,7 +49,7 @@ defmodule KmxgitWeb.RepositoryController do
     else
       user = slug.user
       if user do
-        if user != current_user && !current_user.is_admin do
+        if user != current_user do
           not_found(conn)
         else
           create_repo(conn, user, params["repository"])
diff --git a/lib/kmxgit_web/templates/admin/repository/index.html.heex b/lib/kmxgit_web/templates/admin/repository/index.html.heex
new file mode 100644
index 0000000..46f115c
--- /dev/null
+++ b/lib/kmxgit_web/templates/admin/repository/index.html.heex
@@ -0,0 +1,23 @@
+<div class="container-fluid">
+  <h1>Repositories</h1>
+
+  <table class="table admin-index">
+    <tr>
+      <th><%= gettext "Id" %></th>
+      <th><%= gettext "Owner" %></th>
+      <th><%= gettext "Slug" %></th>
+    </tr>
+    <%= Enum.map @repos, fn(repo) -> %>
+      <tr>
+        <td><%= link repo.id, to: Routes.admin_repository_path(@conn, :show, repo) %></td>
+        <%= case owner = Repository.owner(repo) do %>
+          <% %Organisation{} -> %>
+            <td><%= link owner.name || owner.slug.slug, to: Routes.admin_organisation_path(@conn, :show, owner) %></td>
+          <% %User{} -> %>
+            <td><%= link owner.slug.slug, to: Routes.admin_user_path(@conn, :show, owner) %></td>
+        <% end %>
+        <td><%= link Repository.full_slug(repo), to: Routes.repository_path(@conn, :show, Repository.owner_slug(repo), Repository.splat(repo)) %></td>
+      </tr>
+    <% end %>
+  </table>
+</div>
diff --git a/lib/kmxgit_web/templates/layout/admin_nav.html.heex b/lib/kmxgit_web/templates/layout/admin_nav.html.heex
index 6aa56ab..9b63bf7 100644
--- a/lib/kmxgit_web/templates/layout/admin_nav.html.heex
+++ b/lib/kmxgit_web/templates/layout/admin_nav.html.heex
@@ -10,6 +10,9 @@
     <div class="collapse navbar-collapse" id="navbarSupportedContent">
       <ul class="navbar-nav mr-auto">
         <li class="navbar-item">
+          <%= link gettext("Repositories"), to: Routes.admin_repository_path(@conn, :index), class: "nav-link" %>
+        </li>
+        <li class="navbar-item">
           <%= link gettext("Organisations"), to: Routes.admin_organisation_path(@conn, :index), class: "nav-link" %>
         </li>
         <li class="navbar-item">
diff --git a/lib/kmxgit_web/views/admin/repository_view.ex b/lib/kmxgit_web/views/admin/repository_view.ex
new file mode 100644
index 0000000..280cdc0
--- /dev/null
+++ b/lib/kmxgit_web/views/admin/repository_view.ex
@@ -0,0 +1,7 @@
+defmodule KmxgitWeb.Admin.RepositoryView do
+  use KmxgitWeb, :view
+
+  alias Kmxgit.OrganisationManager.Organisation
+  alias Kmxgit.RepositoryManager.Repository
+  alias Kmxgit.UserManager.User
+end