From 750d4948e81e1ac6b6a63386b96f8c60828891e5 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sat, 4 Jan 2025 20:28:41 +0100 Subject: refactor: extract pagination into module --- internal/pagination/pagination.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 internal/pagination/pagination.go (limited to 'internal/pagination/pagination.go') 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 + } +} -- cgit 1.4.1