From c56f3abf88d93b028b0f5b2fb323c80ad3250eb4 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 1 Feb 2023 21:55:47 -0300 Subject: [PATCH] refactor the limit and offset code --- backend/controllers/frame.go | 27 ++++--------------------- backend/controllers/game.go | 27 ++++--------------------- backend/controllers/godot_version.go | 27 ++++--------------------- backend/controllers/level.go | 27 ++++--------------------- backend/controllers/object.go | 27 ++++--------------------- backend/controllers/object_name.go | 27 ++++--------------------- backend/controllers/object_state.go | 27 ++++--------------------- backend/controllers/os.go | 27 ++++--------------------- backend/controllers/player.go | 27 ++++--------------------- backend/controllers/user.go | 27 ++++--------------------- backend/utils/database_filters.go | 30 ++++++++++++++++++++++++++++ 11 files changed, 70 insertions(+), 230 deletions(-) diff --git a/backend/controllers/frame.go b/backend/controllers/frame.go index d12a349..98b1728 100644 --- a/backend/controllers/frame.go +++ b/backend/controllers/frame.go @@ -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{ diff --git a/backend/controllers/game.go b/backend/controllers/game.go index b9509d8..9735019 100644 --- a/backend/controllers/game.go +++ b/backend/controllers/game.go @@ -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{ diff --git a/backend/controllers/godot_version.go b/backend/controllers/godot_version.go index e7a721f..230449a 100644 --- a/backend/controllers/godot_version.go +++ b/backend/controllers/godot_version.go @@ -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{ diff --git a/backend/controllers/level.go b/backend/controllers/level.go index 3c552e2..d05ffe1 100644 --- a/backend/controllers/level.go +++ b/backend/controllers/level.go @@ -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{ diff --git a/backend/controllers/object.go b/backend/controllers/object.go index 52fce3c..b45e85d 100644 --- a/backend/controllers/object.go +++ b/backend/controllers/object.go @@ -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{ diff --git a/backend/controllers/object_name.go b/backend/controllers/object_name.go index c55b2f8..07f4e48 100644 --- a/backend/controllers/object_name.go +++ b/backend/controllers/object_name.go @@ -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{ diff --git a/backend/controllers/object_state.go b/backend/controllers/object_state.go index 2e3fe68..2021134 100644 --- a/backend/controllers/object_state.go +++ b/backend/controllers/object_state.go @@ -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{ diff --git a/backend/controllers/os.go b/backend/controllers/os.go index ab7b202..6cbce7b 100644 --- a/backend/controllers/os.go +++ b/backend/controllers/os.go @@ -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{ diff --git a/backend/controllers/player.go b/backend/controllers/player.go index 82d74be..6dc7a79 100644 --- a/backend/controllers/player.go +++ b/backend/controllers/player.go @@ -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{ diff --git a/backend/controllers/user.go b/backend/controllers/user.go index 079dcf0..e17e089 100644 --- a/backend/controllers/user.go +++ b/backend/controllers/user.go @@ -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{ diff --git a/backend/utils/database_filters.go b/backend/utils/database_filters.go index 4426159..8877401 100644 --- a/backend/utils/database_filters.go +++ b/backend/utils/database_filters.go @@ -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{})