refactor(10.2-01): nest framework packages under modules
- Move remaining beach packages and embedded admin assets\n- Rewrite framework, example, build, and gate paths
This commit is contained in:
40
modules/lagoon/paginate.go
Normal file
40
modules/lagoon/paginate.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package lagoon
|
||||
|
||||
// PageMeta is the pagination block of the house REST envelope (DATA-10).
|
||||
type PageMeta struct {
|
||||
CurrentPage int `json:"current_page"`
|
||||
LastPage int `json:"last_page"`
|
||||
PerPage int `json:"per_page"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
// Page wraps already-built row DTOs. There is no links field.
|
||||
type Page[T any] struct {
|
||||
Data []T `json:"data"`
|
||||
Meta PageMeta `json:"meta"`
|
||||
}
|
||||
|
||||
// Paginate builds {data, meta{current_page,last_page,per_page,total}} from
|
||||
// a caller-sliced row set. LastPage is ceil(total/perPage); perPage<=0
|
||||
// yields LastPage 1 instead of dividing by zero. Nil rows become [].
|
||||
func Paginate[T any](rows []T, page, perPage int, total int64) Page[T] {
|
||||
last := 1
|
||||
if perPage > 0 {
|
||||
last = int((total + int64(perPage) - 1) / int64(perPage))
|
||||
if last < 1 {
|
||||
last = 1
|
||||
}
|
||||
}
|
||||
if rows == nil {
|
||||
rows = []T{}
|
||||
}
|
||||
return Page[T]{
|
||||
Data: rows,
|
||||
Meta: PageMeta{
|
||||
CurrentPage: page,
|
||||
LastPage: last,
|
||||
PerPage: perPage,
|
||||
Total: total,
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user