budgit/internal/service/pagination.go
juancwu 45fcecdc04
All checks were successful
Deploy / build-and-deploy (push) Successful in 3m45s
chore: refactor
2026-03-14 16:39:07 +00:00

18 lines
485 B
Go

package service
// Paginate calculates pagination values from a page number, total count, and page size.
// Returns the adjusted page, total pages, and offset for the query.
func Paginate(page, total, perPage int) (adjustedPage, totalPages, offset int) {
totalPages = (total + perPage - 1) / perPage
if totalPages < 1 {
totalPages = 1
}
if page < 1 {
page = 1
}
if page > totalPages {
page = totalPages
}
offset = (page - 1) * perPage
return page, totalPages, offset
}