add limits to list endpoints

This commit is contained in:
Chris Cromer 2023-02-01 16:20:29 -03:00
parent b179808592
commit 3960bba3eb
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
10 changed files with 280 additions and 10 deletions

View File

@ -2,6 +2,7 @@ package controllers
import (
"encoding/json"
"math"
"net/http"
"strconv"
@ -18,7 +19,33 @@ func ListFrame(writer http.ResponseWriter, request *http.Request, params httprou
var frame []models.Frame
result := gdb.Model(&models.Frame{}).Order("ID asc").Find(&frame)
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)))
}
result := gdb.Model(&models.Frame{}).Order("ID asc").Limit(limit).Offset(offset).Find(&frame)
if result.Error != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error())
return

View File

@ -4,6 +4,7 @@ import (
"compress/gzip"
"encoding/base64"
"encoding/json"
"math"
"net/http"
"strconv"
@ -20,7 +21,33 @@ func ListGame(writer http.ResponseWriter, request *http.Request, params httprout
var games []models.Game
result := gdb.Model(&models.Game{}).Order("ID asc").Find(&games)
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)))
}
result := gdb.Model(&models.Game{}).Order("ID asc").Limit(limit).Offset(offset).Find(&games)
if result.Error != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error())
return

View File

@ -2,6 +2,7 @@ package controllers
import (
"encoding/json"
"math"
"net/http"
"strconv"
@ -18,7 +19,33 @@ func ListGodotVersion(writer http.ResponseWriter, request *http.Request, params
var godotVersion []models.GodotVersion
result := gdb.Model(&models.GodotVersion{}).Order("ID asc").Find(&godotVersion)
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)))
}
result := gdb.Model(&models.GodotVersion{}).Order("ID asc").Limit(limit).Offset(offset).Find(&godotVersion)
if result.Error != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error())
return

View File

@ -2,6 +2,7 @@ package controllers
import (
"encoding/json"
"math"
"net/http"
"strconv"
@ -18,7 +19,33 @@ func ListLevel(writer http.ResponseWriter, request *http.Request, params httprou
var level []models.Level
result := gdb.Model(&models.Level{}).Order("ID asc").Find(&level)
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)))
}
result := gdb.Model(&models.Level{}).Order("ID asc").Limit(limit).Offset(offset).Find(&level)
if result.Error != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error())
return

View File

@ -2,6 +2,7 @@ package controllers
import (
"encoding/json"
"math"
"net/http"
"strconv"
@ -18,7 +19,33 @@ func ListObject(writer http.ResponseWriter, request *http.Request, params httpro
var object []models.Object
result := gdb.Model(&models.Object{}).Order("ID asc").Find(&object)
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)))
}
result := gdb.Model(&models.Object{}).Order("ID asc").Limit(limit).Offset(offset).Find(&object)
if result.Error != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error())
return

View File

@ -2,6 +2,7 @@ package controllers
import (
"encoding/json"
"math"
"net/http"
"strconv"
@ -18,7 +19,33 @@ func ListObjectName(writer http.ResponseWriter, request *http.Request, params ht
var objectName []models.ObjectName
result := gdb.Model(&models.ObjectName{}).Order("ID asc").Find(&objectName)
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)))
}
result := gdb.Model(&models.ObjectName{}).Order("ID asc").Limit(limit).Offset(offset).Find(&objectName)
if result.Error != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error())
return

View File

@ -2,6 +2,7 @@ package controllers
import (
"encoding/json"
"math"
"net/http"
"strconv"
@ -18,7 +19,33 @@ func ListObjectState(writer http.ResponseWriter, request *http.Request, params h
var objectState []models.ObjectState
result := gdb.Model(&models.ObjectState{}).Order("ID asc").Find(&objectState)
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)))
}
result := gdb.Model(&models.ObjectState{}).Order("ID asc").Limit(limit).Offset(offset).Find(&objectState)
if result.Error != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error())
return

View File

@ -2,6 +2,7 @@ package controllers
import (
"encoding/json"
"math"
"net/http"
"strconv"
@ -18,7 +19,33 @@ func ListOS(writer http.ResponseWriter, request *http.Request, params httprouter
var os []models.OS
result := gdb.Model(&models.OS{}).Order("ID asc").Find(&os)
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)))
}
result := gdb.Model(&models.OS{}).Order("ID asc").Limit(limit).Offset(offset).Find(&os)
if result.Error != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error())
return

View File

@ -2,6 +2,7 @@ package controllers
import (
"encoding/json"
"math"
"net/http"
"strconv"
@ -18,7 +19,33 @@ func ListPlayer(writer http.ResponseWriter, request *http.Request, params httpro
var player []models.Player
result := gdb.Model(&models.Player{}).Order("ID asc").Find(&player)
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)))
}
result := gdb.Model(&models.Player{}).Order("ID asc").Limit(limit).Offset(offset).Find(&player)
if result.Error != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error())
return

View File

@ -3,6 +3,7 @@ package controllers
import (
"encoding/json"
"errors"
"math"
"net/http"
"strconv"
@ -71,7 +72,33 @@ func ListUser(writer http.ResponseWriter, request *http.Request, params httprout
var users []models.User
result := gdb.Model(&models.User{}).Order("ID asc").Find(&users)
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)))
}
result := gdb.Model(&models.User{}).Order("ID asc").Limit(limit).Offset(offset).Find(&users)
if result.Error != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error())
return