Skip to content

Commit 7f0cd11

Browse files
committed
Cursor Scroll Paginator added
1 parent 7738f60 commit 7f0cd11

File tree

6 files changed

+13
-6
lines changed

6 files changed

+13
-6
lines changed

Dockerfile

-1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,4 @@ RUN mix compile \
3838
RUN useradd -m myuser
3939
USER myuser
4040

41-
4241
CMD MIX_ENV=prod mix phx.server

lib/todo_backend/repo.ex

+2
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ defmodule TodoBackend.Repo do
22
use Ecto.Repo,
33
otp_app: :todo_backend,
44
adapter: Ecto.Adapters.Postgres
5+
6+
use Paginator
57
end

lib/todo_backend/task/repository/todo_repo.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ defmodule TodoBackend.Task.Repository.TodoRepo do
1818
1919
"""
2020
def list_todos do
21-
Repo.all(Todo)
22-
|> Repo.preload(:user)
21+
query = from(t in Todo, preload: [:user], order_by: [asc: t.inserted_at, asc: t.id], select: t)
22+
Repo.paginate(query, cursor_fields: [:inserted_at, :id], limit: 4)
2323
end
2424

2525
@doc """

lib/todo_backend_web/controllers/todo_controller.ex

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ defmodule TodoBackendWeb.TodoController do
88
action_fallback TodoBackendWeb.FallbackController
99

1010
def index(conn, _params) do
11-
todos = TodoRepo.list_todos()
12-
render(conn, "index.json", todos: todos)
11+
%{entries: entries, metadata: metadata} = TodoRepo.list_todos()
12+
IO.inspect(metadata)
13+
renwder(conn, "index.json", todos: entries)
1314
end
1415

1516
def create(conn, %{"user_id" => user_id, "todo" => todo_params}) do

mix.exs

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ defmodule TodoBackend.MixProject do
4343
{:plug_cowboy, "~> 2.0"},
4444
{:comeonin, "~> 5.1.2"},
4545
{:argon2_elixir, "~> 2.0.5"},
46-
{:guardian, "~> 1.2.1"}
46+
{:guardian, "~> 1.2.1"},
47+
{:paginator, "~> 0.6"}
4748
]
4849
end
4950

mix.lock

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
2525
"mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [:mix], [], "hexpm"},
2626
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm"},
27+
"paginator": {:hex, :paginator, "0.6.0", "bc2c01abdd98281ff39b6a7439cf540091122a7927bdaabc167c61d4508f9cbb", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.0", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.13", [hex: :postgrex, repo: "hexpm", optional: true]}], "hexpm"},
2728
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"},
2829
"phoenix": {:hex, :phoenix, "1.4.9", "746d098e10741c334d88143d3c94cab1756435f94387a63441792e66ec0ee974", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.8.1 or ~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm"},
2930
"phoenix_ecto": {:hex, :phoenix_ecto, "4.0.0", "c43117a136e7399ea04ecaac73f8f23ee0ffe3e07acfcb8062fe5f4c9f0f6531", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.9", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
@@ -35,6 +36,9 @@
3536
"poison": {:hex, :poison, "4.0.1", "bcb755a16fac91cad79bfe9fc3585bb07b9331e50cfe3420a24bcc2d735709ae", [:mix], [], "hexpm"},
3637
"postgrex": {:hex, :postgrex, "0.15.0", "dd5349161019caeea93efa42f9b22f9d79995c3a86bdffb796427b4c9863b0f0", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
3738
"ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm"},
39+
"scrivener": {:hex, :scrivener, "2.7.0", "fa94cdea21fad0649921d8066b1833d18d296217bfdf4a5389a2f45ee857b773", [:mix], [], "hexpm"},
40+
"scrivener_ecto": {:hex, :scrivener_ecto, "2.2.0", "53d5f1ba28f35f17891cf526ee102f8f225b7024d1cdaf8984875467158c9c5e", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:scrivener, "~> 2.4", [hex: :scrivener, repo: "hexpm", optional: false]}], "hexpm"},
41+
"scrivener_list": {:hex, :scrivener_list, "2.0.1", "2b3b5c6aaf21d13b76071e755af498b641f37a069e34e68585ba4c624095d719", [:mix], [{:scrivener_ecto, "~> 1.0 or ~> 2.0", [hex: :scrivener_ecto, repo: "hexpm", optional: false]}], "hexpm"},
3842
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"},
3943
"telemetry": {:hex, :telemetry, "0.4.0", "8339bee3fa8b91cb84d14c2935f8ecf399ccd87301ad6da6b71c09553834b2ab", [:rebar3], [], "hexpm"},
4044
"timex": {:hex, :timex, "3.6.1", "efdf56d0e67a6b956cc57774353b0329c8ab7726766a11547e529357ffdc1d56", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 0.1.8 or ~> 0.5 or ~> 1.0.0", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm"},

0 commit comments

Comments
 (0)