about summary refs log tree commit diff stats
path: root/internal/pagination/pagination.go
diff options
context:
space:
mode:
authorAlan Pearce2025-01-04 20:28:41 +0100
committerAlan Pearce2025-01-04 20:28:41 +0100
commit750d4948e81e1ac6b6a63386b96f8c60828891e5 (patch)
tree8354b40cfb28bc154c89b47e37c74fca7f479fb6 /internal/pagination/pagination.go
parent3d9e6998177f7fc8e971df4913c3a880ff911c99 (diff)
downloadsearchix-750d4948e81e1ac6b6a63386b96f8c60828891e5.tar.lz
searchix-750d4948e81e1ac6b6a63386b96f8c60828891e5.tar.zst
searchix-750d4948e81e1ac6b6a63386b96f8c60828891e5.zip
refactor: extract pagination into module
Diffstat (limited to 'internal/pagination/pagination.go')
-rw-r--r--internal/pagination/pagination.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/internal/pagination/pagination.go b/internal/pagination/pagination.go
new file mode 100644
index 0000000..4d41587
--- /dev/null
+++ b/internal/pagination/pagination.go
@@ -0,0 +1,31 @@
+package pagination
+
+type Pagination struct {
+	total uint64
+
+	From int
+
+	Size,
+	Current,
+	Prev,
+	Next int
+
+	Needed bool
+}
+
+func New(page int, pageSize int) *Pagination {
+	return &Pagination{
+		Current: page,
+		From:    (page - 1) * pageSize,
+		Size:    pageSize,
+	}
+}
+
+func (p *Pagination) SetResults(total uint64) {
+	p.total = total
+	p.Needed = p.total > uint64(p.Size)
+	if uint64(p.Current*p.Size) <= p.total {
+		p.Next = p.Current + 1
+		p.Prev = p.Current - 1
+	}
+}