diff options
author | Simponic <loganhunt@simponic.xyz> | 2023-01-20 23:12:23 -0700 |
---|---|---|
committer | Simponic <loganhunt@simponic.xyz> | 2023-01-20 23:12:23 -0700 |
commit | 06f1ca76037397fb61c69319802ed029ac73e715 (patch) | |
tree | 37cdf1ebf5fcbaefda58d377bb6b4e3a8a11410b /lib/chessh/ssh/client/menus/game_selector.ex | |
parent | e0058fedfb191db8802a95548cbaf96b6fe58f80 (diff) | |
download | chessh-06f1ca76037397fb61c69319802ed029ac73e715.tar.gz chessh-06f1ca76037397fb61c69319802ed029ac73e715.zip |
Add pagination menus
Diffstat (limited to 'lib/chessh/ssh/client/menus/game_selector.ex')
-rw-r--r-- | lib/chessh/ssh/client/menus/game_selector.ex | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/chessh/ssh/client/menus/game_selector.ex b/lib/chessh/ssh/client/menus/game_selector.ex new file mode 100644 index 0000000..7792082 --- /dev/null +++ b/lib/chessh/ssh/client/menus/game_selector.ex @@ -0,0 +1,24 @@ +defmodule Chessh.SSH.Client.GameSelector do + import Ecto.Query + alias Chessh.Repo + + def paginate_ish_query(query, current_id, direction) do + sorted_query = + if direction == :desc, + do: from(g in query, order_by: [desc: g.id]), + else: from(g in query, order_by: [asc: g.id]) + + results = + if !is_nil(current_id) do + if direction == :desc, + do: from(g in sorted_query, where: g.id < ^current_id), + else: from(g in sorted_query, where: g.id > ^current_id) + else + sorted_query + end + |> Repo.all() + |> Repo.preload([:light_player, :dark_player]) + + if direction == :desc, do: results, else: Enum.reverse(results) + end +end |