Commit f7aa45b4a6ef7bec5cb6166f311e912bc09f6e77

Thomas de Grivel 2022-01-28T11:31:14

[admin] search repositories

diff --git a/lib/kmxgit/repository_manager.ex b/lib/kmxgit/repository_manager.ex
index 644edba..fb9742c 100644
--- a/lib/kmxgit/repository_manager.ex
+++ b/lib/kmxgit/repository_manager.ex
@@ -22,10 +22,18 @@ defmodule Kmxgit.RepositoryManager do
     |> preload([members: :slug,
                organisation: [:slug, users: :slug],
                user: :slug])
+    |> search(params)
     |> index_order_by(params)
     |> Repo.all()
   end
 
+  def search(query, %IndexParams{search: search}) do
+    query
+    |> where([r, o, os, u, us],
+         ilike(fragment("concat(?, ?, '/', ?)", os.slug, us.slug, r.slug),
+           ^"%#{search}%"))
+  end
+
   def index_order_by(query, %IndexParams{column: "id", reverse: true}) do
     order_by(query, [desc: :id])
   end
diff --git a/lib/kmxgit_web/controllers/admin/repository_controller.ex b/lib/kmxgit_web/controllers/admin/repository_controller.ex
index ba4c5f3..a61b1f8 100644
--- a/lib/kmxgit_web/controllers/admin/repository_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/repository_controller.ex
@@ -17,6 +17,8 @@ defmodule KmxgitWeb.Admin.RepositoryController do
     conn
     |> assign(:repos, repos)
     |> assign(:index, index_params)
+    |> assign(:search, params["search"])
+    |> assign(:search_action, Routes.admin_repository_path(conn, :index, sort: params["sort"], search: params["search"]))
     |> render("index.html")
   end
 
diff --git a/lib/kmxgit_web/templates/admin/repository/index.html.heex b/lib/kmxgit_web/templates/admin/repository/index.html.heex
index d626179..c5fd04a 100644
--- a/lib/kmxgit_web/templates/admin/repository/index.html.heex
+++ b/lib/kmxgit_web/templates/admin/repository/index.html.heex
@@ -1,13 +1,15 @@
 <div class="container-fluid">
   <h1>Repositories</h1>
 
+  <%= render(KmxgitWeb.LayoutView, "search.html", assigns) %>
+
   <table class="table admin-index">
     <thead>
       <tr>
-        <th><%= link gettext("Id"), to: Routes.admin_repository_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("Owner"), to: Routes.admin_repository_path(@conn, :index, sort: "owner#{if @index.column == "owner" && !@index.reverse, do: "-"}") %><%= if @index.column == "owner" 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_repository_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_repository_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><%= link gettext("Id"), to: Routes.admin_repository_path(@conn, :index, search: @search, 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("Owner"), to: Routes.admin_repository_path(@conn, :index, search: @search, sort: "owner#{if @index.column == "owner" && !@index.reverse, do: "-"}") %><%= if @index.column == "owner" 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_repository_path(@conn, :index, search: @search, 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_repository_path(@conn, :index, search: @search, 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>
diff --git a/lib/kmxgit_web/templates/layout/search.html.heex b/lib/kmxgit_web/templates/layout/search.html.heex
new file mode 100644
index 0000000..d1671e7
--- /dev/null
+++ b/lib/kmxgit_web/templates/layout/search.html.heex
@@ -0,0 +1,6 @@
+<%= form_for @conn, @search_action, [method: :get], fn f -> %>
+  <div class="input-group mb-3">
+    <%= text_input f, :search, class: "form-control" %>
+    <%= submit gettext("Search"), class: "btn btn-primary" %>
+  </div>
+<% end %>