Refactor pagination constants

This commit is contained in:
Kailash Nadh 2020-08-01 18:57:33 +05:30
parent 0db95799fb
commit ccf1c499ad
4 changed files with 5 additions and 15 deletions

View File

@ -69,7 +69,7 @@ var (
func handleGetCampaigns(c echo.Context) error { func handleGetCampaigns(c echo.Context) error {
var ( var (
app = c.Get("app").(*App) app = c.Get("app").(*App)
pg = getPagination(c.QueryParams()) pg = getPagination(c.QueryParams(), 20, 50)
out campsWrap out campsWrap
id, _ = strconv.Atoi(c.Param("id")) id, _ = strconv.Atoi(c.Param("id"))

View File

@ -12,12 +12,6 @@ import (
const ( const (
// stdInputMaxLen is the maximum allowed length for a standard input field. // stdInputMaxLen is the maximum allowed length for a standard input field.
stdInputMaxLen = 200 stdInputMaxLen = 200
// defaultPerPage is the default number of results returned in an GET call.
defaultPerPage = 20
// maxPerPage is the maximum number of allowed for paginated records.
maxPerPage = 100
) )
type okResp struct { type okResp struct {
@ -191,12 +185,8 @@ func subscriberExists(next echo.HandlerFunc, params ...string) echo.HandlerFunc
} }
// getPagination takes form values and extracts pagination values from it. // getPagination takes form values and extracts pagination values from it.
func getPagination(q url.Values) pagination { func getPagination(q url.Values, perPage, maxPerPage int) pagination {
var ( page, _ := strconv.Atoi(q.Get("page"))
page, _ = strconv.Atoi(q.Get("page"))
perPage = defaultPerPage
)
pp := q.Get("per_page") pp := q.Get("per_page")
if pp == "all" { if pp == "all" {
// No limit. // No limit.

View File

@ -26,7 +26,7 @@ func handleGetLists(c echo.Context) error {
app = c.Get("app").(*App) app = c.Get("app").(*App)
out listsWrap out listsWrap
pg = getPagination(c.QueryParams()) pg = getPagination(c.QueryParams(), 20, 50)
listID, _ = strconv.Atoi(c.Param("id")) listID, _ = strconv.Atoi(c.Param("id"))
single = false single = false
) )

View File

@ -83,7 +83,7 @@ func handleGetSubscriber(c echo.Context) error {
func handleQuerySubscribers(c echo.Context) error { func handleQuerySubscribers(c echo.Context) error {
var ( var (
app = c.Get("app").(*App) app = c.Get("app").(*App)
pg = getPagination(c.QueryParams()) pg = getPagination(c.QueryParams(), 30, 100)
// Limit the subscribers to a particular list? // Limit the subscribers to a particular list?
listID, _ = strconv.Atoi(c.FormValue("list_id")) listID, _ = strconv.Atoi(c.FormValue("list_id"))