2021-08-03 01:52:46 +00:00
|
|
|
package website
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"git.handmade.network/hmn/hmn/src/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
Parses a path param as a page number, and returns the parsed result and
|
|
|
|
a value indicating whether parsing was successful.
|
|
|
|
|
|
|
|
The returned page number is always valid, even when parsing fails. If
|
|
|
|
parsing fails (ok is false), you should redirect to the returned
|
|
|
|
page number.
|
|
|
|
*/
|
|
|
|
func ParsePageNumber(
|
|
|
|
c *RequestContext,
|
|
|
|
paramName string,
|
|
|
|
numPages int,
|
|
|
|
) (page int, ok bool) {
|
2022-06-14 19:52:50 +00:00
|
|
|
page = 1
|
2021-08-03 01:52:46 +00:00
|
|
|
if pageString, hasPage := c.PathParams[paramName]; hasPage && pageString != "" {
|
|
|
|
if pageParsed, err := strconv.Atoi(pageString); err == nil {
|
|
|
|
page = pageParsed
|
|
|
|
} else {
|
|
|
|
return 1, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if page < 1 || numPages < page {
|
|
|
|
return utils.IntClamp(1, page, numPages), false
|
|
|
|
}
|
|
|
|
|
|
|
|
return page, true
|
|
|
|
}
|