diff --git a/lib/kmxgit/organisation_manager.ex b/lib/kmxgit/organisation_manager.ex
index bd15b90..2086c49 100644
--- a/lib/kmxgit/organisation_manager.ex
+++ b/lib/kmxgit/organisation_manager.ex
@@ -7,14 +7,72 @@ defmodule Kmxgit.OrganisationManager do
alias Kmxgit.SlugManager.Slug
alias Kmxgit.UserManager
- def list_organisations do
+ @list_preload [:owned_repositories,
+ :slug]
+
+ def list_organisations() do
Repo.all from org in Organisation,
join: s in Slug,
on: s.organisation_id == org.id,
- preload: [:owned_repositories,
- :slug],
+ preload: ^@list_preload,
order_by: s.slug
end
+ def list_organisations(%{sort: "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(%{sort: "du"}) do
+ update_disk_usage()
+ Repo.all from org in Organisation,
+ preload: ^@list_preload,
+ order_by: :disk_usage
+ end
+ def list_organisations(%{sort: "id", reverse: true}) do
+ Repo.all from org in Organisation,
+ preload: ^@list_preload,
+ order_by: [desc: :id]
+ end
+ def list_organisations(%{sort: "id"}) do
+ Repo.all from org in Organisation,
+ preload: ^@list_preload,
+ order_by: :id
+ end
+ def list_organisations(%{sort: "name", reverse: true}) do
+ Repo.all from org in Organisation,
+ preload: ^@list_preload,
+ order_by: [desc: :name]
+ end
+ def list_organisations(%{sort: "name"}) do
+ Repo.all from org in Organisation,
+ preload: ^@list_preload,
+ order_by: :name
+ end
+ def list_organisations(%{sort: "slug", reverse: true}) do
+ Repo.all from org in Organisation,
+ join: s in Slug,
+ on: s.organisation_id == org.id,
+ preload: ^@list_preload,
+ order_by: [desc: s.slug]
+ end
+ def list_organisations(%{sort: "slug"}) do
+ Repo.all from org in Organisation,
+ join: s in Slug,
+ on: s.organisation_id == org.id,
+ preload: ^@list_preload,
+ order_by: s.slug
+ end
+
+ def update_disk_usage() do
+ orgs = Repo.all(from org in Organisation, preload: :slug)
+ |> Enum.map(fn org ->
+ org
+ |> Ecto.Changeset.cast(%{}, [])
+ |> Ecto.Changeset.put_change(:disk_usage, Organisation.disk_usage(org))
+ |> Repo.update!()
+ end)
+ end
def put_disk_usage(org = %Organisation{}) do
%Organisation{org | disk_usage: Organisation.disk_usage(org)}
diff --git a/lib/kmxgit/organisation_manager/organisation.ex b/lib/kmxgit/organisation_manager/organisation.ex
index 53d9321..f9065c2 100644
--- a/lib/kmxgit/organisation_manager/organisation.ex
+++ b/lib/kmxgit/organisation_manager/organisation.ex
@@ -10,7 +10,7 @@ defmodule Kmxgit.OrganisationManager.Organisation do
schema "organisations" do
field :description, :string
- field :disk_usage, :integer, virtual: true, default: 0
+ field :disk_usage, :integer, default: 0
field :name, :string
has_many :owned_repositories, Repository
many_to_many :users, User, join_through: "users_organisations", on_replace: :delete, on_delete: :delete_all
diff --git a/lib/kmxgit_web/controllers/admin/organisation_controller.ex b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
index eeb82d7..fe5baca 100644
--- a/lib/kmxgit_web/controllers/admin/organisation_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
@@ -7,15 +7,18 @@ defmodule KmxgitWeb.Admin.OrganisationController do
alias KmxgitWeb.ErrorView
def index(conn, params) do
- {sort, rev} = case String.split(params["sort"], "-") do
- [sort, _] -> {sort, true}
- [sort] -> {sort, false}
- _ -> {nil, false}
- end
- orgs = OrganisationManager.list_organisations()
- |> OrganisationManager.put_disk_usage()
- |> sort_organisations(sort)
- orgs = if rev, do: Enum.reverse(orgs), else: orgs
+ sort = params["sort"]
+ {sort, rev} = if sort do
+ case String.split(sort, "-") do
+ [sort, _] -> {sort, true}
+ [sort] -> {sort, false}
+ _ -> {nil, false}
+ end
+ else
+ {nil, false}
+ end
+ sort = sort || "id"
+ orgs = OrganisationManager.list_organisations(%{sort: sort, reverse: rev})
conn
|> assign(:orgs, orgs)
|> assign(:rev, rev)
diff --git a/priv/repo/migrations/20220127093116_add_disk_usage_to_organisations.exs b/priv/repo/migrations/20220127093116_add_disk_usage_to_organisations.exs
new file mode 100644
index 0000000..bfeeec6
--- /dev/null
+++ b/priv/repo/migrations/20220127093116_add_disk_usage_to_organisations.exs
@@ -0,0 +1,9 @@
+defmodule Kmxgit.Repo.Migrations.AddDiskUsageToOrganisations do
+ use Ecto.Migration
+
+ def change do
+ alter table(:organisations) do
+ add :disk_usage, :integer, default: 0
+ end
+ end
+end