refactor the limit and offset code

This commit is contained in:
Chris Cromer 2023-02-01 21:55:47 -03:00 committed by Martin Araneda
parent bb735afe33
commit c56f3abf88
11 changed files with 70 additions and 230 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,9 +1,39 @@
package utils
import (
"math"
"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) {
whereClause := make(map[string]interface{})