From ccf1c499ad9c38c4b1a0f48659cf1314dc350efe Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Sat, 1 Aug 2020 18:57:33 +0530 Subject: [PATCH] Refactor pagination constants --- campaigns.go | 2 +- handlers.go | 14 ++------------ lists.go | 2 +- subscribers.go | 2 +- 4 files changed, 5 insertions(+), 15 deletions(-) diff --git a/campaigns.go b/campaigns.go index af1967c..7c33e89 100644 --- a/campaigns.go +++ b/campaigns.go @@ -69,7 +69,7 @@ var ( func handleGetCampaigns(c echo.Context) error { var ( app = c.Get("app").(*App) - pg = getPagination(c.QueryParams()) + pg = getPagination(c.QueryParams(), 20, 50) out campsWrap id, _ = strconv.Atoi(c.Param("id")) diff --git a/handlers.go b/handlers.go index cf6a462..fd84c9a 100644 --- a/handlers.go +++ b/handlers.go @@ -12,12 +12,6 @@ import ( const ( // stdInputMaxLen is the maximum allowed length for a standard input field. 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 { @@ -191,12 +185,8 @@ func subscriberExists(next echo.HandlerFunc, params ...string) echo.HandlerFunc } // getPagination takes form values and extracts pagination values from it. -func getPagination(q url.Values) pagination { - var ( - page, _ = strconv.Atoi(q.Get("page")) - perPage = defaultPerPage - ) - +func getPagination(q url.Values, perPage, maxPerPage int) pagination { + page, _ := strconv.Atoi(q.Get("page")) pp := q.Get("per_page") if pp == "all" { // No limit. diff --git a/lists.go b/lists.go index 593e8ed..076988f 100644 --- a/lists.go +++ b/lists.go @@ -26,7 +26,7 @@ func handleGetLists(c echo.Context) error { app = c.Get("app").(*App) out listsWrap - pg = getPagination(c.QueryParams()) + pg = getPagination(c.QueryParams(), 20, 50) listID, _ = strconv.Atoi(c.Param("id")) single = false ) diff --git a/subscribers.go b/subscribers.go index ed5683d..3ec6821 100644 --- a/subscribers.go +++ b/subscribers.go @@ -83,7 +83,7 @@ func handleGetSubscriber(c echo.Context) error { func handleQuerySubscribers(c echo.Context) error { var ( app = c.Get("app").(*App) - pg = getPagination(c.QueryParams()) + pg = getPagination(c.QueryParams(), 30, 100) // Limit the subscribers to a particular list? listID, _ = strconv.Atoi(c.FormValue("list_id"))