Commit dacbfb4026a4371dce6b74bd54a6324ef08d460f

Thomas de Grivel 2022-01-27T19:59:41

[admin] fix column sort

diff --git a/lib/kmxgit/organisation_manager.ex b/lib/kmxgit/organisation_manager.ex
index bb86630..9a94551 100644
--- a/lib/kmxgit/organisation_manager.ex
+++ b/lib/kmxgit/organisation_manager.ex
@@ -16,19 +16,7 @@ defmodule Kmxgit.OrganisationManager do
       join: s in Slug,
       on: s.organisation_id == org.id,
       preload: ^@list_preload,
-      order_by: 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]
-  end
-  def list_organisations(%{column: "du"}) do
-    update_disk_usage()
-    Repo.all from org in Organisation,
-      preload: ^@list_preload,
-      order_by: :disk_usage
+      order_by: [asc_nulls_last: fragment("lower(?)", s.slug)]
   end
   def list_organisations(%{column: "id", reverse: true}) do
     update_disk_usage()
@@ -46,13 +34,13 @@ defmodule Kmxgit.OrganisationManager do
     update_disk_usage()
     Repo.all from org in Organisation,
       preload: ^@list_preload,
-      order_by: [desc: :name]
+      order_by: [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: :name
+      order_by: [asc_nulls_last: fragment("lower(?)", org.name)]
   end
   def list_organisations(%{column: "slug", reverse: true}) do
     update_disk_usage()
@@ -60,7 +48,7 @@ defmodule Kmxgit.OrganisationManager do
       join: s in Slug,
       on: s.organisation_id == org.id,
       preload: ^@list_preload,
-      order_by: [desc: s.slug]
+      order_by: [desc_nulls_last: fragment("lower(?)", s.slug)]
   end
   def list_organisations(%{column: "slug"}) do
     update_disk_usage()
@@ -68,7 +56,19 @@ defmodule Kmxgit.OrganisationManager do
       join: s in Slug,
       on: s.organisation_id == org.id,
       preload: ^@list_preload,
-      order_by: s.slug
+      order_by: [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]
+  end
+  def list_organisations(%{column: "du"}) do
+    update_disk_usage()
+    Repo.all from org in Organisation,
+      preload: ^@list_preload,
+      order_by: :disk_usage
   end
 
   def update_disk_usage() do
diff --git a/lib/kmxgit/repository_manager.ex b/lib/kmxgit/repository_manager.ex
index c6bb64e..cca598f 100644
--- a/lib/kmxgit/repository_manager.ex
+++ b/lib/kmxgit/repository_manager.ex
@@ -15,21 +15,19 @@ defmodule Kmxgit.RepositoryManager do
                  user: :slug]
 
   def list_repositories do
-    Repo.all(from r in Repository,
-      preload: ^@list_preload)
-    |> Enum.sort_by(&Repository.full_slug/1)
-  end
-  def list_repositories(%{column: "du", reverse: true}) do
-    update_disk_usage()
-    Repo.all from r in Repository,
-      preload: ^@list_preload,
-      order_by: [desc: :disk_usage]
-  end
-  def list_repositories(%{column: "du"}) do
     update_disk_usage()
     Repo.all from r in Repository,
+      full_join: o in Organisation,
+      on: o.id == r.organisation_id,
+      full_join: os in Slug,
+      on: os.organisation_id == o.id,
+      full_join: u in User,
+      on: u.id == r.user_id,
+      full_join: us in Slug,
+      on: us.user_id == u.id,
+      where: not is_nil(r),
       preload: ^@list_preload,
-      order_by: :disk_usage
+      order_by: [desc: fragment("concat(lower(?), lower(?))", os.slug, us.slug), desc: :slug]
   end
   def list_repositories(%{column: "id", reverse: true}) do
     update_disk_usage()
@@ -103,6 +101,18 @@ defmodule Kmxgit.RepositoryManager do
       preload: ^@list_preload,
       order_by: [fragment("concat(lower(?), lower(?))", os.slug, us.slug), :slug]
   end
+  def list_repositories(%{column: "du", reverse: true}) do
+    update_disk_usage()
+    Repo.all from r in Repository,
+      preload: ^@list_preload,
+      order_by: [desc: :disk_usage]
+  end
+  def list_repositories(%{column: "du"}) do
+    update_disk_usage()
+    Repo.all from r in Repository,
+      preload: ^@list_preload,
+      order_by: :disk_usage
+  end
 
   def update_disk_usage() do
     repos = Repo.all(from repo in Repository, preload: [organisation: :slug,
diff --git a/lib/kmxgit/user_manager.ex b/lib/kmxgit/user_manager.ex
index 33fb39e..3581339 100644
--- a/lib/kmxgit/user_manager.ex
+++ b/lib/kmxgit/user_manager.ex
@@ -33,25 +33,25 @@ defmodule Kmxgit.UserManager do
     update_disk_usage()
     Repo.all from user in User,
       preload: ^@list_preload,
-      order_by: [desc: :name]
+      order_by: [desc_nulls_last: fragment("lower(?)", user.name)]
   end
   def list_users(%{column: "name"}) do
     update_disk_usage()
     Repo.all from user in User,
       preload: ^@list_preload,
-      order_by: :name
+      order_by: [asc_nulls_last: fragment("lower(?)", user.name)]
   end
   def list_users(%{column: "email", reverse: true}) do
     update_disk_usage()
     Repo.all from user in User,
       preload: ^@list_preload,
-      order_by: [desc: :email]
+      order_by: [desc_nulls_last: fragment("lower(?)", user.email)]
   end
   def list_users(%{column: "email"}) do
     update_disk_usage()
     Repo.all from user in User,
       preload: ^@list_preload,
-      order_by: :email
+      order_by: [asc_nulls_last: fragment("lower(?)", user.email)]
   end
   def list_users(%{column: "login", reverse: true}) do
     update_disk_usage()
@@ -59,7 +59,7 @@ defmodule Kmxgit.UserManager do
       join: s in Slug,
       on: s.user_id == user.id,
       preload: ^@list_preload,
-      order_by: [desc: s.slug]
+      order_by: [desc_nulls_last: fragment("lower(?)", s.slug)]
   end
   def list_users(%{column: "login"}) do
     update_disk_usage()
@@ -67,7 +67,7 @@ defmodule Kmxgit.UserManager do
       join: s in Slug,
       on: s.user_id == user.id,
       preload: ^@list_preload,
-      order_by: s.slug
+      order_by: [asc_nulls_last: fragment("lower(?)", s.slug)]
   end
   def list_users(%{column: "du", reverse: true}) do
     update_disk_usage()