forked from mtusi2026/mtusi2026lab4
30 lines
798 B
Go
30 lines
798 B
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"mtusi2026lab4/internal/transport/http/response"
|
|
)
|
|
|
|
func parsePagination(w http.ResponseWriter, r *http.Request, requestID string) (int, string, bool) {
|
|
query := r.URL.Query()
|
|
limit := 50
|
|
if query.Has("limit") {
|
|
parsed, err := strconv.Atoi(query.Get("limit"))
|
|
if err != nil || parsed < 1 || parsed > 100 {
|
|
response.WriteError(w, http.StatusBadRequest, response.CodeInvalidRequest,
|
|
"limit must be an integer between 1 and 100", requestID)
|
|
return 0, "", false
|
|
}
|
|
limit = parsed
|
|
}
|
|
cursor := query.Get("cursor")
|
|
if query.Has("cursor") && cursor == "" {
|
|
response.WriteError(w, http.StatusBadRequest, response.CodeInvalidRequest,
|
|
"cursor must not be empty", requestID)
|
|
return 0, "", false
|
|
}
|
|
return limit, cursor, true
|
|
}
|