Commit c45775f285c7f209b075f64180f81d18a30ce819

Thomas de Grivel 2022-01-28T10:38:19

[admin] refactor organisation index sort by columns

diff --git a/lib/kmxgit/organisation_manager.ex b/lib/kmxgit/organisation_manager.ex
index eaf78d4..ed9dda5 100644
--- a/lib/kmxgit/organisation_manager.ex
+++ b/lib/kmxgit/organisation_manager.ex
@@ -2,6 +2,7 @@ defmodule Kmxgit.OrganisationManager do
 
   import Ecto.Query, warn: false
 
+  alias Kmxgit.IndexParams
   alias Kmxgit.OrganisationManager.Organisation
   alias Kmxgit.Repo
   alias Kmxgit.SlugManager.Slug
@@ -10,65 +11,37 @@ defmodule Kmxgit.OrganisationManager do
   @list_preload [:owned_repositories,
                  :slug]
 
-  def list_organisations() do
+  def list_organisations(params \\ %IndexParams{}) do
     update_disk_usage()
-    Repo.all from org in Organisation,
-      join: s in Slug,
-      on: s.organisation_id == org.id,
-      preload: ^@list_preload,
-      order_by: [asc_nulls_last: fragment("lower(?)", s.slug)]
+    from(org in Organisation)
+    |> join(:inner, [org], s in Slug, on: s.organisation_id == org.id,)
+    |> preload(^@list_preload)
+    |> index_order_by(params)
+    |> Repo.all()
   end
-  def list_organisations(%{column: "id", reverse: true}) do
-    update_disk_usage()
-    Repo.all from org in Organisation,
-      preload: ^@list_preload,
-      order_by: [desc: :id]
+  def index_order_by(query, %{column: "id", reverse: true}) do
+    order_by(query, [desc: :id])
   end
-  def list_organisations(%{column: "id"}) do
-    update_disk_usage()
-    Repo.all from org in Organisation,
-      preload: ^@list_preload,
-      order_by: :id
+  def index_order_by(query, %{column: "id"}) do
+    order_by(query, :id)
   end
-  def list_organisations(%{column: "name", reverse: true}) do
-    update_disk_usage()
-    Repo.all from org in Organisation,
-      preload: ^@list_preload,
-      order_by: [desc_nulls_last: fragment("lower(?)", org.name)]
+  def index_order_by(query, %{column: "name", reverse: true}) do
+    order_by(query, [org, s], [desc_nulls_last: fragment("lower(?)", org.name)])
   end
-  def list_organisations(%{column: "name"}) do
-    update_disk_usage()
-    Repo.all from org in Organisation,
-      preload: ^@list_preload,
-      order_by: [asc_nulls_last: fragment("lower(?)", org.name)]
+  def index_order_by(query, %{column: "name"}) do
+    order_by(query, [org, s], [asc_nulls_last: fragment("lower(?)", org.name)])
   end
-  def list_organisations(%{column: "slug", reverse: true}) do
-    update_disk_usage()
-    Repo.all from org in Organisation,
-      join: s in Slug,
-      on: s.organisation_id == org.id,
-      preload: ^@list_preload,
-      order_by: [desc_nulls_last: fragment("lower(?)", s.slug)]
+  def index_order_by(query, %{column: "slug", reverse: true}) do
+    order_by(query, [org, s], [desc_nulls_last: fragment("lower(?)", s.slug)])
   end
-  def list_organisations(%{column: "slug"}) do
-    update_disk_usage()
-    Repo.all from org in Organisation,
-      join: s in Slug,
-      on: s.organisation_id == org.id,
-      preload: ^@list_preload,
-      order_by: [asc_nulls_last: fragment("lower(?)", s.slug)]
+  def index_order_by(query, %{column: "slug"}) do
+    order_by(query, [org, s], [asc_nulls_last: fragment("lower(?)", s.slug)])
   end
-  def list_organisations(%{column: "du", reverse: true}) do
-    update_disk_usage()
-    Repo.all from org in Organisation,
-      preload: ^@list_preload,
-      order_by: [desc: :disk_usage]
+  def index_order_by(query, %{column: "du", reverse: true}) do
+    order_by(query, [desc: :disk_usage])
   end
-  def list_organisations(%{column: "du"}) do
-    update_disk_usage()
-    Repo.all from org in Organisation,
-      preload: ^@list_preload,
-      order_by: :disk_usage
+  def index_order_by(query, %{column: "du"}) do
+    order_by(query, :disk_usage)
   end
 
   def update_disk_usage() do
diff --git a/lib/kmxgit_web/controllers/admin/organisation_controller.ex b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
index d7d0512..38100c1 100644
--- a/lib/kmxgit_web/controllers/admin/organisation_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
@@ -1,17 +1,19 @@
 defmodule KmxgitWeb.Admin.OrganisationController do
   use KmxgitWeb, :controller
 
+  alias Kmxgit.IndexParams
   alias Kmxgit.GitManager
   alias Kmxgit.OrganisationManager
   alias Kmxgit.Repo
   alias KmxgitWeb.ErrorView
 
   def index(conn, params) do
-    sort = KmxgitWeb.Admin.sort_param(params["sort"])
-    orgs = OrganisationManager.list_organisations(sort)
+    index_params = %IndexParams{}
+    |> KmxgitWeb.Admin.sort_param(params["sort"])
+    orgs = OrganisationManager.list_organisations(index_params)
     conn
+    |> assign(:index, index_params)
     |> assign(:orgs, orgs)
-    |> assign(:sort, sort)
     |> render("index.html")
   end
 
diff --git a/lib/kmxgit_web/templates/admin/organisation/index.html.heex b/lib/kmxgit_web/templates/admin/organisation/index.html.heex
index 941674a..0d161bd 100644
--- a/lib/kmxgit_web/templates/admin/organisation/index.html.heex
+++ b/lib/kmxgit_web/templates/admin/organisation/index.html.heex
@@ -10,10 +10,10 @@
   <table class="table admin-index">
     <thead>
       <tr>
-        <th><%= link gettext("Id"), to: Routes.admin_organisation_path(@conn, :index, sort: "id#{if @sort.column == "id" && !@sort.reverse, do: "-"}") %><%= if @sort.column == "id" do %><%= if @sort.reverse do %> <i class="fa fa-angle-down"></i><% else %> <i class="fa fa-angle-up"></i><% end %><% end %></th>
-        <th><%= link gettext("Name"), to: Routes.admin_organisation_path(@conn, :index, sort: "name#{if @sort.column == "name" && !@sort.reverse, do: "-"}") %><%= if @sort.column == "name" do %><%= if @sort.reverse do %> <i class="fa fa-angle-down"></i><% else %> <i class="fa fa-angle-up"></i><% end %><% end %></th>
-        <th><%= link gettext("Slug"), to: Routes.admin_organisation_path(@conn, :index, sort: "slug#{if @sort.column == "slug" && !@sort.reverse, do: "-"}") %><%= if @sort.column == "slug" do %><%= if @sort.reverse do %> <i class="fa fa-angle-down"></i><% else %> <i class="fa fa-angle-up"></i><% end %><% end %></th>
-        <th><%= link gettext("Disk usage"), to: Routes.admin_organisation_path(@conn, :index, sort: "du#{if @sort.column != "du" || (@sort.column == "du" && !@sort.reverse), do: "-"}") %><%= if @sort.column == "du" do %><%= if @sort.reverse do %> <i class="fa fa-angle-down"></i><% else %> <i class="fa fa-angle-up"></i><% end %><% end %></th>
+        <th><%= link gettext("Id"), to: Routes.admin_organisation_path(@conn, :index, sort: "id#{if @index.column == "id" && !@index.reverse, do: "-"}") %><%= if @index.column == "id" do %><%= if @index.reverse do %> <i class="fa fa-angle-down"></i><% else %> <i class="fa fa-angle-up"></i><% end %><% end %></th>
+        <th><%= link gettext("Name"), to: Routes.admin_organisation_path(@conn, :index, sort: "name#{if @index.column == "name" && !@index.reverse, do: "-"}") %><%= if @index.column == "name" do %><%= if @index.reverse do %> <i class="fa fa-angle-down"></i><% else %> <i class="fa fa-angle-up"></i><% end %><% end %></th>
+        <th><%= link gettext("Slug"), to: Routes.admin_organisation_path(@conn, :index, sort: "slug#{if @index.column == "slug" && !@index.reverse, do: "-"}") %><%= if @index.column == "slug" do %><%= if @index.reverse do %> <i class="fa fa-angle-down"></i><% else %> <i class="fa fa-angle-up"></i><% end %><% end %></th>
+        <th><%= link gettext("Disk usage"), to: Routes.admin_organisation_path(@conn, :index, sort: "du#{if @index.column != "du" || (@index.column == "du" && !@index.reverse), do: "-"}") %><%= if @index.column == "du" do %><%= if @index.reverse do %> <i class="fa fa-angle-down"></i><% else %> <i class="fa fa-angle-up"></i><% end %><% end %></th>
         <th><%= gettext("Actions") %></th>
       </tr>
     </thead>