Commit 4aa311813584aa4fcac1444e91b5dc2c24c3adcb

Thomas de Grivel 2021-11-19T16:03:18

delete repo

diff --git a/lib/kmxgit_web/controllers/repository_controller.ex b/lib/kmxgit_web/controllers/repository_controller.ex
index ba1880e..b56cf54 100644
--- a/lib/kmxgit_web/controllers/repository_controller.ex
+++ b/lib/kmxgit_web/controllers/repository_controller.ex
@@ -21,6 +21,7 @@ defmodule KmxgitWeb.RepositoryController do
           conn
           |> assign(:action, action)
           |> assign(:changeset, changeset)
+          |> assign(:owner, user)
           |> render("new.html")
         end
       else
@@ -61,19 +62,20 @@ defmodule KmxgitWeb.RepositoryController do
       end
     end
   end
-    # TODO: check the owner part of path
 
   defp create_repo(conn, owner, params) do
-    case RepositoryManager.create_repository(owner, params) do
-      {:ok, repo} ->
-        conn
-        |> redirect(to: Routes.repository_path(conn, :show, owner.slug.slug, Repository.splat(repo)))
-      {:error, changeset} ->
-        IO.inspect(changeset)
-        conn
-        |> assign(:action, Routes.repository_path(conn, :create, owner.slug))
-        |> assign(:changeset, changeset)
-        |> render("new.html")
+    Repo.transaction do
+      case RepositoryManager.create_repository(owner, params) do
+        {:ok, repo} ->
+          conn
+          |> redirect(to: Routes.repository_path(conn, :show, owner.slug.slug, Repository.splat(repo)))
+        {:error, changeset} ->
+          IO.inspect(changeset)
+          conn
+          |> assign(:action, Routes.repository_path(conn, :create, owner.slug.slug))
+          |> assign(:changeset, changeset)
+          |> render("new.html")
+      end
     end
   end
 
@@ -149,6 +151,24 @@ defmodule KmxgitWeb.RepositoryController do
     end
   end
 
+  def delete(conn, params) do
+    current_user = conn.assigns.current_user
+    slug = Enum.join(params["slug"], "/")
+    repo = RepositoryManager.get_repository_by_owner_and_slug(params["owner"], slug)
+    if repo do
+      org = repo.organisation
+      if org && Enum.find(org.users, &(&1.id == current_user.id)) || repo.user_id == current_user.id do
+        {:ok, _} = RepositoryManager.delete_repository(repo)
+        conn
+        |> redirect(to: Routes.slug_path(conn, :show, params["owner"]))
+      else
+        not_found(conn)
+      end
+    else
+      not_found(conn)
+    end
+  end
+
   defp assign_current_organisation(conn, nil), do: conn
   defp assign_current_organisation(conn, %Ecto.Association.NotLoaded{}), do: conn
   defp assign_current_organisation(conn, org) do
diff --git a/lib/kmxgit_web/router.ex b/lib/kmxgit_web/router.ex
index 1180775..2702e08 100644
--- a/lib/kmxgit_web/router.ex
+++ b/lib/kmxgit_web/router.ex
@@ -82,8 +82,9 @@ defmodule KmxgitWeb.Router do
   scope "/", KmxgitWeb do
     pipe_through [:browser, :auth]
 
-    get "/:slug", SlugController, :show
-    get "/:owner/*slug", RepositoryController, :show
+    get    "/:slug", SlugController, :show
+    get    "/:owner/*slug", RepositoryController, :show
+    delete "/:owner/*slug", RepositoryController, :delete
   end
 
   # Other scopes may use custom stacks.
diff --git a/lib/kmxgit_web/templates/repository/form.html.heex b/lib/kmxgit_web/templates/repository/form.html.heex
index fe584fa..338b0c9 100644
--- a/lib/kmxgit_web/templates/repository/form.html.heex
+++ b/lib/kmxgit_web/templates/repository/form.html.heex
@@ -1,4 +1,9 @@
-<%= form_for @changeset, @action, fn f -> %>
+<.form let={f} for={@changeset} action={@action}>
+  <%= if @changeset.action do %>
+    <div class="alert alert-danger">
+      <p>Invalid parameters</p>
+    </div>
+  <% end %>
 
   <div class="mb-3">
     <%= label f, :slug, class: "form-label" %>
@@ -13,7 +18,18 @@
   </div>
 
   <div class="mb-3">
+    <%= if @conn.assigns[:current_repository] do %>
+      <%= link gettext("Delete repository"),
+          to: Routes.repository_path(@conn, :delete, Repository.owner_slug(@current_repository), Repository.splat(@current_repository)),
+          method: :delete,
+          class: "btn btn-danger",
+          data: [confirm: gettext("Are you sure you want to delete the repository %{repo} ?", repo: Repository.full_slug(@current_repository))] %>
+    <% else %>
+      <%= link gettext("Cancel"),
+          to: Routes.slug_path(@conn, :show, @owner.slug.slug),
+          class: "btn btn-secondary" %>
+    <% end %>
     <%= submit gettext("Submit"), class: "btn btn-primary" %>
   </div>
 
-<% end %>
+</.form>
diff --git a/lib/kmxgit_web/templates/repository/new.html.heex b/lib/kmxgit_web/templates/repository/new.html.heex
index e806943..61acd57 100644
--- a/lib/kmxgit_web/templates/repository/new.html.heex
+++ b/lib/kmxgit_web/templates/repository/new.html.heex
@@ -1,4 +1,10 @@
 <div class="container-fluid">
-  <h1>New repository</h1>
+  <h1>
+    <%= if @conn.assigns[:current_organisation] do %>
+      <%= gettext("New repository for organisation %{org}", org: @current_organisation.name || @current_organisation.slug.slug) %>
+    <% else %>
+      <%= gettext("New repository for user %{login}", login: @owner.slug.slug) %>
+    <% end %>
+  </h1>
   <%= render "form.html", assigns %>
 </div>