refactor the limit and offset code

This commit is contained in:
Chris Cromer 2023-02-01 21:55:47 -03:00
parent 7cd197ee3a
commit 5cbd6ef227
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
11 changed files with 70 additions and 230 deletions

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"math"
"net/http" "net/http"
"strconv" "strconv"
@ -21,28 +20,10 @@ func ListFrame(writer http.ResponseWriter, request *http.Request, params httprou
queryParams := request.URL.Query() queryParams := request.URL.Query()
limit := 50 limit, offset, err := utils.GetLimitOffset(queryParams)
if queryParams.Get("limit") != "" { if err != nil {
var err error utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
limit, err = strconv.Atoi(queryParams.Get("limit")) return
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
limit = int(math.Min(float64(500), float64(limit)))
limit = int(math.Max(float64(1), float64(limit)))
}
offset := 0
if queryParams.Get("offset") != "" {
var err error
offset, err = strconv.Atoi(queryParams.Get("offset"))
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
offset = int(math.Min(float64(9223372036854775807), float64(offset)))
offset = int(math.Max(float64(0), float64(offset)))
} }
filters := []string{ filters := []string{

View File

@ -4,7 +4,6 @@ import (
"compress/gzip" "compress/gzip"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"math"
"net/http" "net/http"
"strconv" "strconv"
@ -23,28 +22,10 @@ func ListGame(writer http.ResponseWriter, request *http.Request, params httprout
queryParams := request.URL.Query() queryParams := request.URL.Query()
limit := 50 limit, offset, err := utils.GetLimitOffset(queryParams)
if queryParams.Get("limit") != "" { if err != nil {
var err error utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
limit, err = strconv.Atoi(queryParams.Get("limit")) return
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
limit = int(math.Min(float64(500), float64(limit)))
limit = int(math.Max(float64(1), float64(limit)))
}
offset := 0
if queryParams.Get("offset") != "" {
var err error
offset, err = strconv.Atoi(queryParams.Get("offset"))
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
offset = int(math.Min(float64(9223372036854775807), float64(offset)))
offset = int(math.Max(float64(0), float64(offset)))
} }
filters := []string{ filters := []string{

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"math"
"net/http" "net/http"
"strconv" "strconv"
@ -21,28 +20,10 @@ func ListGodotVersion(writer http.ResponseWriter, request *http.Request, params
queryParams := request.URL.Query() queryParams := request.URL.Query()
limit := 50 limit, offset, err := utils.GetLimitOffset(queryParams)
if queryParams.Get("limit") != "" { if err != nil {
var err error utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
limit, err = strconv.Atoi(queryParams.Get("limit")) return
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
limit = int(math.Min(float64(500), float64(limit)))
limit = int(math.Max(float64(1), float64(limit)))
}
offset := 0
if queryParams.Get("offset") != "" {
var err error
offset, err = strconv.Atoi(queryParams.Get("offset"))
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
offset = int(math.Min(float64(9223372036854775807), float64(offset)))
offset = int(math.Max(float64(0), float64(offset)))
} }
filters := []string{ filters := []string{

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"math"
"net/http" "net/http"
"strconv" "strconv"
@ -21,28 +20,10 @@ func ListLevel(writer http.ResponseWriter, request *http.Request, params httprou
queryParams := request.URL.Query() queryParams := request.URL.Query()
limit := 50 limit, offset, err := utils.GetLimitOffset(queryParams)
if queryParams.Get("limit") != "" { if err != nil {
var err error utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
limit, err = strconv.Atoi(queryParams.Get("limit")) return
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
limit = int(math.Min(float64(500), float64(limit)))
limit = int(math.Max(float64(1), float64(limit)))
}
offset := 0
if queryParams.Get("offset") != "" {
var err error
offset, err = strconv.Atoi(queryParams.Get("offset"))
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
offset = int(math.Min(float64(9223372036854775807), float64(offset)))
offset = int(math.Max(float64(0), float64(offset)))
} }
filters := []string{ filters := []string{

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"math"
"net/http" "net/http"
"strconv" "strconv"
@ -21,28 +20,10 @@ func ListObject(writer http.ResponseWriter, request *http.Request, params httpro
queryParams := request.URL.Query() queryParams := request.URL.Query()
limit := 50 limit, offset, err := utils.GetLimitOffset(queryParams)
if queryParams.Get("limit") != "" { if err != nil {
var err error utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
limit, err = strconv.Atoi(queryParams.Get("limit")) return
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
limit = int(math.Min(float64(500), float64(limit)))
limit = int(math.Max(float64(1), float64(limit)))
}
offset := 0
if queryParams.Get("offset") != "" {
var err error
offset, err = strconv.Atoi(queryParams.Get("offset"))
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
offset = int(math.Min(float64(9223372036854775807), float64(offset)))
offset = int(math.Max(float64(0), float64(offset)))
} }
filters := []string{ filters := []string{

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"math"
"net/http" "net/http"
"strconv" "strconv"
@ -21,28 +20,10 @@ func ListObjectName(writer http.ResponseWriter, request *http.Request, params ht
queryParams := request.URL.Query() queryParams := request.URL.Query()
limit := 50 limit, offset, err := utils.GetLimitOffset(queryParams)
if queryParams.Get("limit") != "" { if err != nil {
var err error utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
limit, err = strconv.Atoi(queryParams.Get("limit")) return
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
limit = int(math.Min(float64(500), float64(limit)))
limit = int(math.Max(float64(1), float64(limit)))
}
offset := 0
if queryParams.Get("offset") != "" {
var err error
offset, err = strconv.Atoi(queryParams.Get("offset"))
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
offset = int(math.Min(float64(9223372036854775807), float64(offset)))
offset = int(math.Max(float64(0), float64(offset)))
} }
filters := []string{ filters := []string{

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"math"
"net/http" "net/http"
"strconv" "strconv"
@ -21,28 +20,10 @@ func ListObjectState(writer http.ResponseWriter, request *http.Request, params h
queryParams := request.URL.Query() queryParams := request.URL.Query()
limit := 50 limit, offset, err := utils.GetLimitOffset(queryParams)
if queryParams.Get("limit") != "" { if err != nil {
var err error utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
limit, err = strconv.Atoi(queryParams.Get("limit")) return
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
limit = int(math.Min(float64(500), float64(limit)))
limit = int(math.Max(float64(1), float64(limit)))
}
offset := 0
if queryParams.Get("offset") != "" {
var err error
offset, err = strconv.Atoi(queryParams.Get("offset"))
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
offset = int(math.Min(float64(9223372036854775807), float64(offset)))
offset = int(math.Max(float64(0), float64(offset)))
} }
filters := []string{ filters := []string{

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"math"
"net/http" "net/http"
"strconv" "strconv"
@ -21,28 +20,10 @@ func ListOS(writer http.ResponseWriter, request *http.Request, params httprouter
queryParams := request.URL.Query() queryParams := request.URL.Query()
limit := 50 limit, offset, err := utils.GetLimitOffset(queryParams)
if queryParams.Get("limit") != "" { if err != nil {
var err error utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
limit, err = strconv.Atoi(queryParams.Get("limit")) return
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
limit = int(math.Min(float64(500), float64(limit)))
limit = int(math.Max(float64(1), float64(limit)))
}
offset := 0
if queryParams.Get("offset") != "" {
var err error
offset, err = strconv.Atoi(queryParams.Get("offset"))
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
offset = int(math.Min(float64(9223372036854775807), float64(offset)))
offset = int(math.Max(float64(0), float64(offset)))
} }
filters := []string{ filters := []string{

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"math"
"net/http" "net/http"
"strconv" "strconv"
@ -21,28 +20,10 @@ func ListPlayer(writer http.ResponseWriter, request *http.Request, params httpro
queryParams := request.URL.Query() queryParams := request.URL.Query()
limit := 50 limit, offset, err := utils.GetLimitOffset(queryParams)
if queryParams.Get("limit") != "" { if err != nil {
var err error utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
limit, err = strconv.Atoi(queryParams.Get("limit")) return
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
limit = int(math.Min(float64(500), float64(limit)))
limit = int(math.Max(float64(1), float64(limit)))
}
offset := 0
if queryParams.Get("offset") != "" {
var err error
offset, err = strconv.Atoi(queryParams.Get("offset"))
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
offset = int(math.Min(float64(9223372036854775807), float64(offset)))
offset = int(math.Max(float64(0), float64(offset)))
} }
filters := []string{ filters := []string{

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"math"
"net/http" "net/http"
"strconv" "strconv"
@ -21,28 +20,10 @@ func ListUser(writer http.ResponseWriter, request *http.Request, params httprout
queryParams := request.URL.Query() queryParams := request.URL.Query()
limit := 50 limit, offset, err := utils.GetLimitOffset(queryParams)
if queryParams.Get("limit") != "" { if err != nil {
var err error utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
limit, err = strconv.Atoi(queryParams.Get("limit")) return
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
limit = int(math.Min(float64(500), float64(limit)))
limit = int(math.Max(float64(1), float64(limit)))
}
offset := 0
if queryParams.Get("offset") != "" {
var err error
offset, err = strconv.Atoi(queryParams.Get("offset"))
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
offset = int(math.Min(float64(9223372036854775807), float64(offset)))
offset = int(math.Max(float64(0), float64(offset)))
} }
filters := []string{ filters := []string{

View File

@ -1,9 +1,39 @@
package utils package utils
import ( import (
"math"
"net/url" "net/url"
"strconv"
) )
func GetLimitOffset(queryParams url.Values) (int, int, error) {
limit := 50
if queryParams.Get("limit") != "" {
var err error
limit, err = strconv.Atoi(queryParams.Get("limit"))
if err != nil {
return -1, -1, err
}
limit = int(math.Min(float64(500), float64(limit)))
limit = int(math.Max(float64(1), float64(limit)))
}
offset := 0
if queryParams.Get("offset") != "" {
var err error
offset, err = strconv.Atoi(queryParams.Get("offset"))
if err != nil {
return -1, -1, err
}
offset = int(math.Min(float64(9223372036854775807), float64(offset)))
offset = int(math.Max(float64(0), float64(offset)))
}
return limit, offset, nil
}
func GenerateWhereFilter(filters []string, queryParams url.Values) (map[string]interface{}, error) { func GenerateWhereFilter(filters []string, queryParams url.Values) (map[string]interface{}, error) {
whereClause := make(map[string]interface{}) whereClause := make(map[string]interface{})