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 ( import (
"encoding/json" "encoding/json"
"math"
"net/http" "net/http"
"strconv" "strconv"
@ -21,29 +20,11 @@ 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") != "" {
var err error
limit, err = strconv.Atoi(queryParams.Get("limit"))
if err != nil { if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return 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{
"game_id", "game_id",

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,29 +22,11 @@ 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") != "" {
var err error
limit, err = strconv.Atoi(queryParams.Get("limit"))
if err != nil { if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return 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{
"player_id", "player_id",

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"math"
"net/http" "net/http"
"strconv" "strconv"
@ -21,29 +20,11 @@ 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") != "" {
var err error
limit, err = strconv.Atoi(queryParams.Get("limit"))
if err != nil { if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return 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{
"major", "major",

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"math"
"net/http" "net/http"
"strconv" "strconv"
@ -21,29 +20,11 @@ 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") != "" {
var err error
limit, err = strconv.Atoi(queryParams.Get("limit"))
if err != nil { if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return 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{
"name", "name",

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"math"
"net/http" "net/http"
"strconv" "strconv"
@ -21,29 +20,11 @@ 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") != "" {
var err error
limit, err = strconv.Atoi(queryParams.Get("limit"))
if err != nil { if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return 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{
"frame_id", "frame_id",

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"math"
"net/http" "net/http"
"strconv" "strconv"
@ -21,29 +20,11 @@ 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") != "" {
var err error
limit, err = strconv.Atoi(queryParams.Get("limit"))
if err != nil { if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return 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{
"name", "name",

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"math"
"net/http" "net/http"
"strconv" "strconv"
@ -21,29 +20,11 @@ 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") != "" {
var err error
limit, err = strconv.Atoi(queryParams.Get("limit"))
if err != nil { if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return 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{
"name", "name",

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"math"
"net/http" "net/http"
"strconv" "strconv"
@ -21,29 +20,11 @@ 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") != "" {
var err error
limit, err = strconv.Atoi(queryParams.Get("limit"))
if err != nil { if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return 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{
"name", "name",

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"math"
"net/http" "net/http"
"strconv" "strconv"
@ -21,29 +20,11 @@ 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") != "" {
var err error
limit, err = strconv.Atoi(queryParams.Get("limit"))
if err != nil { if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return 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{
"rut", "rut",

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"math"
"net/http" "net/http"
"strconv" "strconv"
@ -21,29 +20,11 @@ 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") != "" {
var err error
limit, err = strconv.Atoi(queryParams.Get("limit"))
if err != nil { if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return 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{
"name", "name",

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{})