Search, filter and group the admin endgames list

The admin list now groups endgames under collapsible Key headers and can be
filtered by Theme (search + tappable chips) and Key. Group counts and
filtering run in SQL, and a group's endgames are fetched only when it is
expanded, so the list stays fast as the dataset grows.

- Add Endgames.list_keys/0, list_key_groups/1 and list_endgames_for_key/2.
- Add the shared ChesstrainerWeb.EndgameList filter bar and grouped list
  component (to be reused by the player picker).
- Rework EndgameLive.Index around server-side group state.

Closes #15
This commit is contained in:
2026-11-02 10:21:22 +01:00
parent ca65207baa
commit d231860f14
5 changed files with 458 additions and 41 deletions
+55
View File
@@ -21,6 +21,61 @@ defmodule Chesstrainer.Endgames do
Repo.all(Endgame)
end
@doc """
Returns the distinct Keys across all endgames, sorted.
"""
def list_keys do
Repo.all(from e in Endgame, distinct: true, select: e.key, order_by: [asc: e.key])
end
@doc """
Returns `{key, count}` groups for endgames matching the filters, sorted by key.
Filters is a map with optional `:themes` (a list of theme ids; an endgame must
carry any one of them) and `:key` (an exact Key).
"""
def list_key_groups(filters \\ %{}) do
Endgame
|> apply_filters(filters)
|> group_by([e], e.key)
|> select([e], {e.key, count(e.id)})
|> order_by([e], asc: e.key)
|> Repo.all()
end
@doc """
Returns the endgames for one Key, matching the filters, with themes preloaded.
"""
def list_endgames_for_key(key, filters \\ %{}) do
Endgame
|> apply_filters(filters)
|> where([e], e.key == ^key)
|> preload(:themes)
|> order_by([e], asc: e.fen)
|> Repo.all()
end
defp apply_filters(query, filters) do
query
|> filter_themes(Map.get(filters, :themes, []))
|> filter_key(Map.get(filters, :key))
end
defp filter_themes(query, []), do: query
defp filter_themes(query, theme_ids) do
endgame_ids =
from(t in "endgames_themes",
where: t.theme_id in ^theme_ids,
select: t.endgame_id
)
where(query, [e], e.id in subquery(endgame_ids))
end
defp filter_key(query, key) when key in [nil, ""], do: query
defp filter_key(query, key), do: where(query, [e], e.key == ^key)
@doc """
Gets a single endgame.