diff --git a/lib/chesstrainer/endgames.ex b/lib/chesstrainer/endgames.ex
index d64efe8..16dd180 100644
--- a/lib/chesstrainer/endgames.ex
+++ b/lib/chesstrainer/endgames.ex
@@ -111,8 +111,13 @@ defmodule Chesstrainer.Endgames do
@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).
+ Filters is a map with optional:
+
+ * `:themes` - a list of theme ids; an endgame must carry any one of them
+ * `:key` - an exact Key
+ * `:with_theme` - `{theme_id, source}`; only endgames linked to the theme
+ with that source
+ * `:without_theme` - a theme id; only endgames not linked to the theme at all
"""
def list_key_groups(filters \\ %{}) do
Endgame
@@ -125,20 +130,31 @@ defmodule Chesstrainer.Endgames do
@doc """
Returns the endgames for one Key, matching the filters, with themes preloaded.
+
+ Accepts `:limit` and `:offset` options for paginating large Key groups.
"""
- def list_endgames_for_key(key, filters \\ %{}) do
+ def list_endgames_for_key(key, filters \\ %{}, opts \\ []) do
+ skip = Keyword.get(opts, :offset, 0)
+
Endgame
|> apply_filters(filters)
|> where([e], e.key == ^key)
|> preload(:themes)
- |> order_by([e], asc: e.fen)
+ |> order_by([e], asc: e.fen, asc: e.id)
+ |> limit_query(Keyword.get(opts, :limit))
+ |> offset(^skip)
|> Repo.all()
end
+ defp limit_query(query, nil), do: query
+ defp limit_query(query, limit), do: limit(query, ^limit)
+
defp apply_filters(query, filters) do
query
|> filter_themes(Map.get(filters, :themes, []))
|> filter_key(Map.get(filters, :key))
+ |> filter_with_theme(Map.get(filters, :with_theme))
+ |> filter_without_theme(Map.get(filters, :without_theme))
end
defp filter_themes(query, []), do: query
@@ -156,6 +172,30 @@ defmodule Chesstrainer.Endgames do
defp filter_key(query, key) when key in [nil, ""], do: query
defp filter_key(query, key), do: where(query, [e], e.key == ^key)
+ defp filter_with_theme(query, nil), do: query
+
+ defp filter_with_theme(query, {theme_id, source}) do
+ endgame_ids =
+ from(t in "endgames_themes",
+ where: t.theme_id == ^theme_id and t.source == ^to_string(source),
+ select: t.endgame_id
+ )
+
+ where(query, [e], e.id in subquery(endgame_ids))
+ end
+
+ defp filter_without_theme(query, nil), do: query
+
+ defp filter_without_theme(query, theme_id) do
+ endgame_ids =
+ from(t in "endgames_themes",
+ where: t.theme_id == ^theme_id,
+ select: t.endgame_id
+ )
+
+ where(query, [e], e.id not in subquery(endgame_ids))
+ end
+
@doc """
Gets a single endgame.
diff --git a/lib/chesstrainer/themes.ex b/lib/chesstrainer/themes.ex
index 756bdaf..5be6ebb 100644
--- a/lib/chesstrainer/themes.ex
+++ b/lib/chesstrainer/themes.ex
@@ -263,6 +263,61 @@ defmodule Chesstrainer.Themes do
:ok
end
+ @link_sources [:curated, :community]
+
+ @doc """
+ Links `theme` to many endgames at once with the given source.
+
+ Defaults to `:curated`, which is what the admin tagging workspace writes.
+ Existing links are left alone, so re-applying is safe. Returns the number of
+ endgames the link was attempted for.
+ """
+ @spec tag_endgames(Theme.t(), [Ecto.UUID.t()], :curated | :community) ::
+ {:ok, non_neg_integer()}
+ def tag_endgames(%Theme{id: theme_id}, endgame_ids, source \\ :curated)
+ when is_list(endgame_ids) and source in @link_sources do
+ now = DateTime.utc_now() |> DateTime.truncate(:second)
+
+ endgame_ids
+ |> Enum.uniq()
+ |> Enum.map(fn endgame_id ->
+ %{
+ endgame_id: Ecto.UUID.dump!(endgame_id),
+ theme_id: theme_id,
+ source: to_string(source),
+ inserted_at: now
+ }
+ end)
+ |> Enum.chunk_every(5_000)
+ |> Enum.each(fn rows ->
+ Repo.insert_all("endgames_themes", rows, on_conflict: :nothing)
+ end)
+
+ {:ok, length(Enum.uniq(endgame_ids))}
+ end
+
+ @doc """
+ Removes a link from `theme` to one endgame, but only if it has the given
+ source.
+
+ The tagging workspace removes `:curated` links only, so a derived or
+ community link can never be deleted by hand. Returns the number removed.
+ """
+ @spec untag_endgame(Theme.t(), Ecto.UUID.t(), :curated | :community) ::
+ {:ok, non_neg_integer()}
+ def untag_endgame(%Theme{id: theme_id}, endgame_id, source \\ :curated)
+ when source in @link_sources do
+ {count, _} =
+ Repo.delete_all(
+ from l in "endgames_themes",
+ where:
+ l.theme_id == ^theme_id and l.endgame_id == type(^endgame_id, :binary_id) and
+ l.source == ^to_string(source)
+ )
+
+ {:ok, count}
+ end
+
@doc """
Replaces the derived theme links for one endgame.
diff --git a/lib/chesstrainer_web/live/admin/theme_live/index.ex b/lib/chesstrainer_web/live/admin/theme_live/index.ex
index 668cb74..7468595 100644
--- a/lib/chesstrainer_web/live/admin/theme_live/index.ex
+++ b/lib/chesstrainer_web/live/admin/theme_live/index.ex
@@ -89,6 +89,12 @@ defmodule ChesstrainerWeb.Admin.ThemeLive.Index do
+ <.link
+ :if={theme.category == :endgame and theme.kind == :technique}
+ navigate={~p"/admin/themes/#{theme.id}/tag"}
+ >
+ Tag
+
<.link navigate={~p"/admin/themes/#{theme.id}/edit"}>Edit
<.link
phx-click="delete"
diff --git a/lib/chesstrainer_web/live/admin/theme_live/tag.ex b/lib/chesstrainer_web/live/admin/theme_live/tag.ex
new file mode 100644
index 0000000..0ed9b26
--- /dev/null
+++ b/lib/chesstrainer_web/live/admin/theme_live/tag.ex
@@ -0,0 +1,460 @@
+defmodule ChesstrainerWeb.Admin.ThemeLive.Tag do
+ @moduledoc """
+ Workspace for applying a technique theme to endgames by family.
+
+ Candidates are grouped by Key so an admin tags a whole family rather than
+ individual rows. Boards are previews only; nothing here starts a puzzle.
+ Only technique themes can be tagged, because material themes are derived and
+ a hand-made link would be erased by the next refresh.
+ """
+
+ use ChesstrainerWeb, :live_view
+
+ alias Chesstrainer.Endgames
+ alias Chesstrainer.Game
+ alias Chesstrainer.Themes
+
+ import ChesstrainerWeb.Board.BoardComponent
+
+ @page_size 24
+
+ @impl true
+ def render(assigns) do
+ ~H"""
+
+
+
+ <.header>
+ Tag “{@theme.name}”
+ <:subtitle>
+ Pick a family, preview the positions and apply the theme in bulk. The links are
+ recorded as curated, so derivation never overwrites them.
+
+ <:actions>
+ <.button navigate={~p"/admin/themes"}>
+ <.icon name="hero-arrow-left" /> All themes
+
+
+
+
+ <.form
+ for={@theme_form}
+ id="theme-workspace-form"
+ phx-change="select_theme"
+ class="mb-6 max-w-md"
+ >
+ <.input
+ field={@theme_form[:theme_id]}
+ type="select"
+ label="Technique theme"
+ options={@theme_options}
+ />
+
+
+