Commit 9ffd646805ac4cd1df0a9ed299136765ca831507

Thomas de Grivel 2022-01-28T13:56:50

fix pagination

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
diff --git a/lib/kmxgit/pagination.ex b/lib/kmxgit/pagination.ex
index 84422fb..55f3c56 100644
--- a/lib/kmxgit/pagination.ex
+++ b/lib/kmxgit/pagination.ex
@@ -25,12 +25,13 @@ defmodule Kmxgit.Pagination do
     count = Repo.one(from(t in subquery(query), select: count("*")))
     count_pages = Float.ceil(count / per) |> trunc()
     last_page = if (page < count_pages - 1), do: count_pages
-    first = (page - 1) * per + 1
+    first = if count > 0, do: (page - 1) * per + 1, else: 0
+    last = if count > 0, do: first + length(result) - 1, else: 0
     %{count: count,
       count_pages: count_pages,
       first: first,
       first_page: first_page,
-      last: first + length(result) - 1,
+      last: last,
       last_page: last_page,
       next_page: next_page,
       page: page,