Browse Source
* Add pagination for admin social list * Use Paginated API for followers tab on frontendpull/1759/head
12 changed files with 118 additions and 38 deletions
@ -0,0 +1,7 @@ |
|||||||
|
package controllers |
||||||
|
|
||||||
|
// PaginatedResponse is a structure for returning a total count with results.
|
||||||
|
type PaginatedResponse struct { |
||||||
|
Total int `json:"total"` |
||||||
|
Results interface{} `json:"results"` |
||||||
|
} |
@ -0,0 +1,39 @@ |
|||||||
|
package middleware |
||||||
|
|
||||||
|
import ( |
||||||
|
"net/http" |
||||||
|
"strconv" |
||||||
|
) |
||||||
|
|
||||||
|
// PaginatedHandlerFunc is a handler for endpoints that require pagination.
|
||||||
|
type PaginatedHandlerFunc func(int, int, http.ResponseWriter, *http.Request) |
||||||
|
|
||||||
|
// HandlePagination is a middleware handler that pulls pagination values
|
||||||
|
// and passes them along.
|
||||||
|
func HandlePagination(handler PaginatedHandlerFunc) http.HandlerFunc { |
||||||
|
return func(w http.ResponseWriter, r *http.Request) { |
||||||
|
// Default 50 items per page
|
||||||
|
limitString := r.URL.Query().Get("limit") |
||||||
|
if limitString == "" { |
||||||
|
limitString = "50" |
||||||
|
} |
||||||
|
limit, err := strconv.Atoi(limitString) |
||||||
|
if err != nil { |
||||||
|
w.WriteHeader(http.StatusBadRequest) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
// Default first page 0
|
||||||
|
offsetString := r.URL.Query().Get("offset") |
||||||
|
if offsetString == "" { |
||||||
|
offsetString = "0" |
||||||
|
} |
||||||
|
offset, err := strconv.Atoi(offsetString) |
||||||
|
if err != nil { |
||||||
|
w.WriteHeader(http.StatusBadRequest) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
handler(offset, limit, w, r) |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue