mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
Add user profile hub with account, settings and data export (#103)
This commit is contained in:
@@ -70,6 +70,39 @@ defmodule Chesstrainer.Attempts do
|
||||
|> Repo.one()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Aggregated attempt totals for `user_id`.
|
||||
|
||||
Returns a map with `:attempts`, `:solved`, `:failed` and `:endgames` (the
|
||||
number of distinct puzzles tried).
|
||||
"""
|
||||
def stats_for_user(user_id) do
|
||||
Attempt
|
||||
|> where([a], a.user_id == ^user_id)
|
||||
|> select([a], %{
|
||||
attempts: count(a.id),
|
||||
solved: filter(count(a.id), a.outcome == :solved),
|
||||
failed: filter(count(a.id), a.outcome == :failed),
|
||||
endgames: count(a.endgame_id, :distinct)
|
||||
})
|
||||
|> Repo.one()
|
||||
end
|
||||
|
||||
@doc """
|
||||
The attempts of `user_id`, newest first, with each endgame preloaded.
|
||||
|
||||
Pass `limit: n` to cap the history (the profile page shows a recent window;
|
||||
exports pass no limit).
|
||||
"""
|
||||
def list_attempts_for_user(user_id, opts \\ []) do
|
||||
Attempt
|
||||
|> where([a], a.user_id == ^user_id)
|
||||
|> order_by([a], desc: a.inserted_at)
|
||||
|> maybe_limit(Keyword.get(opts, :limit))
|
||||
|> preload(:endgame)
|
||||
|> Repo.all()
|
||||
end
|
||||
|
||||
@doc """
|
||||
The distinct endgame ids `user_id` has attempted in the last `days` days.
|
||||
|
||||
@@ -87,4 +120,7 @@ defmodule Chesstrainer.Attempts do
|
||||
end
|
||||
|
||||
defp rated(query), do: where(query, [a], not is_nil(a.rated_at))
|
||||
|
||||
defp maybe_limit(query, nil), do: query
|
||||
defp maybe_limit(query, limit) when is_integer(limit), do: limit(query, ^limit)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user